OBOImporterTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace Tests;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. class OBOImporterTest extends TripalTestCase {
  6. // Uncomment to auto start and rollback db transactions per test method.
  7. use DBTransaction;
  8. /**
  9. * Tests that the Goslim ontology loads using a remote URL. Ensure subgroups
  10. * load.
  11. *
  12. * @group obo
  13. */
  14. public function testGO_SLIM_load() {
  15. $name = 'core_test_goslim_plant';
  16. $path = 'http://www.geneontology.org/ontology/subsets/goslim_plant.obo';
  17. $this->load_obo($name, $path);
  18. $exists = db_select('chado.cv', 'c')
  19. ->fields('c', ['cv_id'])
  20. ->condition('name', 'biological_process')
  21. ->execute()
  22. ->fetchField();
  23. $this->assertNotFalse($exists);
  24. $exists = db_select('chado.cv', 'c')
  25. ->fields('c', ['cv_id'])
  26. ->condition('name', 'cellular_component')
  27. ->execute()
  28. ->fetchField();
  29. $this->assertNotFalse($exists);
  30. $exists = db_select('chado.cv', 'c')
  31. ->fields('c', ['cv_id'])
  32. ->condition('name', 'molecular_function')
  33. ->execute()
  34. ->fetchField();
  35. $this->assertNotFalse($exists);
  36. $sql = "
  37. SELECT DISTINCT CVTP.value
  38. FROM {cvtermprop} CVTP
  39. INNER JOIN {cvterm} CVTPT on CVTPT.cvterm_id = CVTP.type_id
  40. INNER JOIN {cvterm} CVT on CVT.cvterm_id = CVTP.cvterm_id
  41. INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
  42. INNER JOIN {db} DB on DB.db_id = DBX.db_id
  43. WHERE CVTPT.name = 'Subgroup' and DB.name = 'GO' and CVTP.value = 'goslim_plant'
  44. ";
  45. $exists = chado_query($sql)->fetchField();
  46. $this->assertNotFalse($exists);
  47. }
  48. /**
  49. * @param $name - ontology name. This goes in the tripal_cv_obo table.
  50. * @param $path - path to the OBO. this can be a file path or a URL.
  51. *
  52. * @throws \Exception
  53. */
  54. private function load_obo($name, $path) {
  55. $obo_id = db_select('public.tripal_cv_obo', 't')
  56. ->fields('t', ['obo_id'])
  57. ->condition('t.name', $name)
  58. ->execute()
  59. ->fetchField();
  60. if (!$obo_id) {
  61. $obo_id = db_insert('public.tripal_cv_obo')
  62. ->fields(['name' => $name, 'path' => $path])
  63. ->execute();
  64. }
  65. $run_args = ['obo_id' => $obo_id];
  66. module_load_include('inc', 'tripal_chado', 'includes/TripalImporter/OBOImporter');
  67. $importer = new \OBOImporter();
  68. $importer->create($run_args);
  69. $importer->prepareFiles();
  70. $importer->run();
  71. }
  72. /**
  73. * Ensure SO can be loaded. Ensure that synonyms are set properly.
  74. *
  75. * @group obo
  76. * @ticket 525
  77. */
  78. public function test_relationships_in_SO_exist() {
  79. // step 1: drop the SO CV and CASCADE.
  80. $result = chado_query("SET search_path to public, chado;
  81. DELETE FROM {cv} WHERE name = 'sequence'");
  82. $result = chado_query("SET search_path to public, chado;
  83. DELETE FROM {db} WHERE name = 'SO'");
  84. // step 2: re-add SO.
  85. $name = 'Sequence Ontology';
  86. $path = 'http://purl.obolibrary.org/obo/so.obo';
  87. $this->load_obo($name, $path);
  88. $sql = "SELECT CVT.name, CVTSYN.synonym
  89. FROM {cvterm} CVT
  90. INNER JOIN {dbxref} DBX on DBX.dbxref_id = CVT.dbxref_id
  91. INNER JOIN {db} on DB.db_id = DBX.db_id
  92. LEFT JOIN {cvtermsynonym} CVTSYN on CVTSYN.cvterm_id = CVT.cvterm_id
  93. WHERE DB.name = 'SO' and CVT.name = 'supercontig'
  94. ORDER BY DBX.accession";
  95. $results = chado_query($sql)->fetchAll();
  96. $result = $results[0];
  97. $this->assertNotNull($result);
  98. $this->assertNotEmpty($result);
  99. $this->assertEquals("supercontig", $result->name);
  100. $this->assertEquals("scaffold", $result->synonym);
  101. }
  102. /**
  103. * Test simply that nodes are inserted.
  104. ** @group obo
  105. *
  106. */
  107. public function test_cvtermpath_cv_nodes_inserted() {
  108. $name = 'path_test_mini';
  109. $path = __DIR__ . '/../example_files/cvtermpath_test.obo';
  110. $this->load_obo($name, $path);
  111. //Check cvtermpath is sane.
  112. $cv_name = 'cvtermpath_test';
  113. $nodes = [
  114. 'node01',
  115. 'node02',
  116. 'node03',
  117. 'node04',
  118. 'node05',
  119. 'node06',
  120. 'node07',
  121. 'node08',
  122. 'node09',
  123. 'node10',
  124. 'node11',
  125. 'node12',
  126. 'node13',
  127. 'node14',
  128. ];
  129. //check nodes are inserted.
  130. foreach ($nodes as $node) {
  131. $query = db_select('chado.cvterm', 'cvt')
  132. ->fields('cvt', ['cvterm_id', 'cv_id'])
  133. ->condition('cvt.name', $node);
  134. $query->join('chado.cv', 'cv', 'cvt.cv_id = cv.cv_id');
  135. $query->condition('cv.name', $cv_name);
  136. $result = $query->execute()
  137. ->fetchObject();
  138. $this->assertNotFalse($result);
  139. }
  140. }
  141. /**
  142. * This data provider currently returns an array of data in the following manner:
  143. * item[0] - the object node
  144. * item[1] - an array containing a list of subject nodes, ie, nodes that claim they have a "is_a" relationship with this node.
  145. *
  146. * @return array
  147. */
  148. public function node_data_provider() {
  149. $data = [
  150. [
  151. 'node01',// object
  152. [ //subjects
  153. 'node02',
  154. 'node03',
  155. 'node04',
  156. 'node05',
  157. 'node06',
  158. 'node07',
  159. 'node08',
  160. 'node09',
  161. 'node10',
  162. 'node11',
  163. 'node12',
  164. 'node13',
  165. // 'node14',
  166. //Node 14 is not connected!
  167. ],
  168. ],
  169. [
  170. 'node04',
  171. ['node09','node11'],
  172. ],
  173. [
  174. 'node11',
  175. ['node13',],
  176. ],
  177. ];
  178. return $data;
  179. }
  180. /**
  181. * @group obo
  182. * @dataProvider node_data_provider
  183. */
  184. public function test_cvtermpath_correct($object, $subjects) {
  185. $name = 'path_test_mini';
  186. $path = __DIR__ . '/../example_files/cvtermpath_test.obo';
  187. $this->load_obo($name, $path);
  188. $cv_name = 'cvtermpath_test';
  189. $cv_id = chado_get_cv(['name' => $cv_name])->cv_id;
  190. //populate cvtermpath
  191. chado_update_cvtermpath($cv_id);
  192. $query = db_select('chado.cvtermpath', 'cp');
  193. $query->fields('cp', ['pathdistance']);
  194. $query->condition('cp.cv_id', $cv_id);
  195. $query->join('chado.cvterm', 'subject', 'cp.subject_id = subject.cvterm_id');
  196. $query->join('chado.cvterm', 'object', 'cp.object_id = object.cvterm_id');
  197. $query->condition('object.name', $object);
  198. $query->fields('subject', ['name']);
  199. //First, ensure that the number of relationships is correct
  200. $countquery = clone $query;
  201. $results = $countquery->execute()->fetchAll();
  202. $this->assertEquals(count($subjects), count($results));
  203. foreach ($subjects as $subject) {
  204. $query_copy = clone $query;
  205. $query_copy->condition('subject.name', $subject);
  206. $results = $query_copy->execute()->fetchObject();
  207. $this->assertNotFalse($results, "failed for {$object} as object and {$subject} as subject.");
  208. }
  209. }
  210. /**
  211. * ONLY node 1 should be root for test OBO.
  212. *
  213. * @group obo
  214. */
  215. public function test_cvtermpath_mview_root_terms_correct() {
  216. $name = 'path_test_mini';
  217. $path = __DIR__ . '/../example_files/cvtermpath_test.obo';
  218. $this->load_obo($name, $path);
  219. //populate mview
  220. chado_populate_mview(chado_get_mview_id('cv_root_mview'));
  221. $cv_name = 'cvtermpath_test';
  222. $roots = db_select('chado.cv_root_mview', 't')
  223. ->fields('t', ['name'])
  224. ->condition('cv_name', $cv_name)
  225. ->execute()
  226. ->fetchAll();
  227. $this->assertNotEmpty($roots);
  228. $this->assertLessThan(2, count($roots));
  229. $this->assertEquals("node01", $roots[0]->name);
  230. }
  231. /**
  232. * Test OBO has a synonym. check its inserted properly.
  233. * @group obo
  234. * @group wip
  235. */
  236. public function test_synonyms_are_loaded(){
  237. //node11 has a synonym:"crazy node" EXACT []
  238. $name = 'path_test_mini';
  239. $path = __DIR__ . '/../example_files/cvtermpath_test.obo';
  240. $this->load_obo($name, $path);
  241. $cv_name = 'cvtermpath_test';
  242. $query = db_select('chado.cvtermsynonym', 't')
  243. ->fields('t', ['synonym']);
  244. $query->join('chado.cvterm', 'cvt', 't.cvterm_id = cvt.cvterm_id');
  245. $query->condition('cvt.name', 'node11');
  246. $result = $query->execute()->fetchField();
  247. $this->assertNotFalse($result);
  248. $this->assertEquals("crazy node", $result);
  249. }
  250. /**
  251. * Test OBO has a xref. check its inserted properly.
  252. * @group obo
  253. * @group wip
  254. */
  255. public function test_dbxref_loaded(){
  256. //node11 has a synonym:"crazy node" EXACT []
  257. $name = 'path_test_mini';
  258. $path = __DIR__ . '/../example_files/cvtermpath_test.obo';
  259. $this->load_obo($name, $path);
  260. $cv_name = 'cvtermpath_test';
  261. $query = db_select('chado.cvterm_dbxref', 't');
  262. $query->join('chado.cvterm', 'cvt', 't.cvterm_id = cvt.cvterm_id');
  263. $query->join('chado.dbxref', 'dbx', 'dbx.dbxref_id = t.dbxref_id');
  264. $query->fields('dbx', ['accession']);
  265. $query->condition('cvt.name', 'node11');
  266. $result = $query->execute()->fetchField();
  267. $this->assertNotFalse($result);
  268. $this->assertEquals("0043226", $result);
  269. }
  270. /**
  271. * ensure that new CV's aren't accidentally created when term names have
  272. * colons in them.
  273. *
  274. * @group obo
  275. * @ticket 525
  276. */
  277. public function test_PTO_loads_colon_issue() {
  278. $name = 'core_test_PTO_mini';
  279. $path = __DIR__ . '/../example_files/pto_colon.obo';
  280. $this->load_obo($name, $path);
  281. $exists = db_select('chado.cv', 'c')
  282. ->fields('c', ['cv_id'])
  283. ->condition('name', 'core_test_PTO_mini')
  284. ->execute()
  285. ->fetchField();
  286. $this->assertNotFalse($exists);
  287. //hte colon splitting issue: a new CV will created named fatty acid 18
  288. $exists = db_select('chado.cv', 'c')
  289. ->fields('c', ['cv_id'])
  290. ->condition('name', 'fatty acid 18')
  291. ->execute()
  292. ->fetchField();
  293. $this->assertFalse($exists);
  294. }
  295. }