GFF3ImporterTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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 small_gene.gff for testing.
  373. *
  374. * This gff has many attributes that we would like to test in the
  375. * testGFFImporterAttribute*() methods.
  376. */
  377. private function initGFFImporterAttributes() {
  378. $gff = ['file_local' => __DIR__ . '/../data/small_gene.gff'];
  379. $fasta = ['file_local' => __DIR__ . '/../data/short_scaffold.fasta'];
  380. $analysis = factory('chado.analysis')->create();
  381. $organism = factory('chado.organism')->create();
  382. $run_args = [
  383. 'analysis_id' => $analysis->analysis_id,
  384. 'organism_id' => $organism->organism_id,
  385. 'use_transaction' => 1,
  386. 'add_only' => 0,
  387. 'update' => 1,
  388. 'create_organism' => 0,
  389. 'create_target' => 0,
  390. ///regexps for mRNA and protein.
  391. 're_mrna' => NULL,
  392. 're_protein' => NULL,
  393. //optional
  394. 'target_organism_id' => $organism->organism_id,
  395. 'target_type' => NULL,
  396. 'start_line' => NULL,
  397. 'landmark_type' => NULL,
  398. 'alt_id_attr' => NULL,
  399. ];
  400. $this->loadLandmarks($analysis, $organism, $fasta);
  401. $this->runGFFLoader($run_args, $gff);
  402. $this->organism = $organism;
  403. $this->analysis = $analysis;
  404. $this->gene_cvt = chado_get_cvterm(array(
  405. 'name' => 'gene',
  406. 'cv_id' => array(
  407. 'name' => 'sequence',
  408. ),
  409. ))->cvterm_id;
  410. $this->mrna_cvt = chado_get_cvterm(array(
  411. 'name' => 'mRNA',
  412. 'cv_id' => array(
  413. 'name' => 'sequence',
  414. ),
  415. ))->cvterm_id;
  416. $this->supercontig_cvt = chado_get_cvterm(array(
  417. 'name' => 'supercontig',
  418. 'cv_id' => array(
  419. 'name' => 'sequence',
  420. ),
  421. ))->cvterm_id;
  422. $this->gene_1_uname = 'test_gene_001';
  423. $this->gene_2_uname = 'test_gene_002';
  424. $this->scaffold_1_uname = 'scaffold1';
  425. }
  426. /**
  427. * Ensures that the feature record is loaded correctly into chado.
  428. *
  429. * @group gff
  430. */
  431. public function testGFFImporterAttributeFeature() {
  432. $this->initGFFImporterAttributes();
  433. $organism = $this->organism;
  434. $query = db_select('chado.feature', 'f')
  435. ->fields('f')
  436. ->condition('uniquename', $this->gene_1_uname)
  437. ->condition('type_id', $this->gene_cvt)
  438. ->execute();
  439. $gene_1 = $query->fetchObject();
  440. $this->assertEquals('test_gene_001', $gene_1->uniquename);
  441. $this->assertEquals('test_gene_001', $gene_1->name);
  442. $this->assertEquals($organism->organism_id, $gene_1->organism_id);
  443. $this->assertEquals($this->gene_cvt, $gene_1->type_id);
  444. }
  445. /**
  446. * Ensures the feature alias is loaded correctly into chado.
  447. *
  448. * @group gff
  449. */
  450. public function testGFFImporterAttributeAlias() {
  451. $this->initGFFImporterAttributes();
  452. $alias = 'first_test_gene';
  453. $gene_1 = db_select('chado.feature', 'f')
  454. ->fields('f')
  455. ->condition('uniquename', $this->gene_1_uname)
  456. ->condition('type_id', $this->gene_cvt)
  457. ->execute()->fetchObject();
  458. $query = db_select('chado.feature_synonym', 'fs');
  459. $query->join('chado.synonym', 's', 's.synonym_id = fs.synonym_id');
  460. $query->fields('s');
  461. $query->condition('fs.feature_id', $gene_1->feature_id);
  462. $query = $query->execute();
  463. $result = $query->fetchObject();
  464. $this->assertEquals($alias, $result->name);
  465. }
  466. /**
  467. * Ensures that the dbxref records are loaded correctly into chado.
  468. *
  469. * @group gff
  470. */
  471. public function testGFFImporterAttributeDbxref() {
  472. $this->initGFFImporterAttributes();
  473. $test_db_name = 'TEST_DB';
  474. $dbx_accession = 'test_gene_dbx_001';
  475. $test_db = chado_get_db(array('name' => $test_db_name));
  476. $gff_db = chado_get_db(array('name' => 'GFF_source'));
  477. $gene_1 = db_select('chado.feature', 'f')
  478. ->fields('f')
  479. ->condition('uniquename', $this->gene_1_uname)
  480. ->condition('type_id', $this->gene_cvt)
  481. ->execute()->fetchObject();
  482. $dbx_query = db_select('chado.feature_dbxref', 'fdbx');
  483. $dbx_query->join('chado.dbxref', 'dbx', 'dbx.dbxref_id = fdbx.dbxref_id');
  484. $dbx_query->fields('dbx');
  485. $dbx_query->condition('fdbx.feature_id', $gene_1->feature_id);
  486. $gff_query = clone $dbx_query;
  487. $dbx_query->condition('dbx.db_id', $test_db->db_id);
  488. $dbx_query = $dbx_query->execute();
  489. $gff_query->condition('dbx.db_id', $gff_db->db_id);
  490. $gff_query = $gff_query->execute();
  491. $dbxref = $dbx_query->fetchObject();
  492. $gff_dbxref = $gff_query->fetchObject();
  493. $this->assertEquals($dbx_accession, $dbxref->accession);
  494. $this->assertEquals($this->gene_1_uname, $gff_dbxref->accession);
  495. }
  496. /**
  497. * Ensures ontology term records loaded correctly into chado.
  498. *
  499. * @group gff
  500. */
  501. public function testGFFImporterAttributeOntology() {
  502. $this->initGFFImporterAttributes();
  503. $ontology_db = 'SO';
  504. $ontology_accession = '0000704';
  505. $gene_1 = db_select('chado.feature', 'f')
  506. ->fields('f')
  507. ->condition('uniquename', $this->gene_1_uname)
  508. ->condition('type_id', $this->gene_cvt)
  509. ->execute()->fetchObject();
  510. $term = chado_get_cvterm(array(
  511. 'dbxref_id' => array(
  512. 'accession' => $ontology_accession,
  513. 'db_id' => array(
  514. 'name' => $ontology_db,
  515. ),
  516. ),
  517. ));
  518. $feature_cvt = db_select('chado.feature_cvterm', 'fcvt')
  519. ->fields('fcvt')
  520. ->condition('cvterm_id', $term->cvterm_id)
  521. ->condition('feature_id', $gene_1->feature_id)
  522. ->execute();
  523. $this->assertEquals(1, $feature_cvt->rowCount());
  524. }
  525. /**
  526. * Ensures feature parent record loaded correctly into chado.
  527. *
  528. * @group gff
  529. */
  530. public function testGFFImporterAttributeParent() {
  531. $this->initGFFImporterAttributes();
  532. $mrna_uname = 'test_mrna_001.1';
  533. $rel_cvt = chado_get_cvterm(array(
  534. 'name' => 'part_of',
  535. 'cv_id' => array(
  536. 'name' => 'sequence',
  537. ),
  538. ))->cvterm_id;
  539. $mrna = db_select('chado.feature', 'f')
  540. ->fields('f')
  541. ->condition('uniquename', $mrna_uname)
  542. ->condition('type_id', $this->mrna_cvt)
  543. ->execute()->fetchObject();
  544. $query = db_select('chado.feature_relationship', 'fr');
  545. $query->join('chado.feature', 'f', 'f.feature_id = fr.object_id');
  546. $query->fields('f');
  547. $query->condition('fr.subject_id', $mrna->feature_id);
  548. $query->condition('fr.type_id', $rel_cvt);
  549. $query = $query->execute();
  550. $parent = $query->fetchObject();
  551. $this->assertEquals('test_gene_001', $parent->uniquename);
  552. $this->assertEquals('test_gene_001', $parent->name);
  553. $this->assertEquals($this->gene_cvt, $parent->type_id);
  554. $this->assertEquals($this->organism->organism_id, $parent->organism_id);
  555. }
  556. /**
  557. * Ensure target record loaded correctly into chado.
  558. *
  559. * @group gff
  560. */
  561. public function testGFFImporterAttributeTarget() {
  562. $this->initGFFImporterAttributes();
  563. $target_feature = 'scaffold1';
  564. $start = 99;
  565. $end = 200;
  566. $target_type = 'supercontig';
  567. $target_cvt = chado_get_cvterm(array(
  568. 'name' => $target_type,
  569. 'cv_id' => array(
  570. 'name' => 'sequence',
  571. ),
  572. ))->cvterm_id;
  573. $source_feature = db_select('chado.feature', 'f')
  574. ->fields('f')
  575. ->condition('uniquename', $target_feature)
  576. ->condition('type_id', $target_cvt)
  577. ->execute()->fetchObject();
  578. $gene_1 = db_select('chado.feature', 'f')
  579. ->fields('f')
  580. ->condition('uniquename', $this->gene_1_uname)
  581. ->condition('type_id', $this->gene_cvt)
  582. ->execute()->fetchObject();
  583. $featureloc = db_select('chado.featureloc', 'fl')
  584. ->fields('fl')
  585. ->condition('fl.feature_id', $gene_1->feature_id)
  586. ->condition('fl.srcfeature_id', $source_feature->feature_id)
  587. ->execute()->fetchObject();
  588. $this->assertEquals($start, $featureloc->fmin);
  589. $this->assertEquals($end, $featureloc->fmax);
  590. }
  591. /**
  592. * Ensure properties loaded correctly into chado.
  593. *
  594. * @group gff
  595. */
  596. public function testGFFImporterAttributeProperty() {
  597. $this->initGFFImporterAttributes();
  598. $gap_1 = 'test_gap_1';
  599. $gap_2 = 'test_gap_2';
  600. $note_val = 'test_gene_001_note';
  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. $gap_cvt = chado_get_cvterm(array(
  607. 'name' => 'Gap',
  608. 'cv_id' => array(
  609. 'name' => 'feature_property',
  610. ),
  611. ))->cvterm_id;
  612. $note_cvt = chado_get_cvterm(array(
  613. 'name' => 'Note',
  614. 'cv_id' => array(
  615. 'name' => 'feature_property',
  616. ),
  617. ))->cvterm_id;
  618. // Assert gaps loaded correctly
  619. $gaps_query = db_select('chado.featureprop', 'fp')
  620. ->fields('fp')
  621. ->condition('feature_id', $gene_1->feature_id)
  622. ->condition('type_id', $gap_cvt)
  623. ->execute();
  624. while (($gap = $gaps_query->fetchObject())) {
  625. $gaps[$gap->value] = $gap;
  626. }
  627. $this->assertEquals($gap_1, $gaps[$gap_1]->value);
  628. $this->assertEquals(0, $gaps[$gap_1]->rank);
  629. // Assert note loaded correctly
  630. $note = db_select('chado.featureprop', 'fp')
  631. ->fields('fp')
  632. ->condition('feature_id', $gene_1->feature_id)
  633. ->condition('type_id', $note_cvt)
  634. ->execute()->fetchObject();
  635. $this->assertEquals($note_val, $note->value);
  636. $this->assertEquals(0, $note->rank);
  637. }
  638. /**
  639. * Ensure derives from information loaded correctly into chado.
  640. *
  641. * @group gff
  642. */
  643. public function testGFFImporterAttributeDerivesFrom() {
  644. $this->initGFFImporterAttributes();
  645. $gene_2 = db_select('chado.feature', 'f')
  646. ->fields('f')
  647. ->condition('uniquename', $this->gene_2_uname)
  648. ->condition('type_id', $this->gene_cvt)
  649. ->execute()->fetchObject();
  650. $derivesfrom_cvt = chado_get_cvterm(array(
  651. 'name' => 'derives_from',
  652. 'cv_id' => array(
  653. 'name' => 'sequence',
  654. ),
  655. ))->cvterm_id;
  656. $query = db_select('chado.feature', 'f');
  657. $query->join('chado.feature_relationship', 'fr', 'f.feature_id = fr.object_id');
  658. $query->fields('f');
  659. $query->condition('fr.subject_id', $gene_2->feature_id);
  660. $query->condition('fr.type_id', $derivesfrom_cvt);
  661. $query = $query->execute();
  662. $derivesfrom_feature = $query->fetchObject();
  663. $this->assertEquals($this->gene_1_uname, $derivesfrom_feature->uniquename);
  664. $this->assertEquals($this->gene_1_uname, $derivesfrom_feature->name);
  665. $this->assertEquals($this->gene_cvt, $derivesfrom_feature->type_id);
  666. }
  667. /**
  668. * Ensure FASTA information loaded correctly into chado.
  669. *
  670. * @group gff
  671. */
  672. public function testGFFImporterAttributeFastas() {
  673. $this->initGFFImporterAttributes();
  674. $scaffold = db_select('chado.feature', 'f')
  675. ->fields('f')
  676. ->condition('uniquename', $this->scaffold_1_uname)
  677. ->condition('type_id', $this->supercontig_cvt)
  678. ->execute()->fetchObject();
  679. $this->assertEquals(720, $scaffold->seqlen);
  680. $this->assertEquals(720, strlen($scaffold->residues));
  681. $this->assertEquals('83578d8afdaec399c682aa6c0ddd29c9', $scaffold->md5checksum);
  682. }
  683. /**
  684. * Test that when checked, explicit proteins are created when specified within
  685. * the GFF file. Explicit proteins will not respect the skip_protein argument
  686. * and will therefore be added to the database.
  687. *
  688. * @group gff
  689. * @ticket 77
  690. *
  691. */
  692. public function testGFFPolypeptide() {
  693. $gff_file = ['file_local' => __DIR__ . '/../data/simpleGFF.gff'];
  694. $analysis = factory('chado.analysis')->create();
  695. $organism = factory('chado.organism')->create();
  696. $run_args = [
  697. // The new argument
  698. 'skip_protein' => 1,
  699. 'analysis_id' => $analysis->analysis_id,
  700. 'organism_id' => $organism->organism_id,
  701. 'use_transaction' => 1,
  702. 'add_only' => 0,
  703. 'update' => 1,
  704. 'create_organism' => 0,
  705. 'create_target' => 0,
  706. // regexps for mRNA and protein.
  707. 're_mrna' => NULL,
  708. 're_protein' => NULL,
  709. // optional
  710. 'target_organism_id' => NULL,
  711. 'target_type' => NULL,
  712. 'start_line' => NULL,
  713. 'landmark_type' => NULL,
  714. 'alt_id_attr' => NULL,
  715. ];
  716. $this->loadLandmarks($analysis, $organism);
  717. $this->runGFFLoader($run_args, $gff_file);
  718. $identifier = [
  719. 'cv_id' => ['name' => 'sequence'],
  720. 'name' => 'polypeptide',
  721. ];
  722. $protein_type_id = tripal_get_cvterm($identifier);
  723. $name = 'FRAEX38873_v2_000000010.1.3_test_protein';
  724. $query = db_select('chado.feature', 'f')
  725. ->fields('f', ['uniquename'])
  726. ->condition('f.uniquename', $name)
  727. ->condition('f.type_id', $protein_type_id->cvterm_id)
  728. ->execute()
  729. ->fetchAll();
  730. $this->assertEquals(1, count($query));
  731. }
  732. /**
  733. * Add a skip protein option. Test that when checked, implicit proteins are
  734. * not created, but that they are created when unchecked.
  735. *
  736. * @group gff
  737. * @ticket 77
  738. *
  739. */
  740. public function testGFFNoProteinOption() {
  741. $gff_file = ['file_local' => __DIR__ . '/../data/gff_protein_generation.gff'];
  742. $analysis = factory('chado.analysis')->create();
  743. $organism = factory('chado.organism')->create();
  744. $run_args = [
  745. //Skip protein feature generation
  746. 'skip_protein' => 1,
  747. 'analysis_id' => $analysis->analysis_id,
  748. 'organism_id' => $organism->organism_id,
  749. 'use_transaction' => 1,
  750. 'add_only' => 0,
  751. 'update' => 1,
  752. 'create_organism' => 0,
  753. 'create_target' => 0,
  754. ///regexps for mRNA and protein.
  755. 're_mrna' => NULL,
  756. 're_protein' => NULL,
  757. //optional
  758. 'target_organism_id' => NULL,
  759. 'target_type' => NULL,
  760. 'start_line' => NULL,
  761. 'landmark_type' => NULL,
  762. 'alt_id_attr' => NULL,
  763. ];
  764. $this->loadLandmarks($analysis, $organism);
  765. $this->runGFFLoader($run_args, $gff_file);
  766. $identifier = [
  767. 'cv_id' => ['name' => 'sequence'],
  768. 'name' => 'polypeptide',
  769. ];
  770. $protein_type_id = tripal_get_cvterm($identifier);
  771. $name = "FRAEX38873_v2_000000190.1-protein";
  772. $results = db_select('chado.feature', 'f')
  773. ->fields('f', ['uniquename'])
  774. ->condition('f.uniquename', $name)
  775. ->condition('f.type_id', $protein_type_id->cvterm_id)
  776. ->execute()
  777. ->fetchAll();
  778. // There should be no proteins since we used the skip_proteins flag and no
  779. // explicit proteins were specified in the test file
  780. $this->assertEquals(0, count($results));
  781. // Now perform a unit test where we do not skip proteins generation
  782. $run_args['skip_protein'] = 0;
  783. $this->runGFFLoader($run_args, $gff_file);
  784. $query = db_select('chado.feature', 'f')
  785. ->fields('f', ['uniquename'])
  786. ->condition('f.uniquename', $name)
  787. ->condition('f.type_id', $protein_type_id->cvterm_id)
  788. ->execute()
  789. ->fetchObject();
  790. $this->assertEquals($name, $query->uniquename);
  791. }
  792. /**
  793. * The GFF importer should still create explicitly defined proteins if
  794. * skip_protein is true.
  795. *
  796. * @group gff
  797. * @ticket 77
  798. */
  799. public function testGFFImporterLoadsExplicitProteins() {
  800. $gff_file = ['file_local' => __DIR__ . '/../data/simpleGFF.gff'];
  801. $analysis = factory('chado.analysis')->create();
  802. $organism = factory('chado.organism')->create();
  803. $run_args = [
  804. //The new argument
  805. 'skip_protein' => 1,
  806. ///
  807. 'analysis_id' => $analysis->analysis_id,
  808. 'organism_id' => $organism->organism_id,
  809. 'use_transaction' => 1,
  810. 'add_only' => 0,
  811. 'update' => 1,
  812. 'create_organism' => 0,
  813. 'create_target' => 0,
  814. ///regexps for mRNA and protein.
  815. 're_mrna' => NULL,
  816. 're_protein' => NULL,
  817. //optional
  818. 'target_organism_id' => NULL,
  819. 'target_type' => NULL,
  820. 'start_line' => NULL,
  821. 'landmark_type' => NULL,
  822. 'alt_id_attr' => NULL,
  823. ];
  824. $this->loadLandmarks($analysis, $organism);
  825. $this->runGFFLoader($run_args, $gff_file);
  826. $name = 'FRAEX38873_v2_000000010.1.3_test_protein';
  827. $query = db_select('chado.feature', 'f')
  828. ->fields('f', ['uniquename'])
  829. ->condition('f.uniquename', $name)
  830. ->execute()
  831. ->fetchField();
  832. $this->assertEquals($name, $query);
  833. }
  834. private function runGFFLoader($run_args, $file) {
  835. // silent(function ($run_args, $file) {
  836. module_load_include('inc', 'tripal_chado', 'includes/TripalImporter/GFF3Importer');
  837. $importer = new \GFF3Importer();
  838. $importer->create($run_args, $file);
  839. $importer->prepareFiles();
  840. $importer->run();
  841. // });
  842. }
  843. private function loadLandmarks($analysis, $organism, $landmark_file = array()) {
  844. if (empty($landmark_file)) {
  845. $landmark_file = ['file_local' => __DIR__ . '/../data/empty_landmarks.fasta'];
  846. }
  847. $run_args = [
  848. 'organism_id' => $organism->organism_id,
  849. 'analysis_id' => $analysis->analysis_id,
  850. 'seqtype' => 'supercontig',
  851. 'method' => 2, //default insert and update
  852. 'match_type' => 1, //unique name default
  853. //optional
  854. 're_name' => NULL,
  855. 're_uname' => NULL,
  856. 're_accession' => NULL,
  857. 'db_id' => NULL,
  858. 'rel_type' => NULL,
  859. 're_subject' => NULL,
  860. 'parent_type' => NULL,
  861. ];
  862. module_load_include('inc', 'tripal_chado', 'includes/TripalImporter/FASTAImporter');
  863. //silent(function ($run_args, $landmark_file) {
  864. $importer = new \FASTAImporter();
  865. $importer->create($run_args, $landmark_file);
  866. $importer->prepareFiles();
  867. $importer->run();
  868. // });
  869. }
  870. }