GFF3ImporterTest.php 34 KB

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