PageRenderTime 27ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/oldtests/graph_test.py

https://github.com/deepreds/pygr
Python | 169 lines | 162 code | 7 blank | 0 comment | 18 complexity | 405a045db92b7339dc48bb65ee23a865 MD5 | raw file
  1. import pygrtest_common
  2. from nosebase import skip_errors
  3. from pygr import mapping,graphquery
  4. class Query_Test(object):
  5. "Pygr Query tests..."
  6. def dqcmp(self,datagraph,querygraph,result):
  7. try:
  8. g = self.datagraph
  9. except AttributeError:
  10. pass
  11. else:
  12. g.update(datagraph)
  13. datagraph = g
  14. result_kounter = 0
  15. l = [d.copy() for d in graphquery.GraphQuery(datagraph,querygraph)]
  16. assert len(l)==len(result),'length mismatch'
  17. l.sort()
  18. result.sort()
  19. for i in range(len(l)):
  20. assert l[i]==result[i],'incorrect result'
  21. def basicquery_test(self): # Basic Query Test
  22. datagraph = {0: {1: None, 2: None, 3: None}, 1: {2:None}, 3: {4: None, 5: None},
  23. 4: {6: None}, 5: {6: None}, 2: {}, 6: {}}
  24. querygraph = {0: {1: None, 2: None, 3: None}, 3:{4: None},1:{},2:{},4:{}}
  25. result = [{0: 0, 1: 1, 2: 2, 3: 3, 4: 4}, {0: 0, 1: 1, 2: 2, 3: 3, 4: 5},
  26. {0: 0, 1: 2, 2: 1, 3: 3, 4: 4}, {0: 0, 1: 2, 2: 1, 3: 3, 4: 5}]
  27. self.dqcmp(datagraph,querygraph,result)
  28. def cyclicquery_test(self): # Test cyclic QG against cyclic DG
  29. datagraph = {1:{2:None},2:{3:None},3:{4:None},4:{5:None},5:{2:None}}
  30. querygraph = {0:{1:None},1:{2:None},2:{4:None},3:{1:None},4:{3:None}}
  31. result = [{0: 1, 1: 2, 2: 3, 3: 5, 4: 4}]
  32. self.dqcmp(datagraph,querygraph,result)
  33. def cyclicacyclicquery_test(self): # Test cyclic QG against acyclic DG
  34. datagraph = {0:{1:None},1:{3:None},5:{3:None},4:{5:None},2:{4:None,1:None},3:{}}
  35. querygraph = {0:{1:None},1:{3:None},3:{5:None},5:{4:None},4:{2:None},2:{1:None}}
  36. result = []
  37. self.dqcmp(datagraph,querygraph,result)
  38. def symmetricquery_test(self): # Test symmetrical QG against symmetrical DG
  39. datagraph = {1:{2:None},2:{3:None,4:None},5:{2:None},3:{},4:{}}
  40. querygraph = {0:{1:None},1:{2:None},2:{}}
  41. result = [{0: 1, 1: 2, 2: 3}, {0: 1, 1: 2, 2: 4},
  42. {0: 5, 1: 2, 2: 3}, {0: 5, 1: 2, 2: 4}]
  43. self.dqcmp(datagraph,querygraph,result)
  44. def filteredquery_test(self): # Test a filter against a query
  45. datagraph = {0: {1: None, 2: None, 3: None}, 1: {2: None, 3: None}, 3: {4: None}}
  46. querygraph = {0:{1:{'filter':lambda toNode,**kw:toNode == 3}},1:{}}
  47. result = [{0: 0, 1: 3},{0: 1, 1: 3}]
  48. self.dqcmp(datagraph,querygraph,result)
  49. def headlessquery_test(self): # Test a query with no head nodes
  50. datagraph = {0:{1:None},1:{2:None},2:{3:None},3:{4:None},4:{1:None}}
  51. querygraph = {0:{1:None},1:{2:None},2:{3:None},3:{0:None}}
  52. result = [{0: 1, 1: 2, 2: 3, 3: 4},
  53. {0: 2, 1: 3, 2: 4, 3: 1},
  54. {0: 3, 1: 4, 2: 1, 3: 2},
  55. {0: 4, 1: 1, 2: 2, 3: 3}]
  56. self.dqcmp(datagraph,querygraph,result)
  57. class Mapping_Test(Query_Test):
  58. def setup(self):
  59. self.datagraph = mapping.dictGraph()
  60. def graphdict_test(self):
  61. datagraph = self.datagraph
  62. datagraph += 1
  63. datagraph[1] += 2
  64. results = {1: {2: None}, 2: {}}
  65. assert datagraph == results, 'incorrect result'
  66. def nodedel_test(self):
  67. datagraph = self.datagraph
  68. datagraph += 1
  69. datagraph += 2
  70. datagraph[2] += 3
  71. datagraph -= 1
  72. results = {2: {3: None}, 3: {}}
  73. assert datagraph == results, 'incorrect result'
  74. def delraise_test(self):
  75. datagraph = self.datagraph
  76. datagraph += 1
  77. datagraph += 2
  78. datagraph[2] += 3
  79. try:
  80. for i in range(0,2):
  81. datagraph -= 3
  82. raise ValueError('failed to catch bad node deletion attempt')
  83. except KeyError:
  84. pass # THIS IS THE CORRECT RESULT
  85. def setitemraise_test(self):
  86. datagraph = self.datagraph
  87. datagraph += 1
  88. try:
  89. datagraph[1] = 2
  90. raise KeyError('failed to catch bad setitem attempt')
  91. except ValueError:
  92. pass # THIS IS THE CORRECT RESULT
  93. def graphedges_test(self):
  94. datagraph = self.datagraph
  95. graphvals = {1:{2:None},2:{3:None,4:None},5:{2:None},3:{},4:{}}
  96. edge_list = [[1, 2,None], [2, 3,None], [2, 4,None], [5, 2,None]]
  97. for i in graphvals:
  98. datagraph += i
  99. for n in graphvals[i].keys():
  100. datagraph[i] += n
  101. edge_results = []
  102. for e in datagraph.edges():
  103. edge_results.append(e)
  104. edge_results.sort()
  105. edge_results = [list(t) for t in edge_results]
  106. edge_list.sort()
  107. print 'edge_results:',edge_results
  108. assert edge_results == edge_list, 'incorrect result'
  109. class Graph_Test(Mapping_Test):
  110. 'run same tests on mapping.Graph class'
  111. def setup(self):
  112. self.datagraph = mapping.Graph()
  113. class GraphShelve_Test(Mapping_Test):
  114. 'run same tests on mapping.Graph class'
  115. def setup(self):
  116. from nosebase import TempDir
  117. tmp = TempDir()
  118. self.datagraph = mapping.Graph(filename=tmp.subfile('mygraph'),
  119. intKeys=True)
  120. self.tempdir = tmp # KEEP BOUND SO NOT DELETED UNTIL THIS TEST COMPLETED
  121. def teardown(self):
  122. self.datagraph.close() # close shelve before deleting directory
  123. self.tempdir.__del__() # FORCE IT TO DELETE TEMPORARY DIRECTORY
  124. class SQLGraph_Test(Mapping_Test):
  125. 'run same tests on mapping.SQLGraph class'
  126. @skip_errors(ImportError)
  127. def setup(self):
  128. from pygr import sqlgraph
  129. import MySQLdb # test will be skipped if unavailable
  130. createOpts = dict(source_id='int', target_id='int', edge_id='int')
  131. try:
  132. self.datagraph = sqlgraph.SQLGraph('test.dumbo_foo_test',
  133. dropIfExists=True,
  134. createTable=createOpts)
  135. except MySQLdb.MySQLError:
  136. tempcurs = sqlgraph.getNameCursor()[1]
  137. try: # hmm, maybe need to create the test database
  138. tempcurs.execute('create database if not exists test')
  139. self.datagraph = sqlgraph.SQLGraph('test.dumbo_foo_test',
  140. dropIfExists=True,
  141. createTable=createOpts)
  142. except MySQLdb.MySQLError: # no server, database or privileges?
  143. print """The MySQL 'test' database doesn't exist and/or can't be
  144. created or accessed on this account. This test will be skipped
  145. """
  146. raise ImportError # skip tests.
  147. def teardown(self):
  148. self.datagraph.cursor.execute('drop table if exists test.dumbo_foo_test')
  149. class Splicegraph_Test(object):
  150. @skip_errors(KeyError)
  151. def setup(self):
  152. import pygr.Data
  153. self.sg = pygr.Data.Bio.Annotation.ASAP2.Isoform.HUMAN.hg17.splicegraph()
  154. def exonskip_megatest(self):
  155. 'perform exon skip query'
  156. from pygr import graphquery
  157. query = {0:{1:None,2:None},1:{2:None},2:{}}
  158. gq = graphquery.GraphQuery(self.sg,query)
  159. l = list(gq)
  160. assert len(l)==11546,'test exact size of exonskip set'