GFF3ImporterTest.php 35 KB

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