TaxonomyImporter.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. <?php
  2. class TaxonomyImporter extends TripalImporter {
  3. /**
  4. * The name of this loader. This name will be presented to the site
  5. * user.
  6. */
  7. public static $name = 'Chado NCBI Taxonomy Loader';
  8. /**
  9. * The machine name for this loader. This name will be used to construct
  10. * the URL for the loader.
  11. */
  12. public static $machine_name = 'chado_taxonomy';
  13. /**
  14. * A brief description for this loader. This description will be
  15. * presented to the site user.
  16. */
  17. public static $description = 'Imports new organisms from NCBI using taxonomy IDs, or loads taxonomic details about existing organisms.';
  18. /**
  19. * An array containing the extensions of allowed file types.
  20. */
  21. public static $file_types = array();
  22. /**
  23. * Provides information to the user about the file upload. Typically this
  24. * may include a description of the file types allowed.
  25. */
  26. public static $upload_description = '';
  27. /**
  28. * The title that should appear above the upload button.
  29. */
  30. public static $upload_title = 'File Upload';
  31. /**
  32. * If the loader should require an analysis record. To maintain provenance
  33. * we should always indiate where the data we are uploading comes from.
  34. * The method that Tripal attempts to use for this by associating upload files
  35. * with an analysis record. The analysis record provides the details for
  36. * how the file was created or obtained. Set this to FALSE if the loader
  37. * should not require an analysis when loading. if $use_analysis is set to
  38. * true then the form values will have an 'analysis_id' key in the $form_state
  39. * array on submitted forms.
  40. */
  41. public static $use_analysis = FALSE;
  42. /**
  43. * If the $use_analysis value is set above then this value indicates if the
  44. * analysis should be required.
  45. */
  46. public static $require_analysis = FALSE;
  47. /**
  48. * Text that should appear on the button at the bottom of the importer
  49. * form.
  50. */
  51. public static $button_text = 'Import from NCBI Taxonomy';
  52. /**
  53. * Indicates the methods that the file uploader will support.
  54. */
  55. public static $methods = array(
  56. // Allow the user to upload a file to the server.
  57. 'file_upload' => FALSE,
  58. // Allow the user to provide the path on the Tripal server for the file.
  59. 'file_local' => FALSE,
  60. // Allow the user to provide a remote URL for the file.
  61. 'file_remote' => FALSE,
  62. );
  63. /**
  64. * Indicates if the file must be provided. An example when it may not be
  65. * necessary to require that the user provide a file for uploading if the
  66. * loader keeps track of previous files and makes those available for
  67. * selection.
  68. */
  69. public static $file_required = FALSE;
  70. /**
  71. * The array of arguments used for this loader. Each argument should
  72. * be a separate array containing a machine_name, name, and description
  73. * keys. This information is used to build the help text for the loader.
  74. */
  75. public static $argument_list = array();
  76. /**
  77. * Indicates how many files are allowed to be uploaded. By default this is
  78. * set to allow only one file. Change to any positive number. A value of
  79. * zero indicates an unlimited number of uploaded files are allowed.
  80. */
  81. public static $cardinality = 0;
  82. /**
  83. * Holds the list of all orgainsms currently in Chado. This list
  84. * is needed when checking to see if an organism has already been
  85. * loaded.
  86. */
  87. private $all_orgs = array();
  88. /**
  89. * The record from the Chado phylotree table that refers to this
  90. * Taxonomic tree.
  91. */
  92. private $phylotree = NULL;
  93. /**
  94. * The temporary tree array used by the Tripal Phylotree API for
  95. * importing a new tree.
  96. */
  97. private $tree = NULL;
  98. /**
  99. * @see TripalImporter::form()
  100. */
  101. public function form($form, &$form_state) {
  102. $form['instructions'] = array(
  103. '#type' => 'fieldset',
  104. '#title' => 'instructions',
  105. '#description' => t('This form is used to import species from the NCBI
  106. Taxonomy database into this site. Alternatively, it can import details
  107. about organisms from the NCBI Taxonomy database for organisms that
  108. already exist on this site. This loader will also construct
  109. the taxonomic tree for the species loaded.'),
  110. );
  111. $form['taxonomy_ids'] = array(
  112. '#type' => 'textarea',
  113. '#title' => 'Taxonomy ID',
  114. '#description' => t('Please provide a list of NCBI taxonomy IDs separated
  115. by spaces, tabs or new lines.
  116. The information about these organisms will be downloaded and new organism
  117. records will be added to this site.')
  118. );
  119. $form['import_existing'] = array(
  120. '#type' => 'checkbox',
  121. '#title' => 'Import details for existing species.',
  122. '#description' => t('The NCBI Taxonomic Importer examines the organisms
  123. currently present in the database and queries NCBI for the
  124. taxonomic details. If the importer is able to match the
  125. genus and species with NCBI the species details will be imported,
  126. and a page containing the taxonomic tree will be created.'),
  127. '#default value' => 1,
  128. );
  129. return $form;
  130. }
  131. /**
  132. * @see TripalImporter::formValidate()
  133. */
  134. public function formValidate($form, &$form_state) {
  135. global $user;
  136. $import_existing = $form_state['values']['import_existing'];
  137. $taxonomy_ids = $form_state['values']['taxonomy_ids'];
  138. // make sure that we have numeric values, one per line.
  139. if ($taxonomy_ids) {
  140. $tax_ids = preg_split("/[\s\n\t\r]+/", $taxonomy_ids);
  141. $bad_ids = array();
  142. foreach ($tax_ids as $tax_id) {
  143. $tax_id = trim($tax_id);
  144. if (!preg_match('/^\d+$/', $tax_id)) {
  145. $bad_ids[] = $tax_id;
  146. }
  147. }
  148. if (count($bad_ids) > 0) {
  149. form_set_error('taxonomy_ids',
  150. t('Taxonomy IDs must be numeric. The following are not valid: "@ids".',
  151. array('@ids' => implode('", "', $bad_ids))));
  152. }
  153. }
  154. }
  155. /**
  156. * Performs the import.
  157. */
  158. public function run() {
  159. global $site_name;
  160. $arguments = $this->arguments['run_args'];
  161. $taxonomy_ids = $arguments['taxonomy_ids'];
  162. $import_existing = $arguments['import_existing'];
  163. // Get the list of all organisms as we'll need this to lookup existing
  164. // organisms.
  165. if (chado_get_version() > 1.2) {
  166. $sql = "
  167. SELECT O.*, CVT.name as type
  168. FROM {organism} O
  169. LEFT JOIN {cvterm} CVT ON CVT.cvterm_id = O.type_id
  170. ORDER BY O.genus, O.species
  171. ";
  172. }
  173. else {
  174. $sql = "
  175. SELECT O.*, '' as type
  176. FROM {organism} O
  177. ORDER BY O.genus, O.species
  178. ";
  179. }
  180. $results = chado_query($sql);
  181. while ($item = $results->fetchObject()) {
  182. $this->all_orgs[] = $item;
  183. }
  184. // Get the phylotree object.
  185. $this->logMessage('Initializing Tree...');
  186. $this->phylotree = $this->initTree();
  187. $this->logMessage('Rebuilding Tree...');
  188. $this->tree = $this->rebuildTree();
  189. // Clean out the phnylondes for this tree in the event this is a reload
  190. chado_delete_record('phylonode', array('phylotree_id' => $this->phylotree->phylotree_id));
  191. // Get the taxonomy IDs provided by the user (if any).
  192. $tax_ids = array();
  193. if ($taxonomy_ids) {
  194. $tax_ids = preg_split("/[\s\n\t\r]+/", $taxonomy_ids);
  195. }
  196. // Set the number of items to handle.
  197. if ($taxonomy_ids and $import_existing) {
  198. $this->setTotalItems(count($this->all_orgs) + count($tax_ids));
  199. }
  200. if ($taxonomy_ids and !$import_existing) {
  201. $this->setTotalItems(count($tax_ids));
  202. }
  203. if (!$taxonomy_ids and $import_existing) {
  204. $this->setTotalItems(count($this->all_orgs));
  205. }
  206. $this->setItemsHandled(0);
  207. // If the user wants to import new taxonomy IDs then do that.
  208. if ($taxonomy_ids){
  209. $this->logMessage('Importing Taxonomy IDs...');
  210. foreach ($tax_ids as $tax_id) {
  211. $tax_id = trim($tax_id);
  212. $this->importRecord($tax_id);
  213. $this->addItemsHandled(1);
  214. }
  215. }
  216. // If the user wants to update existing records then do that.
  217. if ($import_existing) {
  218. $this->logMessage('Updating Existing...');
  219. $this->updateExisting();
  220. }
  221. // Now import the tree.
  222. $options = array('taxonomy' => 1);
  223. chado_phylogeny_import_tree($this->tree, $this->phylotree, $options);
  224. }
  225. /**
  226. * Create the taxonomic tree in Chado.
  227. *
  228. * If the tree already exists it will not be recreated.
  229. *
  230. * @throws Exception
  231. * @return
  232. * Returns the phylotree object.
  233. */
  234. private function initTree() {
  235. // Add the taxonomy tree record into the phylotree table. If the tree
  236. // already exists then don't insert it again.
  237. $site_name = variable_get('site_name');
  238. $tree_name = $site_name . 'Taxonomy Tree';
  239. $phylotree = chado_select_record('phylotree', array('*'), array('name' => $tree_name));
  240. if (count($phylotree) == 0) {
  241. // Add the taxonomic tree.
  242. $phylotree = array(
  243. 'name' => $site_name . 'Taxonomy Tree',
  244. 'description' => 'A phylogenetic tree based on taxonomic rank.',
  245. 'leaf_type' => 'taxonomy',
  246. 'tree_file' => '/dev/null',
  247. 'format' => 'taxonomy',
  248. 'no_load' => TRUE,
  249. );
  250. $errors = array();
  251. $warnings = array();
  252. $success = tripal_insert_phylotree($phylotree, $errors, $warnings);
  253. if (!$success) {
  254. throw new Exception("Cannot add the Taxonomy Tree record.");
  255. }
  256. $phylotree = (object) $phylotree;
  257. }
  258. else {
  259. $phylotree = $phylotree[0];
  260. }
  261. return $phylotree;
  262. }
  263. /**
  264. * Iterates through all existing organisms and rebuilds the taxonomy tree.
  265. *
  266. * The phloytree API doesn't support adding nodes to existing trees only
  267. * importing whole trees. So, we must rebuild the tree using the current
  268. * organisms and then we can add to it.
  269. *
  270. */
  271. private function rebuildTree() {
  272. $lineage_nodes[] = array();
  273. // Get the "rank" cvterm. It requires that the TAXRANK vocabulary is loaded.
  274. $rank_cvterm = chado_get_cvterm(array(
  275. 'name' => 'rank',
  276. 'cv_id' => array('name' => 'local')
  277. ));
  278. // The taxonomic tree must have a root, so create that first.
  279. $tree = array(
  280. 'name' => 'root',
  281. 'depth' => 0,
  282. 'is_root' => 1,
  283. 'is_leaf' => 0,
  284. 'is_internal' => 0,
  285. 'left_index' => 0,
  286. 'right_index' => 0,
  287. 'branch_set' => array(),
  288. );
  289. $total = count($this->all_orgs);
  290. $j = 1;
  291. foreach ($this->all_orgs as $organism) {
  292. $sci_name = chado_get_organism_scientific_name($organism);
  293. //$this->logMessage("- " . ($j++) . " of $total. Adding @organism", array('@organism' => $sci_name));
  294. // First get the phylonode record for this organism.
  295. $sql = "
  296. SELECT P.*
  297. FROM {phylonode} P
  298. INNER JOIN {phylonode_organism} PO on PO.phylonode_id = P.phylonode_id
  299. WHERE P.phylotree_id = :phylotree_id AND PO.organism_id = :organism_id
  300. ";
  301. $args = array(
  302. ':phylotree_id' => $this->phylotree->phylotree_id,
  303. ':organism_id' => $organism->organism_id,
  304. );
  305. $result = chado_query($sql, $args);
  306. if (!$result) {
  307. continue;
  308. }
  309. $phylonode = $result->fetchObject();
  310. // Next get the lineage for this organism.
  311. $lineage = $this->getProperty($organism->organism_id, 'lineage');
  312. if (!$lineage) {
  313. continue;
  314. }
  315. $lineage_depth = preg_split('/;\s*/', $lineage->value);
  316. // Now rebuild the tree by first creating the nodes for the full
  317. // lineage and then adding the organism as a leaf node.
  318. $parent = $tree;
  319. $i = 1;
  320. $lineage_good = TRUE;
  321. foreach ($lineage_depth as $child) {
  322. // We need to find the node in the phylotree for this level of the
  323. // lineage, but there's a lot of repeats and we don't want to keep
  324. // doing the same queries over and over, so we store the nodes
  325. // we've already seen in the $lineage_nodes array for fast lookup.
  326. if (array_key_exists($child, $lineage_nodes)) {
  327. $phylonode = $lineage_nodes[$child];
  328. if (!$phylonode) {
  329. $lineage_good = FALSE;
  330. continue;
  331. }
  332. }
  333. else {
  334. $values = array(
  335. 'phylotree_id' => $this->phylotree->phylotree_id,
  336. 'label' => $child,
  337. );
  338. $columns = array('*');
  339. $phylonode = chado_select_record('phylonode', $columns, $values);
  340. if (count($phylonode) == 0) {
  341. $lineage_nodes[$child] = NULL;
  342. $lineage_good = FALSE;
  343. continue;
  344. }
  345. $phylonode = $phylonode[0];
  346. $lineage_nodes[$child] = $phylonode;
  347. $values = array(
  348. 'phylonode_id' => $phylonode->phylonode_id,
  349. 'type_id' => $rank_cvterm->cvterm_id,
  350. );
  351. $columns = array('*');
  352. $phylonodeprop = chado_select_record('phylonodeprop', $columns, $values);
  353. }
  354. $name = $child;
  355. $node_rank = (string) $child->Rank;
  356. $node = array(
  357. 'name' => $name,
  358. 'depth' => $i,
  359. 'is_root' => 0,
  360. 'is_leaf' => 0,
  361. 'is_internal' => 1,
  362. 'left_index' => 0,
  363. 'right_index' => 0,
  364. 'parent' => $parent,
  365. 'branch_set' => array(),
  366. 'parent' => $parent['name'],
  367. 'properties' => array(
  368. $rank_cvterm->cvterm_id => $phylonodeprop[0]->value,
  369. ),
  370. );
  371. $parent = $node;
  372. $this->addTaxonomyNode($tree, $node, $lineage_depth);
  373. $i++;
  374. } // end foreach ($lineage_depth as $child) { ...
  375. // If $stop is set then we had problems setting the lineage so
  376. // skip adding the leaf node below.
  377. if (!$lineage_good) {
  378. continue;
  379. }
  380. $rank_type = 'species';
  381. if (property_exists($organism, 'type_id') and $organism->type_id) {
  382. $rank_type = $organism->type;
  383. }
  384. // Now add in the leaf node
  385. $sci_name = chado_get_organism_scientific_name($organism);
  386. $node = array(
  387. 'name' => $sci_name,
  388. 'depth' => $i,
  389. 'is_root' => 0,
  390. 'is_leaf' => 1,
  391. 'is_internal' => 0,
  392. 'left_index' => 0,
  393. 'right_index' => 0,
  394. 'parent' => $parent['name'],
  395. 'organism_id' => $organism->organism_id,
  396. 'properties' => array(
  397. $rank_cvterm->cvterm_id => $rank_type,
  398. ),
  399. );
  400. $this->addTaxonomyNode($tree, $node, $lineage_depth);
  401. // Set the indecies for the tree.
  402. chado_assign_phylogeny_tree_indices($tree);
  403. }
  404. return $tree;
  405. }
  406. /**
  407. * Imports details from NCBI Taxonomy for organisms that alrady exist.
  408. */
  409. private function updateExisting() {
  410. foreach ($this->all_orgs as $organism) {
  411. // If the organism record is marked as new then let's skip it because
  412. // it was newly added and should have the updated information already.
  413. if ($organism->is_new) {
  414. continue;
  415. }
  416. // TODO: we should check if the organism already has a taxonomy ID.
  417. // if so we should use that instead of the scientific name.
  418. // Build the query string to get the information about this species.
  419. $sci_name = chado_get_organism_scientific_name($organism);
  420. $sci_name = urlencode($sci_name);
  421. $search_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?".
  422. "db=taxonomy" .
  423. "&term=$sci_name";
  424. // Get the search response from NCBI.
  425. $rfh = fopen($search_url, "r");
  426. $xml_text = '';
  427. while (!feof($rfh)) {
  428. $xml_text .= fread($rfh, 255);
  429. }
  430. fclose($rfh);
  431. // Parse the XML to get the taxonomy ID
  432. $xml = new SimpleXMLElement($xml_text);
  433. if ($xml) {
  434. $taxid = (string) $xml->IdList->Id;
  435. if ($taxid) {
  436. $this->importRecord($taxid, $organism);
  437. }
  438. }
  439. $this->addItemsHandled(1);
  440. }
  441. }
  442. /**
  443. * Checks the Chado database to see if the organism already exists.
  444. *
  445. * @param $taxid
  446. * The taxonomic ID for the organism.
  447. * @param $sci_name
  448. * The scientific name for the organism as returned by NCBI
  449. */
  450. private function findOrganism($taxid, $sci_name) {
  451. $organism = NULL;
  452. // First check the taxid to see if it's present and assocaited with an
  453. // organism already.
  454. $values = array(
  455. 'db_id' => array(
  456. 'name' => 'NCBITaxon'
  457. ),
  458. 'accession' => $taxid,
  459. );
  460. $columns = array('dbxref_id');
  461. $dbxref = chado_select_record('dbxref', $columns, $values);
  462. if (count($dbxref) > 0) {
  463. $columns = array('organism_id');
  464. $values = array('dbxref_id' => $dbxref[0]->dbxref_id);
  465. $organism_dbxref = chado_select_record('organism_dbxref', $columns, $values);
  466. if (count($organism_dbxref) >0) {
  467. $organism_id = $organism_dbxref[0]->organism_id;
  468. $columns = array('*');
  469. $values = array('organism_id' => $organism_id);
  470. $organism = chado_select_record('organism', $columns, $values);
  471. if (count($organism) > 0) {
  472. $organism = $organism[0];
  473. }
  474. }
  475. }
  476. // If the caller did not provide an organism then we want to try and
  477. // add one. But, it only makes sense to add one if this record
  478. // is of rank species.
  479. // First check if the full name (including the infrasepcific name)
  480. // are all present in the genus and species name. This would have
  481. // been the Chado v1.2 (or less) of storing species.
  482. if (!$organism) {
  483. $sql = "
  484. SELECT organism_id
  485. FROM {organism}
  486. WHERE concat(genus, ' ', species) = :sci_name
  487. ";
  488. $results = chado_query($sql, array(':sci_name' => $sci_name));
  489. $item = $results->fetchObject();
  490. if ($item) {
  491. $columns = array('*');
  492. $values = array('organism_id' => $item->organism_id);
  493. $organism = chado_select_record('organism', $columns, $values);
  494. if (count($organism) > 0) {
  495. $organism = $organism[0];
  496. }
  497. }
  498. }
  499. // Second, check if the full name includes the infraspecific name.
  500. if (!$organism) {
  501. foreach ($this->all_orgs as $item) {
  502. $internal_sci_name = chado_get_organism_scientific_name($item);
  503. if ($sci_name == $internal_sci_name) {
  504. $organism = $item;
  505. }
  506. }
  507. }
  508. return $organism;
  509. }
  510. /**
  511. * Adds a new organism record to Chado.
  512. *
  513. * @param sci_name
  514. * The scientific name as provied by NCBI Taxonomy.
  515. * @param $rank
  516. * The rank of the organism as provied by NCBI Taxonomy.
  517. */
  518. private function addOrganism($sci_name, $rank) {
  519. $organism = NULL;
  520. $matches = array();
  521. $genus = '';
  522. $species = '';
  523. $infra = '';
  524. $values = array();
  525. // Check if the scientific name has an infraspecific part or is just
  526. // a species name.
  527. if (preg_match('/^(.+?)\s+(.+?)\s+(.+)$/', $sci_name, $matches)) {
  528. $genus = $matches[1];
  529. $species = $matches[2];
  530. $infra = $matches[3];
  531. // Get the CV term for the rank.
  532. $type = chado_get_cvterm(array(
  533. 'name' => preg_replace('/ /','_', $rank),
  534. 'cv_id' => array('name' => 'taxonomic_rank')
  535. ));
  536. // Remove the rank from the infraspecific name.
  537. $abbrev = chado_abbreviate_infraspecific_rank($rank);
  538. $infra = preg_replace("/$abbrev/", "", $infra);
  539. $infra = trim($infra);
  540. $values = array(
  541. 'genus' => $genus,
  542. 'species' => $species,
  543. 'abbreviation' => $genus[0] . '. ' . $species,
  544. 'type_id' => $type->cvterm_id,
  545. 'infraspecific_name' => $infra,
  546. );
  547. $organism = chado_insert_record('organism', $values);
  548. $organism = (object) $organism;
  549. $organism->type = $rank;
  550. }
  551. else if (preg_match('/^(.+?)\s+(.+?)$/', $sci_name, $matches)) {
  552. $genus = $matches[1];
  553. $species = $matches[2];
  554. $infra = '';
  555. $values = array(
  556. 'genus' => $genus,
  557. 'species' => $species,
  558. 'abbreviation' => $genus[0] . '. ' . $species,
  559. );
  560. $organism = chado_insert_record('organism', $values);
  561. $organism = (object) $organism;
  562. }
  563. if ($organism) {
  564. $organism->is_new = TRUE;
  565. $this->all_orgs[] = $organism;
  566. }
  567. return $organism;
  568. }
  569. /**
  570. * Imports an organism from the NCBI taxonomy DB by its taxonomy ID
  571. *
  572. * @param $taxid
  573. * The NCBI Taxonomy ID.
  574. * @param $organism
  575. * The organism object to which this taxonomy belongs. If the organism
  576. * is NULL then it will be created.
  577. */
  578. private function importRecord($taxid, $organism = NULL) {
  579. $adds_organism = $organism ? FALSE : TRUE;
  580. // Get the "rank" cvterm. It requires that the TAXRANK vocabulary is loaded.
  581. $rank_cvterm = chado_get_cvterm(array(
  582. 'name' => 'rank',
  583. 'cv_id' => array('name' => 'local')
  584. ));
  585. // Get the details for this taxonomy.
  586. $fetch_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?".
  587. "db=taxonomy" .
  588. "&id=$taxid";
  589. // Get the search response from NCBI.
  590. $rfh = fopen($fetch_url, "r");
  591. $xml_text = '';
  592. while (!feof($rfh)) {
  593. $xml_text .= fread($rfh, 255);
  594. }
  595. fclose($rfh);
  596. $xml = new SimpleXMLElement($xml_text);
  597. if ($xml) {
  598. $taxon = $xml->Taxon;
  599. // Get the genus and species from the xml.
  600. $parent = (string) $taxon->ParentTaxId;
  601. $rank = (string) $taxon->Rank;
  602. $sci_name = (string) $taxon->ScientificName;
  603. //$this->logMessage(' - Importing @sci_name', array('@sci_name' => $sci_name));
  604. // If we don't have an organism record provided then see if there
  605. // is one provided by Chado, if not, the try to add one.
  606. if (!$organism) {
  607. $organism = $this->findOrganism($taxid, $sci_name);
  608. if (!$organism) {
  609. $organism = $this->addOrganism($sci_name, $rank);
  610. if (!$organism) {
  611. throw new Exception(t('Cannot add organism: @sci_name', array('@sci_name' => $sci_name)));
  612. }
  613. }
  614. }
  615. // Associate the Dbxref with the organism.
  616. $this->addDbxref($organism->organism_id, $taxid);
  617. // Get properties for this organism.
  618. $lineage = (string) $taxon->Lineage;
  619. $genetic_code = (string) $taxon->GeneticCode->GCId;
  620. $genetic_code_name = (string) $taxon->GeneticCode->GCName;
  621. $mito_genetic_code = (string) $taxon->MitoGeneticCode->MGCId;
  622. $mito_genetic_code_name = (string) $taxon->MitoGeneticCode->MGCName;
  623. $division = (string) $taxon->Division;
  624. // Add in the organism properties.
  625. $this->addProperty($organism->organism_id, 'division', $division);
  626. $this->addProperty($organism->organism_id, 'mitochondrial_genetic_code_name', $mito_genetic_code_name);
  627. $this->addProperty($organism->organism_id, 'mitochondrial_genetic_code', $mito_genetic_code);
  628. $this->addProperty($organism->organism_id, 'genetic_code_name', $genetic_code_name);
  629. $this->addProperty($organism->organism_id, 'lineage', $lineage);
  630. $this->addProperty($organism->organism_id, 'genetic_code', $genetic_code);
  631. $name_ranks = array();
  632. if ($taxon->OtherNames->children) {
  633. foreach ($taxon->OtherNames->children() as $child) {
  634. $type = $child->getName();
  635. $name = (string) $child;
  636. if (!array_key_exists($type, $name_ranks)) {
  637. $name_ranks[$type] = 0;
  638. }
  639. switch ($type) {
  640. case 'GenbankCommonName':
  641. $this->addProperty($organism->organism_id, 'genbank_common_name', $name, $name_ranks[$type]);
  642. break;
  643. case 'Synonym':
  644. case 'GenbankSynonym':
  645. $this->addProperty($organism->organism_id, 'synonym', $name, $name_ranks[$type]);
  646. break;
  647. case 'CommonName':
  648. // If we had to add the organism then include the commone name too.
  649. if ($adds_organism) {
  650. $organism->common_name = $name;
  651. $values = array('organism_id' => $organism->id);
  652. chado_update_record('organism', $values, $organism);
  653. }
  654. case 'Includes':
  655. $this->addProperty($organism->organism_id, 'other_name', $name, $name_ranks[$type]);
  656. break;
  657. case 'EquivalentName':
  658. $this->addProperty($organism->organism_id, 'equivalent_name', $name, $name_ranks[$type]);
  659. break;
  660. case 'Anamorph':
  661. $this->addProperty($organism->organism_id, 'anamorph', $name, $name_ranks[$type]);
  662. break;
  663. case 'Name':
  664. // skip the Name stanza
  665. break;
  666. default:
  667. print "NOTICE: Skipping unrecognzed name type: $type\n";
  668. // do nothing for unrecognized types
  669. }
  670. $name_ranks[$type]++;
  671. }
  672. }
  673. // Generate a nested array structure that can be used for importing the tree.
  674. $lineage_depth = preg_split('/;\s*/', $lineage);
  675. $parent = $this->tree;
  676. $i = 1;
  677. foreach ($taxon->LineageEx->children() as $child) {
  678. $tid = (string) $child->TaxID;
  679. $name = (string) $child->ScientificName;
  680. $node_rank = (string) $child->Rank;
  681. $node = array(
  682. 'name' => $name,
  683. 'depth' => $i,
  684. 'is_root' => 0,
  685. 'is_leaf' => 0,
  686. 'is_internal' => 1,
  687. 'left_index' => 0,
  688. 'right_index' => 0,
  689. 'parent' => $parent,
  690. 'branch_set' => array(),
  691. 'parent' => $parent['name'],
  692. 'properties' => array(
  693. $rank_cvterm->cvterm_id => $node_rank,
  694. ),
  695. );
  696. $parent = $node;
  697. $this->addTaxonomyNode($this->tree, $node, $lineage_depth);
  698. $i++;
  699. }
  700. // Now add in the leaf node
  701. $node = array(
  702. 'name' => $sci_name,
  703. 'depth' => $i,
  704. 'is_root' => 0,
  705. 'is_leaf' => 1,
  706. 'is_internal' => 0,
  707. 'left_index' => 0,
  708. 'right_index' => 0,
  709. 'parent' => $parent['name'],
  710. 'organism_id' => $organism->organism_id,
  711. 'properties' => array(
  712. $rank_cvterm->cvterm_id => $rank,
  713. ),
  714. );
  715. $this->addTaxonomyNode($this->tree, $node, $lineage_depth);
  716. // Set the indecies for the tree.
  717. chado_assign_phylogeny_tree_indices($this->tree);
  718. }
  719. }
  720. /**
  721. *
  722. */
  723. private function addTaxonomyNode(&$tree, $node, $lineage_depth) {
  724. // Get the branch set for the tree root.
  725. $branch_set = &$tree['branch_set'];
  726. // Iterate through the tree up until the depth where this node will
  727. // be placed.
  728. $node_depth = $node['depth'];
  729. for ($i = 1; $i <= $node_depth; $i++) {
  730. // Iterate through any existing nodes in the branch set to see if
  731. // the node name matches the correct name for the lineage at this
  732. // depth. If it matches then it is inside of this branch set that
  733. // we will place the node.
  734. for ($j = 0; $j < count($branch_set); $j++) {
  735. // If this node already exists in the tree then return.
  736. if ($branch_set[$j]['name'] == $node['name'] and
  737. $branch_set[$j]['depth'] = $node['depth']) {
  738. return;
  739. }
  740. // Otherwise, set the branch to be the current branch and continue.
  741. if ($branch_set[$j]['name'] == $lineage_depth[$i-1]) {
  742. $branch_set = &$branch_set[$j]['branch_set'];
  743. break;
  744. }
  745. }
  746. }
  747. // Add the node to the last branch set. This should be where this node goes.
  748. $branch_set[] = $node;
  749. }
  750. /**
  751. * Retrieves a property for a given organism.
  752. *
  753. * @param $organism_id
  754. * The organism ID to which the property is added.
  755. * @param $term_name
  756. * The name of the organism property term. This term must be
  757. * present in the 'organism_property' cv.
  758. * @param $rank
  759. * The order for this property. The first instance of this term for
  760. * this organism should be zero. Defaults to zero.
  761. * @return
  762. * The property object.
  763. */
  764. private function getProperty($organism_id, $term_name, $rank = 0) {
  765. $record = array(
  766. 'table' => 'organism',
  767. 'id' => $organism_id
  768. );
  769. $property = array(
  770. 'type_name' => $term_name,
  771. 'cv_name' => 'organism_property',
  772. 'value' => $value,
  773. 'rank' => $rank
  774. );
  775. return chado_get_property($record, $property);
  776. }
  777. /**
  778. * Adds a property to an organism node.
  779. *
  780. * @param $organism_id
  781. * The organism ID to which the property is added.
  782. * @param $term_name
  783. * The name of the organism property term. This term must be
  784. * present in the 'organism_property' cv.
  785. * @param $value
  786. * The value of the property.
  787. * @param $rank
  788. * The order for this property. The first instance of this term for
  789. * this organism should be zero. Defaults to zero.
  790. */
  791. private function addProperty($organism_id, $term_name, $value, $rank = 0) {
  792. if (!$value) {
  793. return;
  794. }
  795. $record = array(
  796. 'table' => 'organism',
  797. 'id' => $organism_id
  798. );
  799. $property = array(
  800. 'type_name' => $term_name,
  801. 'cv_name' => 'organism_property',
  802. 'value' => $value
  803. );
  804. // Delete all properties of this type if the rank is zero.
  805. if ($rank == 0) {
  806. chado_delete_property($record, $property);
  807. }
  808. chado_insert_property($record, $property);
  809. }
  810. /**
  811. *
  812. * @param unknown $organism_id
  813. * @param unknown $taxId
  814. */
  815. private function addDbxref($organism_id, $taxId) {
  816. $db = chado_get_db(array('name' => 'NCBITaxon'));
  817. $values = array(
  818. 'db_id' => $db->db_id,
  819. 'accession' => $taxId
  820. );
  821. $dbxref = chado_insert_dbxref($values);
  822. $values = array(
  823. 'dbxref_id' => $dbxref->dbxref_id,
  824. 'organism_id' => $organism_id,
  825. );
  826. if (!chado_select_record('organism_dbxref', ['organism_dbxref_id'], $values)) {
  827. chado_insert_record('organism_dbxref', $values);
  828. }
  829. }
  830. }