GFF3ImporterTest.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. <?php
  2. namespace Tests;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. class GFF3ImporterTest extends TripalTestCase {
  6. use DBTransaction;
  7. /**
  8. * Confirm basic GFF importer functionality.
  9. *
  10. * @group gff
  11. */
  12. public function testGFFImporter() {
  13. $gff_file = ['file_local' => __DIR__ . '/../data/small_gene.gff'];
  14. $fasta = ['file_local' => __DIR__ . '/../data/short_scaffold.fasta'];
  15. $analysis = factory('chado.analysis')->create();
  16. $organism = factory('chado.organism')->create();
  17. $run_args = [
  18. 'analysis_id' => $analysis->analysis_id,
  19. 'organism_id' => $organism->organism_id,
  20. 'use_transaction' => 1,
  21. 'add_only' => 0,
  22. 'update' => 1,
  23. 'create_organism' => 0,
  24. 'create_target' => 0,
  25. // regexps for mRNA and protein.
  26. 're_mrna' => NULL,
  27. 're_protein' => NULL,
  28. // optional
  29. 'target_organism_id' => NULL,
  30. 'target_type' => NULL,
  31. 'start_line' => NULL,
  32. 'landmark_type' => NULL,
  33. 'alt_id_attr' => NULL,
  34. ];
  35. $this->loadLandmarks($analysis, $organism, $fasta);
  36. $this->runGFFLoader($run_args, $gff_file);
  37. // This protein is an explicit protein / polypeptide imported from the GFF
  38. // file.
  39. $name = 'test_protein_001.1';
  40. $query = db_select('chado.feature', 'f')
  41. ->fields('f', ['uniquename'])
  42. ->condition('f.uniquename', $name)
  43. ->execute()
  44. ->fetchField();
  45. $this->assertEquals($name, $query);
  46. }
  47. /**
  48. * Run the GFF loader on gff_tag_unescaped_character.gff for testing.
  49. *
  50. * This tests whether the GFF loader adds IDs that contain a comma.
  51. * The GFF loader should allow it
  52. */
  53. public function testGFFImporterUnescapedTagWithComma() {
  54. $gff_file = ['file_local' => __DIR__ . '/../data/gff_tag_unescaped_character.gff'];
  55. $analysis = factory('chado.analysis')->create();
  56. $organism = factory('chado.organism')->create();
  57. $run_args = [
  58. 'analysis_id' => $analysis->analysis_id,
  59. 'organism_id' => $organism->organism_id,
  60. 'use_transaction' => 1,
  61. 'add_only' => 0,
  62. 'update' => 1,
  63. 'create_organism' => 0,
  64. 'create_target' => 0,
  65. // regexps for mRNA and protein.
  66. 're_mrna' => NULL,
  67. 're_protein' => NULL,
  68. // optional
  69. 'target_organism_id' => NULL,
  70. 'target_type' => NULL,
  71. 'start_line' => NULL,
  72. 'landmark_type' => NULL,
  73. 'alt_id_attr' => NULL,
  74. ];
  75. $this->loadLandmarks($analysis, $organism);
  76. // This should throw an error based on the tag name having the comma
  77. $hasException = false;
  78. try {
  79. $this->runGFFLoader($run_args, $gff_file);
  80. }
  81. catch (\Exception $ex) {
  82. $hasException = true;
  83. }
  84. $this->assertEquals($hasException, true);
  85. }
  86. /**
  87. * Run the GFF loader on gff_tagvalue_unescaped_character.gff for testing.
  88. *
  89. * This tests whether the GFF loader adds IDs that contain a comma.
  90. * The GFF loader should allow it
  91. */
  92. public function testGFFImporterUnescapedTagValueWithComma() {
  93. $gff_file = ['file_local' => __DIR__ . '/../data/gff_tagvalue_unescaped_character.gff'];
  94. $analysis = factory('chado.analysis')->create();
  95. $organism = factory('chado.organism')->create();
  96. $run_args = [
  97. 'analysis_id' => $analysis->analysis_id,
  98. 'organism_id' => $organism->organism_id,
  99. 'use_transaction' => 1,
  100. 'add_only' => 0,
  101. 'update' => 1,
  102. 'create_organism' => 0,
  103. 'create_target' => 0,
  104. // regexps for mRNA and protein.
  105. 're_mrna' => NULL,
  106. 're_protein' => NULL,
  107. // optional
  108. 'target_organism_id' => NULL,
  109. 'target_type' => NULL,
  110. 'start_line' => NULL,
  111. 'landmark_type' => NULL,
  112. 'alt_id_attr' => NULL,
  113. ];
  114. $this->loadLandmarks($analysis, $organism);
  115. // This should throw an error based on the tag name having the comma
  116. $hasException = false;
  117. try {
  118. $this->runGFFLoader($run_args, $gff_file);
  119. }
  120. catch (\Exception $ex) {
  121. $hasException = true;
  122. }
  123. $this->assertEquals($hasException, true);
  124. }
  125. /**
  126. * Run the GFF loader on gff_seqid_invalid_character.gff for testing.
  127. * Seqids seem to also be called landmarks within GFF loader.
  128. * This tests whether the GFF loader has any issues with characters like
  129. * single quotes.
  130. */
  131. public function testGFFImporterSeqidWithInvalidCharacter() {
  132. $gff_file = ['file_local' =>
  133. __DIR__ . '/../data/gff_seqid_invalid_character.gff'];
  134. $analysis = factory('chado.analysis')->create();
  135. $organism = factory('chado.organism')->create();
  136. $run_args = [
  137. 'analysis_id' => $analysis->analysis_id,
  138. 'organism_id' => $organism->organism_id,
  139. 'use_transaction' => 1,
  140. 'add_only' => 0,
  141. 'update' => 1,
  142. 'create_organism' => 0,
  143. 'create_target' => 0,
  144. // regexps for mRNA and protein.
  145. 're_mrna' => NULL,
  146. 're_protein' => NULL,
  147. // optional
  148. 'target_organism_id' => NULL,
  149. 'target_type' => NULL,
  150. 'start_line' => NULL,
  151. 'landmark_type' => NULL,
  152. 'alt_id_attr' => NULL,
  153. ];
  154. $this->loadLandmarks($analysis, $organism);
  155. // This will produce an exception due to quote character in Seqid
  156. $hasException = false;
  157. try {
  158. $this->runGFFLoader($run_args, $gff_file);
  159. }
  160. catch (\Exception $ex) {
  161. $hasException = true;
  162. }
  163. $this->assertEquals($hasException, true);
  164. }
  165. /**
  166. * Run the GFF loader on gff_unescaped_ids.gff for testing.
  167. *
  168. * This tests whether the GFF loader adds IDs that contain whitespaces.
  169. * The GFF loader should allow it
  170. */
  171. public function testGFFImporterUnescapedWhitespaceID() {
  172. $gff_file = ['file_local' => __DIR__ . '/../data/gff_unescaped_ids.gff'];
  173. $analysis = factory('chado.analysis')->create();
  174. $organism = factory('chado.organism')->create();
  175. $run_args = [
  176. 'analysis_id' => $analysis->analysis_id,
  177. 'organism_id' => $organism->organism_id,
  178. 'use_transaction' => 1,
  179. 'add_only' => 0,
  180. 'update' => 1,
  181. 'create_organism' => 0,
  182. 'create_target' => 0,
  183. // regexps for mRNA and protein.
  184. 're_mrna' => NULL,
  185. 're_protein' => NULL,
  186. // optional
  187. 'target_organism_id' => NULL,
  188. 'target_type' => NULL,
  189. 'start_line' => NULL,
  190. 'landmark_type' => NULL,
  191. 'alt_id_attr' => NULL,
  192. ];
  193. $this->loadLandmarks($analysis, $organism);
  194. // This should go through just fine
  195. $this->runGFFLoader($run_args, $gff_file);
  196. $results = db_query("SELECT * FROM chado.feature WHERE uniquename =
  197. 'FRAEX38873_v2_000000010 SPACED';");
  198. $this->assertEquals($results->rowCount(), 1);
  199. }
  200. /**
  201. * Run the GFF loader on gff_rightarrow_ids.gff for testing.
  202. *
  203. * This tests whether the GFF loader fails if ID contains
  204. * arrow >. It should not fail.
  205. */
  206. public function testGFFImporterRightArrowID() {
  207. $gff_file = ['file_local' => __DIR__ . '/../data/gff_rightarrow_id.gff'];
  208. $analysis = factory('chado.analysis')->create();
  209. $organism = factory('chado.organism')->create();
  210. $run_args = [
  211. 'analysis_id' => $analysis->analysis_id,
  212. 'organism_id' => $organism->organism_id,
  213. 'use_transaction' => 1,
  214. 'add_only' => 0,
  215. 'update' => 1,
  216. 'create_organism' => 0,
  217. 'create_target' => 0,
  218. // regexps for mRNA and protein.
  219. 're_mrna' => NULL,
  220. 're_protein' => NULL,
  221. // optional
  222. 'target_organism_id' => NULL,
  223. 'target_type' => NULL,
  224. 'start_line' => NULL,
  225. 'landmark_type' => NULL,
  226. 'alt_id_attr' => NULL,
  227. ];
  228. $this->loadLandmarks($analysis, $organism);
  229. // This will produce an exception due to right arrow in ID
  230. $this->runGFFLoader($run_args, $gff_file);
  231. $results = db_query("SELECT * FROM chado.feature
  232. WHERE uniquename = '>FRAEX38873_v2_000000010';");
  233. // We expect this record to get inserted.
  234. $this->assertEquals($results->rowCount(), 1);
  235. }
  236. /**
  237. * Run the GFF loader on gff_duplicate_ids.gff for testing.
  238. *
  239. * This tests whether the GFF loader detects duplicate IDs which makes a
  240. * GFF file invalid since IDs should be unique. The GFF loader should throw
  241. * and exception which this test checks for
  242. */
  243. public function testGFFImporterDuplicateIDsExceptionCheck() {
  244. $gff_file = ['file_local' => __DIR__ . '/../data/gff_duplicate_ids.gff'];
  245. $analysis = factory('chado.analysis')->create();
  246. $organism = factory('chado.organism')->create();
  247. $run_args = [
  248. 'analysis_id' => $analysis->analysis_id,
  249. 'organism_id' => $organism->organism_id,
  250. 'use_transaction' => 1,
  251. 'add_only' => 0,
  252. 'update' => 1,
  253. 'create_organism' => 0,
  254. 'create_target' => 0,
  255. // regexps for mRNA and protein.
  256. 're_mrna' => NULL,
  257. 're_protein' => NULL,
  258. // optional
  259. 'target_organism_id' => NULL,
  260. 'target_type' => NULL,
  261. 'start_line' => NULL,
  262. 'landmark_type' => NULL,
  263. 'alt_id_attr' => NULL,
  264. ];
  265. $hasException = false;
  266. try {
  267. $this->loadLandmarks($analysis, $organism);
  268. // This will produce an exception of duplicate feature ID
  269. $this->runGFFLoader($run_args, $gff_file);
  270. }
  271. catch(\Exception $ex) {
  272. $hasException = true;
  273. }
  274. // We expect an exception to happen so we are looking for a return of true
  275. $this->assertEquals($hasException, true);
  276. }
  277. /**
  278. * Run the GFF loader on gff_invalidstartend.gff for testing.
  279. *
  280. * This tests whether the GFF loader fixes start end values
  281. */
  282. public function testGFFImporterInvalidStartEnd() {
  283. $gff_file = ['file_local' => __DIR__ . '/../data/gff_invalidstartend.gff'];
  284. $analysis = factory('chado.analysis')->create();
  285. $organism = factory('chado.organism')->create();
  286. $run_args = [
  287. 'analysis_id' => $analysis->analysis_id,
  288. 'organism_id' => $organism->organism_id,
  289. 'use_transaction' => 1,
  290. 'add_only' => 0,
  291. 'update' => 1,
  292. 'create_organism' => 0,
  293. 'create_target' => 0,
  294. // regexps for mRNA and protein.
  295. 're_mrna' => NULL,
  296. 're_protein' => NULL,
  297. // optional
  298. 'target_organism_id' => NULL,
  299. 'target_type' => NULL,
  300. 'start_line' => NULL,
  301. 'landmark_type' => NULL,
  302. 'alt_id_attr' => NULL,
  303. ];
  304. $this->loadLandmarks($analysis, $organism);
  305. // This will produce an exception of duplicate feature ID
  306. $this->runGFFLoader($run_args, $gff_file);
  307. $results = db_select('chado.feature', 'f')
  308. ->fields('f', ['uniquename'])
  309. ->condition('f.uniquename', 'FRAEX38873_v2_000000010')
  310. ->execute()
  311. ->fetchAll();
  312. // We expect the feature to still be added to the database
  313. // since the GFF Loader caters for reversing backward numbers
  314. $this->assertEquals(count($results), 1);
  315. }
  316. /**
  317. * Run the GFF loader on gff_score.gff for testing.
  318. *
  319. * This tests whether the GFF loader interprets the score values
  320. */
  321. public function testGFFImporterScoreTest() {
  322. $gff_file = ['file_local' => __DIR__ . '/../data/gff_score.gff'];
  323. $analysis = factory('chado.analysis')->create();
  324. $organism = factory('chado.organism')->create();
  325. $run_args = [
  326. 'analysis_id' => $analysis->analysis_id,
  327. 'organism_id' => $organism->organism_id,
  328. 'use_transaction' => 1,
  329. 'add_only' => 0,
  330. 'update' => 1,
  331. 'create_organism' => 0,
  332. 'create_target' => 0,
  333. // regexps for mRNA and protein.
  334. 're_mrna' => NULL,
  335. 're_protein' => NULL,
  336. // optional
  337. 'target_organism_id' => NULL,
  338. 'target_type' => NULL,
  339. 'start_line' => NULL,
  340. 'landmark_type' => NULL,
  341. 'alt_id_attr' => NULL,
  342. ];
  343. $this->loadLandmarks($analysis, $organism);
  344. $this->runGFFLoader($run_args, $gff_file);
  345. // Test that integer values get placed in the db
  346. $results = db_query('SELECT * FROM chado.analysisfeature WHERE significance = 2 LIMIT 1', array(
  347. ));
  348. foreach ($results as $row){
  349. $this->assertEquals($row->significance,2);
  350. }
  351. // Test that decimal/float values get placed in the db
  352. $results = db_query('SELECT * FROM chado.analysisfeature WHERE significance = 2.5 LIMIT 1', array(
  353. ));
  354. foreach ($results as $row){
  355. $this->assertEquals($row->significance,2.5);
  356. }
  357. // Test that negative score values get placed in the db
  358. $results = db_query('SELECT * FROM chado.analysisfeature WHERE significance = -2.5 LIMIT 1', array(
  359. ));
  360. foreach ($results as $row){
  361. $this->assertEquals($row->significance,-2.5);
  362. }
  363. }
  364. /**
  365. * Run the GFF loader on gff_strand.gff for testing.
  366. *
  367. * This tests whether the GFF loader interprets the strand values
  368. */
  369. public function testGFFImporterInvalidStrandTest() {
  370. $gff_file = ['file_local' => __DIR__ . '/../data/gff_strand_invalid.gff'];
  371. $analysis = factory('chado.analysis')->create();
  372. $organism = factory('chado.organism')->create();
  373. $run_args = [
  374. 'analysis_id' => $analysis->analysis_id,
  375. 'organism_id' => $organism->organism_id,
  376. 'use_transaction' => 1,
  377. 'add_only' => 0,
  378. 'update' => 1,
  379. 'create_organism' => 0,
  380. 'create_target' => 0,
  381. // regexps for mRNA and protein.
  382. 're_mrna' => NULL,
  383. 're_protein' => NULL,
  384. // optional
  385. 'target_organism_id' => NULL,
  386. 'target_type' => NULL,
  387. 'start_line' => NULL,
  388. 'landmark_type' => NULL,
  389. 'alt_id_attr' => NULL,
  390. ];
  391. $this->loadLandmarks($analysis, $organism);
  392. $isException = false;
  393. try {
  394. $this->runGFFLoader($run_args, $gff_file);
  395. }
  396. catch(\Exception $ex) {
  397. $isException = true;
  398. }
  399. $this->assertEquals($isException, true);
  400. }
  401. /**
  402. * Run the GFF loader on gff_strand.gff for testing.
  403. *
  404. * This tests whether the GFF loader interprets the strand values
  405. */
  406. public function testGFFImporterStrandTest() {
  407. $gff_file = ['file_local' => __DIR__ . '/../data/gff_strand.gff'];
  408. $analysis = factory('chado.analysis')->create();
  409. $organism = factory('chado.organism')->create();
  410. $run_args = [
  411. 'analysis_id' => $analysis->analysis_id,
  412. 'organism_id' => $organism->organism_id,
  413. 'use_transaction' => 1,
  414. 'add_only' => 0,
  415. 'update' => 1,
  416. 'create_organism' => 0,
  417. 'create_target' => 0,
  418. // regexps for mRNA and protein.
  419. 're_mrna' => NULL,
  420. 're_protein' => NULL,
  421. // optional
  422. 'target_organism_id' => NULL,
  423. 'target_type' => NULL,
  424. 'start_line' => NULL,
  425. 'landmark_type' => NULL,
  426. 'alt_id_attr' => NULL,
  427. ];
  428. $this->loadLandmarks($analysis, $organism);
  429. $this->runGFFLoader($run_args, $gff_file);
  430. // Test that integer values for strand that get placed in the db
  431. // Strand data gets saved in chado.featureloc
  432. $results = db_query('SELECT * FROM chado.featureloc fl
  433. LEFT JOIN chado.feature f ON (fl.feature_id = f.feature_id)
  434. WHERE uniquename = :uniquename LIMIT 1',
  435. array(
  436. ':uniquename' => 'FRAEX38873_v2_000000010'
  437. )
  438. );
  439. foreach ($results as $row) {
  440. $this->assertEquals($row->strand, 1); // +
  441. }
  442. $results = db_query('SELECT * FROM chado.featureloc fl
  443. LEFT JOIN chado.feature f ON (fl.feature_id = f.feature_id)
  444. WHERE uniquename = :uniquename LIMIT 1',
  445. array(
  446. ':uniquename' => 'FRAEX38873_v2_000000010.1'
  447. )
  448. );
  449. foreach ($results as $row) {
  450. $this->assertEquals($row->strand,-1); // -
  451. }
  452. $results = db_query('SELECT * FROM chado.featureloc fl
  453. LEFT JOIN chado.feature f ON (fl.feature_id = f.feature_id)
  454. WHERE uniquename = :uniquename LIMIT 1',
  455. array(
  456. ':uniquename' => 'FRAEX38873_v2_000000010.2'
  457. )
  458. );
  459. foreach ($results as $row) {
  460. $this->assertEquals($row->strand, 0); // ?
  461. }
  462. $results = db_query('SELECT * FROM chado.featureloc fl
  463. LEFT JOIN chado.feature f ON (fl.feature_id = f.feature_id)
  464. WHERE uniquename = :uniquename LIMIT 1',
  465. array(
  466. ':uniquename' => 'FRAEX38873_v2_000000010.3'
  467. )
  468. );
  469. foreach ($results as $row) {
  470. $this->assertEquals($row->strand, 0); // .
  471. }
  472. // This GFF should create 5 featureloc records
  473. $results = db_query('SELECT COUNT(*) as c FROM chado.featureloc;');
  474. foreach ($results as $row) {
  475. $this->assertEquals($row->c, 5);
  476. }
  477. }
  478. /**
  479. * Run the GFF loader on gff_phase.gff for testing.
  480. *
  481. * This tests whether the GFF loader interprets the phase values correctly
  482. * for CDS rows.
  483. */
  484. public function testGFFImporterPhaseTest() {
  485. $gff_file = ['file_local' => __DIR__ . '/../data/gff_phase.gff'];
  486. $analysis = factory('chado.analysis')->create();
  487. $organism = factory('chado.organism')->create();
  488. $run_args = [
  489. 'analysis_id' => $analysis->analysis_id,
  490. 'organism_id' => $organism->organism_id,
  491. 'use_transaction' => 1,
  492. 'add_only' => 0,
  493. 'update' => 1,
  494. 'create_organism' => 0,
  495. 'create_target' => 0,
  496. // regexps for mRNA and protein.
  497. 're_mrna' => NULL,
  498. 're_protein' => NULL,
  499. // optional
  500. 'target_organism_id' => NULL,
  501. 'target_type' => NULL,
  502. 'start_line' => NULL,
  503. 'landmark_type' => NULL,
  504. 'alt_id_attr' => NULL,
  505. ];
  506. $this->loadLandmarks($analysis, $organism);
  507. $this->runGFFLoader($run_args, $gff_file);
  508. $results = db_query("SELECT * FROM chado.feature
  509. WHERE uniquename = :uniquename LIMIT 1", array(
  510. ':uniquename' => 'FRAEX38873_v2_000000010.1.cds1'
  511. )
  512. );
  513. // Check to make sure it returns a single row (implying a match)
  514. // by the uniquename specified
  515. $this->assertEquals($results->rowCount(), 1);
  516. $results = db_query("SELECT * FROM chado.featureloc
  517. WHERE phase = 1 LIMIT 1", array(
  518. )
  519. );
  520. // Check to make sure it returns a single row (implying a match)
  521. // by phase value 1
  522. $this->assertEquals($results->rowCount(), 1);
  523. }
  524. /**
  525. * Run the GFF loader on gff_phase_invalid_number.gff for testing.
  526. *
  527. * This tests whether the GFF loader interprets the phase values correctly
  528. * for CDS rows when a number outside of the range 0,1,2 is specified.
  529. */
  530. public function testGFFImporterInvalidPhaseNumberTest() {
  531. $gff_file = ['file_local' => __DIR__ . '/../data/gff_phase_invalid_number.gff'];
  532. $analysis = factory('chado.analysis')->create();
  533. $organism = factory('chado.organism')->create();
  534. $run_args = [
  535. 'analysis_id' => $analysis->analysis_id,
  536. 'organism_id' => $organism->organism_id,
  537. 'use_transaction' => 1,
  538. 'add_only' => 0,
  539. 'update' => 1,
  540. 'create_organism' => 0,
  541. 'create_target' => 0,
  542. // regexps for mRNA and protein.
  543. 're_mrna' => NULL,
  544. 're_protein' => NULL,
  545. // optional
  546. 'target_organism_id' => NULL,
  547. 'target_type' => NULL,
  548. 'start_line' => NULL,
  549. 'landmark_type' => NULL,
  550. 'alt_id_attr' => NULL,
  551. ];
  552. $this->loadLandmarks($analysis, $organism);
  553. $hasException = false;
  554. try {
  555. $this->runGFFLoader($run_args, $gff_file);
  556. }
  557. catch (\Exception $ex) {
  558. $hasException = true;
  559. }
  560. // An exception should have been thrown since the phase number is invalid
  561. $this->assertEquals($hasException, true);
  562. }
  563. /**
  564. * Run the GFF loader on gff_phase_invalid_character.gff for testing.
  565. *
  566. * This tests whether the GFF loader interprets the phase values correctly
  567. * for CDS rows when a character outside of the range 0,1,2 is specified.
  568. */
  569. public function testGFFImporterInvalidPhaseCharacterTest() {
  570. $gff_file = ['file_local' => __DIR__ . '/../data/gff_phase_invalid_character.gff'];
  571. $analysis = factory('chado.analysis')->create();
  572. $organism = factory('chado.organism')->create();
  573. $run_args = [
  574. 'analysis_id' => $analysis->analysis_id,
  575. 'organism_id' => $organism->organism_id,
  576. 'use_transaction' => 1,
  577. 'add_only' => 0,
  578. 'update' => 1,
  579. 'create_organism' => 0,
  580. 'create_target' => 0,
  581. // regexps for mRNA and protein.
  582. 're_mrna' => NULL,
  583. 're_protein' => NULL,
  584. // optional
  585. 'target_organism_id' => NULL,
  586. 'target_type' => NULL,
  587. 'start_line' => NULL,
  588. 'landmark_type' => NULL,
  589. 'alt_id_attr' => NULL,
  590. ];
  591. $this->loadLandmarks($analysis, $organism);
  592. $hasException = false;
  593. try {
  594. $this->runGFFLoader($run_args, $gff_file);
  595. }
  596. catch (\Exception $ex) {
  597. $hasException = true;
  598. }
  599. // An exception should have been thrown since the phase number is invalid
  600. $this->assertEquals($hasException, true);
  601. }
  602. /**
  603. * Run the GFF loader on small_gene.gff for testing.
  604. *
  605. * This gff has many attributes that we would like to test in the
  606. * testGFFImporterAttribute*() methods.
  607. */
  608. private function initGFFImporterAttributes() {
  609. $gff = ['file_local' => __DIR__ . '/../data/small_gene.gff'];
  610. $fasta = ['file_local' => __DIR__ . '/../data/short_scaffold.fasta'];
  611. $analysis = factory('chado.analysis')->create();
  612. $organism = factory('chado.organism')->create();
  613. $run_args = [
  614. 'analysis_id' => $analysis->analysis_id,
  615. 'organism_id' => $organism->organism_id,
  616. 'use_transaction' => 1,
  617. 'add_only' => 0,
  618. 'update' => 1,
  619. 'create_organism' => 0,
  620. 'create_target' => 0,
  621. ///regexps for mRNA and protein.
  622. 're_mrna' => NULL,
  623. 're_protein' => NULL,
  624. //optional
  625. 'target_organism_id' => $organism->organism_id,
  626. 'target_type' => NULL,
  627. 'start_line' => NULL,
  628. 'landmark_type' => NULL,
  629. 'alt_id_attr' => NULL,
  630. ];
  631. $this->loadLandmarks($analysis, $organism, $fasta);
  632. $this->runGFFLoader($run_args, $gff);
  633. $this->organism = $organism;
  634. $this->analysis = $analysis;
  635. $this->gene_cvt = chado_get_cvterm(array(
  636. 'name' => 'gene',
  637. 'cv_id' => array(
  638. 'name' => 'sequence',
  639. ),
  640. ))->cvterm_id;
  641. $this->mrna_cvt = chado_get_cvterm(array(
  642. 'name' => 'mRNA',
  643. 'cv_id' => array(
  644. 'name' => 'sequence',
  645. ),
  646. ))->cvterm_id;
  647. $this->supercontig_cvt = chado_get_cvterm(array(
  648. 'name' => 'supercontig',
  649. 'cv_id' => array(
  650. 'name' => 'sequence',
  651. ),
  652. ))->cvterm_id;
  653. $this->gene_1_uname = 'test_gene_001';
  654. $this->gene_2_uname = 'test_gene_002';
  655. $this->scaffold_1_uname = 'scaffold1';
  656. }
  657. /**
  658. * Ensures that the feature record is loaded correctly into chado.
  659. *
  660. * @group gff
  661. */
  662. public function testGFFImporterAttributeFeature() {
  663. $this->initGFFImporterAttributes();
  664. $organism = $this->organism;
  665. $query = db_select('chado.feature', 'f')
  666. ->fields('f')
  667. ->condition('uniquename', $this->gene_1_uname)
  668. ->condition('type_id', $this->gene_cvt)
  669. ->execute();
  670. $gene_1 = $query->fetchObject();
  671. $this->assertEquals('test_gene_001', $gene_1->uniquename);
  672. $this->assertEquals('test_gene_001', $gene_1->name);
  673. $this->assertEquals($organism->organism_id, $gene_1->organism_id);
  674. $this->assertEquals($this->gene_cvt, $gene_1->type_id);
  675. }
  676. /**
  677. * Ensures the feature alias is loaded correctly into chado.
  678. *
  679. * @group gff
  680. */
  681. public function testGFFImporterAttributeAlias() {
  682. $this->initGFFImporterAttributes();
  683. $alias = 'first_test_gene';
  684. $gene_1 = db_select('chado.feature', 'f')
  685. ->fields('f')
  686. ->condition('uniquename', $this->gene_1_uname)
  687. ->condition('type_id', $this->gene_cvt)
  688. ->execute()->fetchObject();
  689. $query = db_select('chado.feature_synonym', 'fs');
  690. $query->join('chado.synonym', 's', 's.synonym_id = fs.synonym_id');
  691. $query->fields('s');
  692. $query->condition('fs.feature_id', $gene_1->feature_id);
  693. $query = $query->execute();
  694. $result = $query->fetchObject();
  695. $this->assertEquals($alias, $result->name);
  696. }
  697. /**
  698. * Ensures that the dbxref records are loaded correctly into chado.
  699. *
  700. * @group gff
  701. */
  702. public function testGFFImporterAttributeDbxref() {
  703. $this->initGFFImporterAttributes();
  704. $test_db_name = 'TEST_DB';
  705. $dbx_accession = 'test_gene_dbx_001';
  706. $test_db = chado_get_db(array('name' => $test_db_name));
  707. $gff_db = chado_get_db(array('name' => 'GFF_source'));
  708. $gene_1 = db_select('chado.feature', 'f')
  709. ->fields('f')
  710. ->condition('uniquename', $this->gene_1_uname)
  711. ->condition('type_id', $this->gene_cvt)
  712. ->execute()->fetchObject();
  713. $dbx_query = db_select('chado.feature_dbxref', 'fdbx');
  714. $dbx_query->join('chado.dbxref', 'dbx', 'dbx.dbxref_id = fdbx.dbxref_id');
  715. $dbx_query->fields('dbx');
  716. $dbx_query->condition('fdbx.feature_id', $gene_1->feature_id);
  717. $gff_query = clone $dbx_query;
  718. $dbx_query->condition('dbx.db_id', $test_db->db_id);
  719. $dbx_query = $dbx_query->execute();
  720. $gff_query->condition('dbx.db_id', $gff_db->db_id);
  721. $gff_query = $gff_query->execute();
  722. $dbxref = $dbx_query->fetchObject();
  723. $gff_dbxref = $gff_query->fetchObject();
  724. $this->assertEquals($dbx_accession, $dbxref->accession);
  725. $this->assertEquals($this->gene_1_uname, $gff_dbxref->accession);
  726. }
  727. /**
  728. * Ensures ontology term records loaded correctly into chado.
  729. *
  730. * @group gff
  731. */
  732. public function testGFFImporterAttributeOntology() {
  733. $this->initGFFImporterAttributes();
  734. $ontology_db = 'SO';
  735. $ontology_accession = '0000704';
  736. $gene_1 = db_select('chado.feature', 'f')
  737. ->fields('f')
  738. ->condition('uniquename', $this->gene_1_uname)
  739. ->condition('type_id', $this->gene_cvt)
  740. ->execute()->fetchObject();
  741. $term = chado_get_cvterm(array(
  742. 'dbxref_id' => array(
  743. 'accession' => $ontology_accession,
  744. 'db_id' => array(
  745. 'name' => $ontology_db,
  746. ),
  747. ),
  748. ));
  749. $feature_cvt = db_select('chado.feature_cvterm', 'fcvt')
  750. ->fields('fcvt')
  751. ->condition('cvterm_id', $term->cvterm_id)
  752. ->condition('feature_id', $gene_1->feature_id)
  753. ->execute();
  754. $this->assertEquals(1, $feature_cvt->rowCount());
  755. }
  756. /**
  757. * Ensures feature parent record loaded correctly into chado.
  758. *
  759. * @group gff
  760. */
  761. public function testGFFImporterAttributeParent() {
  762. $this->initGFFImporterAttributes();
  763. $mrna_uname = 'test_mrna_001.1';
  764. $rel_cvt = chado_get_cvterm(array(
  765. 'name' => 'part_of',
  766. 'cv_id' => array(
  767. 'name' => 'sequence',
  768. ),
  769. ))->cvterm_id;
  770. $mrna = db_select('chado.feature', 'f')
  771. ->fields('f')
  772. ->condition('uniquename', $mrna_uname)
  773. ->condition('type_id', $this->mrna_cvt)
  774. ->execute()->fetchObject();
  775. $query = db_select('chado.feature_relationship', 'fr');
  776. $query->join('chado.feature', 'f', 'f.feature_id = fr.object_id');
  777. $query->fields('f');
  778. $query->condition('fr.subject_id', $mrna->feature_id);
  779. $query->condition('fr.type_id', $rel_cvt);
  780. $query = $query->execute();
  781. $parent = $query->fetchObject();
  782. $this->assertEquals('test_gene_001', $parent->uniquename);
  783. $this->assertEquals('test_gene_001', $parent->name);
  784. $this->assertEquals($this->gene_cvt, $parent->type_id);
  785. $this->assertEquals($this->organism->organism_id, $parent->organism_id);
  786. }
  787. /**
  788. * Ensure target record loaded correctly into chado.
  789. *
  790. * @group gff
  791. */
  792. public function testGFFImporterAttributeTarget() {
  793. $this->initGFFImporterAttributes();
  794. $target_feature = 'scaffold1';
  795. $start = 99;
  796. $end = 200;
  797. $target_type = 'supercontig';
  798. $target_cvt = chado_get_cvterm(array(
  799. 'name' => $target_type,
  800. 'cv_id' => array(
  801. 'name' => 'sequence',
  802. ),
  803. ))->cvterm_id;
  804. $source_feature = db_select('chado.feature', 'f')
  805. ->fields('f')
  806. ->condition('uniquename', $target_feature)
  807. ->condition('type_id', $target_cvt)
  808. ->execute()->fetchObject();
  809. $gene_1 = db_select('chado.feature', 'f')
  810. ->fields('f')
  811. ->condition('uniquename', $this->gene_1_uname)
  812. ->condition('type_id', $this->gene_cvt)
  813. ->execute()->fetchObject();
  814. $featureloc = db_select('chado.featureloc', 'fl')
  815. ->fields('fl')
  816. ->condition('fl.feature_id', $gene_1->feature_id)
  817. ->condition('fl.srcfeature_id', $source_feature->feature_id)
  818. ->execute()->fetchObject();
  819. $this->assertEquals($start, $featureloc->fmin);
  820. $this->assertEquals($end, $featureloc->fmax);
  821. }
  822. /**
  823. * Ensure properties loaded correctly into chado.
  824. *
  825. * @group gff
  826. */
  827. public function testGFFImporterAttributeProperty() {
  828. $this->initGFFImporterAttributes();
  829. $gap_1 = 'test_gap_1';
  830. $gap_2 = 'test_gap_2';
  831. $note_val = 'test_gene_001_note';
  832. $gene_1 = db_select('chado.feature', 'f')
  833. ->fields('f')
  834. ->condition('uniquename', $this->gene_1_uname)
  835. ->condition('type_id', $this->gene_cvt)
  836. ->execute()->fetchObject();
  837. $gap_cvt = chado_get_cvterm(array(
  838. 'name' => 'Gap',
  839. 'cv_id' => array(
  840. 'name' => 'feature_property',
  841. ),
  842. ))->cvterm_id;
  843. $note_cvt = chado_get_cvterm(array(
  844. 'name' => 'Note',
  845. 'cv_id' => array(
  846. 'name' => 'feature_property',
  847. ),
  848. ))->cvterm_id;
  849. // Assert gaps loaded correctly
  850. $gaps_query = db_select('chado.featureprop', 'fp')
  851. ->fields('fp')
  852. ->condition('feature_id', $gene_1->feature_id)
  853. ->condition('type_id', $gap_cvt)
  854. ->execute();
  855. while (($gap = $gaps_query->fetchObject())) {
  856. $gaps[$gap->value] = $gap;
  857. }
  858. $this->assertEquals($gap_1, $gaps[$gap_1]->value);
  859. $this->assertEquals(0, $gaps[$gap_1]->rank);
  860. // Assert note loaded correctly
  861. $note = db_select('chado.featureprop', 'fp')
  862. ->fields('fp')
  863. ->condition('feature_id', $gene_1->feature_id)
  864. ->condition('type_id', $note_cvt)
  865. ->execute()->fetchObject();
  866. $this->assertEquals($note_val, $note->value);
  867. $this->assertEquals(0, $note->rank);
  868. }
  869. /**
  870. * Ensure derives from information loaded correctly into chado.
  871. *
  872. * @group gff
  873. */
  874. public function testGFFImporterAttributeDerivesFrom() {
  875. $this->initGFFImporterAttributes();
  876. $gene_2 = db_select('chado.feature', 'f')
  877. ->fields('f')
  878. ->condition('uniquename', $this->gene_2_uname)
  879. ->condition('type_id', $this->gene_cvt)
  880. ->execute()->fetchObject();
  881. $derivesfrom_cvt = chado_get_cvterm(array(
  882. 'name' => 'derives_from',
  883. 'cv_id' => array(
  884. 'name' => 'sequence',
  885. ),
  886. ))->cvterm_id;
  887. $query = db_select('chado.feature', 'f');
  888. $query->join('chado.feature_relationship', 'fr', 'f.feature_id = fr.object_id');
  889. $query->fields('f');
  890. $query->condition('fr.subject_id', $gene_2->feature_id);
  891. $query->condition('fr.type_id', $derivesfrom_cvt);
  892. $query = $query->execute();
  893. $derivesfrom_feature = $query->fetchObject();
  894. $this->assertEquals($this->gene_1_uname, $derivesfrom_feature->uniquename);
  895. $this->assertEquals($this->gene_1_uname, $derivesfrom_feature->name);
  896. $this->assertEquals($this->gene_cvt, $derivesfrom_feature->type_id);
  897. }
  898. /**
  899. * Ensure FASTA information loaded correctly into chado.
  900. *
  901. * @group gff
  902. */
  903. public function testGFFImporterAttributeFastas() {
  904. $this->initGFFImporterAttributes();
  905. $scaffold = db_select('chado.feature', 'f')
  906. ->fields('f')
  907. ->condition('uniquename', $this->scaffold_1_uname)
  908. ->condition('type_id', $this->supercontig_cvt)
  909. ->execute()->fetchObject();
  910. $this->assertEquals(720, $scaffold->seqlen);
  911. $this->assertEquals(720, strlen($scaffold->residues));
  912. $this->assertEquals('83578d8afdaec399c682aa6c0ddd29c9', $scaffold->md5checksum);
  913. }
  914. /**
  915. * Test that when checked, explicit proteins are created when specified within
  916. * the GFF file. Explicit proteins will not respect the skip_protein argument
  917. * and will therefore be added to the database.
  918. *
  919. * @group gff
  920. * @ticket 77
  921. *
  922. */
  923. public function testGFFPolypeptide() {
  924. $gff_file = ['file_local' => __DIR__ . '/../data/simpleGFF.gff'];
  925. $analysis = factory('chado.analysis')->create();
  926. $organism = factory('chado.organism')->create();
  927. $run_args = [
  928. // The new argument
  929. 'skip_protein' => 1,
  930. 'analysis_id' => $analysis->analysis_id,
  931. 'organism_id' => $organism->organism_id,
  932. 'use_transaction' => 1,
  933. 'add_only' => 0,
  934. 'update' => 1,
  935. 'create_organism' => 0,
  936. 'create_target' => 0,
  937. // regexps for mRNA and protein.
  938. 're_mrna' => NULL,
  939. 're_protein' => NULL,
  940. // optional
  941. 'target_organism_id' => NULL,
  942. 'target_type' => NULL,
  943. 'start_line' => NULL,
  944. 'landmark_type' => NULL,
  945. 'alt_id_attr' => NULL,
  946. ];
  947. $this->loadLandmarks($analysis, $organism);
  948. $this->runGFFLoader($run_args, $gff_file);
  949. $identifier = [
  950. 'cv_id' => ['name' => 'sequence'],
  951. 'name' => 'polypeptide',
  952. ];
  953. $protein_type_id = tripal_get_cvterm($identifier);
  954. $name = 'FRAEX38873_v2_000000010.1.3_test_protein';
  955. $query = db_select('chado.feature', 'f')
  956. ->fields('f', ['uniquename'])
  957. ->condition('f.uniquename', $name)
  958. ->condition('f.type_id', $protein_type_id->cvterm_id)
  959. ->execute()
  960. ->fetchAll();
  961. $this->assertEquals(1, count($query));
  962. }
  963. /**
  964. * Add a skip protein option. Test that when checked, implicit proteins are
  965. * not created, but that they are created when unchecked.
  966. *
  967. * @group gff
  968. * @ticket 77
  969. *
  970. */
  971. public function testGFFNoProteinOption() {
  972. $gff_file = ['file_local' => __DIR__ . '/../data/gff_protein_generation.gff'];
  973. $analysis = factory('chado.analysis')->create();
  974. $organism = factory('chado.organism')->create();
  975. $run_args = [
  976. //Skip protein feature generation
  977. 'skip_protein' => 1,
  978. 'analysis_id' => $analysis->analysis_id,
  979. 'organism_id' => $organism->organism_id,
  980. 'use_transaction' => 1,
  981. 'add_only' => 0,
  982. 'update' => 1,
  983. 'create_organism' => 0,
  984. 'create_target' => 0,
  985. ///regexps for mRNA and protein.
  986. 're_mrna' => NULL,
  987. 're_protein' => NULL,
  988. //optional
  989. 'target_organism_id' => NULL,
  990. 'target_type' => NULL,
  991. 'start_line' => NULL,
  992. 'landmark_type' => NULL,
  993. 'alt_id_attr' => NULL,
  994. ];
  995. $this->loadLandmarks($analysis, $organism);
  996. $this->runGFFLoader($run_args, $gff_file);
  997. $identifier = [
  998. 'cv_id' => ['name' => 'sequence'],
  999. 'name' => 'polypeptide',
  1000. ];
  1001. $protein_type_id = tripal_get_cvterm($identifier);
  1002. $name = "FRAEX38873_v2_000000190.1-protein";
  1003. $results = db_select('chado.feature', 'f')
  1004. ->fields('f', ['uniquename'])
  1005. ->condition('f.uniquename', $name)
  1006. ->condition('f.type_id', $protein_type_id->cvterm_id)
  1007. ->execute()
  1008. ->fetchAll();
  1009. // There should be no proteins since we used the skip_proteins flag and no
  1010. // explicit proteins were specified in the test file
  1011. $this->assertEquals(0, count($results));
  1012. // Now perform a unit test where we do not skip proteins generation
  1013. $run_args['skip_protein'] = 0;
  1014. $this->runGFFLoader($run_args, $gff_file);
  1015. $query = db_select('chado.feature', 'f')
  1016. ->fields('f', ['uniquename'])
  1017. ->condition('f.uniquename', $name)
  1018. ->condition('f.type_id', $protein_type_id->cvterm_id)
  1019. ->execute()
  1020. ->fetchObject();
  1021. $this->assertEquals($name, $query->uniquename);
  1022. }
  1023. /**
  1024. * The GFF importer should still create explicitly defined proteins if
  1025. * skip_protein is true.
  1026. *
  1027. * @group gff
  1028. * @ticket 77
  1029. */
  1030. public function testGFFImporterLoadsExplicitProteins() {
  1031. $gff_file = ['file_local' => __DIR__ . '/../data/simpleGFF.gff'];
  1032. $analysis = factory('chado.analysis')->create();
  1033. $organism = factory('chado.organism')->create();
  1034. $run_args = [
  1035. //The new argument
  1036. 'skip_protein' => 1,
  1037. ///
  1038. 'analysis_id' => $analysis->analysis_id,
  1039. 'organism_id' => $organism->organism_id,
  1040. 'use_transaction' => 1,
  1041. 'add_only' => 0,
  1042. 'update' => 1,
  1043. 'create_organism' => 0,
  1044. 'create_target' => 0,
  1045. ///regexps for mRNA and protein.
  1046. 're_mrna' => NULL,
  1047. 're_protein' => NULL,
  1048. //optional
  1049. 'target_organism_id' => NULL,
  1050. 'target_type' => NULL,
  1051. 'start_line' => NULL,
  1052. 'landmark_type' => NULL,
  1053. 'alt_id_attr' => NULL,
  1054. ];
  1055. $this->loadLandmarks($analysis, $organism);
  1056. $this->runGFFLoader($run_args, $gff_file);
  1057. $name = 'FRAEX38873_v2_000000010.1.3_test_protein';
  1058. $query = db_select('chado.feature', 'f')
  1059. ->fields('f', ['uniquename'])
  1060. ->condition('f.uniquename', $name)
  1061. ->execute()
  1062. ->fetchField();
  1063. $this->assertEquals($name, $query);
  1064. }
  1065. private function runGFFLoader($run_args, $file) {
  1066. // silent(function ($run_args, $file) {
  1067. module_load_include('inc', 'tripal_chado', 'includes/TripalImporter/GFF3Importer');
  1068. $importer = new \GFF3Importer();
  1069. $importer->create($run_args, $file);
  1070. $importer->prepareFiles();
  1071. $importer->run();
  1072. // });
  1073. }
  1074. private function loadLandmarks($analysis, $organism, $landmark_file = array()) {
  1075. if (empty($landmark_file)) {
  1076. $landmark_file = ['file_local' => __DIR__ . '/../data/empty_landmarks.fasta'];
  1077. }
  1078. $run_args = [
  1079. 'organism_id' => $organism->organism_id,
  1080. 'analysis_id' => $analysis->analysis_id,
  1081. 'seqtype' => 'supercontig',
  1082. 'method' => 2, //default insert and update
  1083. 'match_type' => 1, //unique name default
  1084. //optional
  1085. 're_name' => NULL,
  1086. 're_uname' => NULL,
  1087. 're_accession' => NULL,
  1088. 'db_id' => NULL,
  1089. 'rel_type' => NULL,
  1090. 're_subject' => NULL,
  1091. 'parent_type' => NULL,
  1092. ];
  1093. module_load_include('inc', 'tripal_chado', 'includes/TripalImporter/FASTAImporter');
  1094. //silent(function ($run_args, $landmark_file) {
  1095. $importer = new \FASTAImporter();
  1096. $importer->create($run_args, $landmark_file);
  1097. $importer->prepareFiles();
  1098. $importer->run();
  1099. // });
  1100. }
  1101. }