GFF3ImporterTest.php 36 KB

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