tripal_chado.phylotree.api.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage organisms
  5. */
  6. /**
  7. * @defgroup tripal_phylotree_api Chado Phylotree
  8. * @ingroup tripal_chado_api
  9. * @{
  10. * @}
  11. */
  12. /**
  13. * Validates an $options array for insert or update of a phylotree record.
  14. *
  15. * If validation passes then any values that needed validation lookups
  16. * (such as the dbxref, analysis, leaf_type, etc) will have their approriate
  17. * primary_keys added to the $options array, and missing default values
  18. * will also be added.
  19. *
  20. * @param $val_type
  21. * The type of validation. Can be either 'insert' or 'update'.
  22. * @param $options
  23. * An array of key/value pairs containing any of the valid keys for
  24. * either the tripal_insert_phylotree() or tripal_update_phylotree()
  25. * functions.
  26. * @param $errors
  27. * An empty array where validation error messages will be set. The keys
  28. * of the array will be name of the field from the options array and the
  29. * value is the error message.
  30. * @param $warnings
  31. * An empty array where validation warning messagges will be set. The
  32. * warnings should not stop an insert or an update but should be provided
  33. * to the user as information by a drupal_set_message() if appropriate. The
  34. * keys of the array will be name of the field from the options array and the
  35. * value is the error message.
  36. * @return
  37. * If validation failes then FALSE is returned. Any options that do not pass
  38. * validation checks will be added in the $errors array with the key being
  39. * the option and the value being the error message. If validation
  40. * is successful then TRUE is returned.
  41. *
  42. * @ingroup tripal_phylotree_api
  43. */
  44. function tripal_validate_phylotree($val_type, &$options, &$errors, &$warnings) {
  45. if ($val_type != 'insert' and $val_type != 'update') {
  46. tripal_report_error('tripal_phylogeny', TRIPAL_ERROR, "The $val_type argument must be either 'update or 'insert'.");
  47. }
  48. // Set Defaults.
  49. if ($val_type == 'insert') {
  50. // Match by feature name.
  51. if (!array_key_exists('match', $options)) {
  52. $options['match'] = 'name';
  53. }
  54. // The regular expression is to match the entire node name.
  55. if (!array_key_exists('name_re', $options)) {
  56. $options['name_re'] = '^(.*)$';
  57. }
  58. // A dbxref is not required by Tripal but is required by the database
  59. // field in the phylotree table. Therefore, if the dbxref is not provided
  60. // we can set this to be the null database and null dbxref which
  61. // is represented as 'null:local:null'
  62. if (!array_key_exists('dbxref', $options)) {
  63. $options['dbxref'] = "null:local:null";
  64. }
  65. }
  66. // Make sure required values are set.
  67. if ($val_type == 'insert') {
  68. if (!array_key_exists('name', $options)) {
  69. $errors['name'] = t('Please provide the name of the tree.');
  70. return FALSE;
  71. }
  72. if (!array_key_exists('description', $options)) {
  73. $errors['description'] = t('Please provide a description for this tree.');
  74. return FALSE;
  75. }
  76. if (!array_key_exists('tree_file', $options)) {
  77. $errors['tree_file'] = t('Please provide either the full path to the tree_file or a Drupal managed file ID number.');
  78. return FALSE;
  79. }
  80. if (!array_key_exists('format', $options) or !$options['format']) {
  81. $errors['format'] = t('Please provide a file format for the tree file.');
  82. return FALSE;
  83. }
  84. // Make sure the file format is correct
  85. if ($options['format'] != 'newick' and $options['format'] != 'taxonomy') {
  86. $errors['format'] = t('The file format is not supported. Currently only the "newick" file format is supported.');
  87. return FALSE;
  88. }
  89. }
  90. else {
  91. // Does the phylotree ID exist and is it valid
  92. if (!array_key_exists('phylotree_id', $options)) {
  93. $errors['phylotree_id'] = t('Please provide the ID for the tree.');
  94. return FALSE;
  95. }
  96. $exists = chado_select_record('phylotree', array('phylotree_id'),
  97. array('phylotree_id' => $options['phylotree_id']), array('has_record' => 1));
  98. if (!$exists) {
  99. $errors['phylotree_id'] = t('The phylotree_id does not exist.');
  100. return FALSE;
  101. }
  102. }
  103. // Make sure the file exists if one is specified
  104. if (array_key_exists('tree_file', $options) and $options['tree_file']) {
  105. // If this is a numeric Drupal file then all is good, no need to check.
  106. if (!is_numeric($options['tree_file'])) {
  107. if (!file_exists($options['tree_file'])) {
  108. $errors['tree_file'] = t('The file provided does not exists.');
  109. return FALSE;
  110. }
  111. }
  112. // Make sure the file format is correct
  113. if (!array_key_exists('format', $options) or
  114. ($options['format'] != 'newick' and $options['format'] != 'taxonomy')) {
  115. $errors['format'] = t('Please provide a supported file format. Currently only the "newick" file format is supported.');
  116. return FALSE;
  117. }
  118. // If no leaf type is provided then use the polypeptide term.
  119. if (!array_key_exists('leaf_type', $options) or !$options['leaf_type']) {
  120. $options['leaf_type'] = 'polypeptide';
  121. }
  122. }
  123. // Make sure the analysis exists.
  124. $analysis = NULL;
  125. if (array_key_exists('analysis_id', $options) and $options['analysis_id']) {
  126. $analysis = chado_select_record('analysis', array('analysis_id'), array('analysis_id' => $options['analysis_id']));
  127. if (!$analysis) {
  128. $errors['analysis_id'] = t('The analysis name provided does not exist.');
  129. return FALSE;
  130. }
  131. $options['analysis_id'] = $analysis[0]->analysis_id;
  132. }
  133. if (array_key_exists('analysis', $options) and $options['analysis']) {
  134. $analysis = chado_select_record('analysis', array('analysis_id'), array('name' => $options['analysis']));
  135. if (!$analysis) {
  136. $errors['analysis'] = t('The analysis ID provided does not exist.');
  137. return FALSE;
  138. }
  139. $options['analysis_id'] = $analysis[0]->analysis_id;
  140. }
  141. // Make sure the leaf type exists.
  142. $type = NULL;
  143. if (array_key_exists('leaf_type', $options) and $options['leaf_type']) {
  144. if ($options['leaf_type'] == 'taxonomy') {
  145. $values = array(
  146. 'cv_id' => array(
  147. 'name' => 'EDAM'
  148. ),
  149. 'name' => 'Species tree'
  150. );
  151. $type = chado_select_record('cvterm', array('cvterm_id'), $values);
  152. }
  153. else {
  154. $values = array(
  155. 'cv_id' => array(
  156. 'name' => 'sequence'
  157. ),
  158. 'name' => $options['leaf_type']
  159. );
  160. $type = chado_select_record('cvterm', array('cvterm_id'), $values);
  161. if (!$type) {
  162. $errors['leaf_type'] = t('The leaf_type provided is not a valid Sequence Ontology term: %term.');
  163. return FALSE;
  164. }
  165. }
  166. $options['type_id'] = $type[0]->cvterm_id;
  167. }
  168. // A Dbxref is required by the phylotree module, but if the
  169. // tree was generated in-house and the site admin doens't want to
  170. // assign a local dbxref then we will set it to the null db
  171. // and the local:null dbxref.
  172. if (array_key_exists('dbxref', $options)) {
  173. if (!$options['dbxref']) {
  174. $options['dbxref'] = 'null:local:null';
  175. }
  176. $matches = array();
  177. preg_match('/^(.*?):(.*)$/', $options['dbxref'], $matches);
  178. $db_name = $matches[1];
  179. $accession = $matches[2];
  180. $values = array(
  181. 'accession' => $accession,
  182. 'db_id' => array(
  183. 'name' => $db_name
  184. ),
  185. );
  186. $dbxref = chado_generate_var('dbxref', $values);
  187. if (!$dbxref) {
  188. $errors['dbxref'] = t('The dbxref provided does not exist in the database: %dbxref.', array('%dbxref' => $dbxref));
  189. return FALSE;
  190. }
  191. $options['dbxref_id'] = $dbxref->dbxref_id;
  192. }
  193. // Make sure the tree name is unique
  194. if (array_key_exists('name', $options) and $options['name']) {
  195. $sql = "
  196. SELECT *
  197. FROM {phylotree} P
  198. WHERE
  199. P.name = :name
  200. ";
  201. $args = array(':name' => $options['name']);
  202. if ($val_type == 'update') {
  203. $sql .= " AND NOT P.phylotree_id = :phylotree_id";
  204. $args[':phylotree_id'] = $options['phylotree_id'];
  205. }
  206. $result = chado_query($sql, $args)->fetchObject();
  207. if ($result) {
  208. $errors['name'] = t("The tree name is in use by another tree. Please provide a different unique name for this tree.");
  209. }
  210. }
  211. return TRUE;
  212. }
  213. /**
  214. * Inserts a phylotree record into Chado.
  215. *
  216. * This function validates the options passed prior to insertion of the record,
  217. * and if validation passes then any values in the options array that needed
  218. * validation lookups (such as the dbxref, analysis, leaf_type, etc) will have
  219. * their approriate primary key values added to the options array.
  220. *
  221. * @param $options
  222. * An array of key value pairs with the following keys required:
  223. * 'name': The name of the tree. This will be displayed to users.
  224. * 'description: A description about the tree
  225. * 'anlaysis_id: The ID of the analysis to which this phylotree should be
  226. * associated.
  227. * 'analysis': If the analysis_id key is not used then the analysis name
  228. * may be provided to identify the analysis to which the tree
  229. * should be associated.
  230. * 'leaf_type': A sequence ontology term or the word 'organism'. If the
  231. * type is 'organism' then this tree represents a
  232. * taxonomic tree. The default, if not specified, is the
  233. * term 'polypeptide'.
  234. * 'tree_file': The path of the file containing the phylogenetic tree to
  235. * import or a Drupal managed_file numeric ID.
  236. * 'format': The file format. Currently only 'newick is supported'.
  237. * Optional keys:
  238. * 'dbxref': A database cross-reference of the form DB:ACCESSION.
  239. * Where DB is the database name, which is already present
  240. * in Chado, and ACCESSION is the unique identifier for
  241. * this tree in the remote database.
  242. * 'name_re': If the leaf type is NOT 'taxonomy', then the value of
  243. * this field can be a regular expression to pull out
  244. * the name of the feature from the node label in the
  245. * intput tree. If no value is provided the entire label is
  246. * used.
  247. * 'match': Set to 'uniquename' if the leaf nodes should be matched
  248. * with the feature uniquename.
  249. * 'load_now': If set, the tree will be loaded immediately if a tree_file
  250. * is provided. Otherwise, the tree will be loaded via
  251. * a Tripal jobs call.
  252. * 'no_load': If set the tree file will not be loaded.
  253. * @param $errors
  254. * An empty array where validation error messages will be set. The keys
  255. * of the array will be name of the field from the options array and the
  256. * value is the error message.
  257. * @param $warnings
  258. * An empty array where validation warning messagges will be set. The
  259. * warnings should not stop an insert or an update but should be provided
  260. * to the user as information by a drupal_set_message() if appropriate. The
  261. * keys of the array will be name of the field from the options array and the
  262. * value is the error message.
  263. * @return
  264. * TRUE for success and FALSE for failure.
  265. *
  266. * @ingroup tripal_phylotree_api
  267. */
  268. function tripal_insert_phylotree(&$options, &$errors, &$warnings) {
  269. global $user;
  270. $options['name_re'] = trim($options['name_re']);
  271. $options['leaf_type'] = trim($options['leaf_type']);
  272. $options['name'] = trim($options['name']);
  273. $options['format'] = trim($options['format']);
  274. $options['tree_file'] = trim($options['tree_file']);
  275. // Validate the incoming options.
  276. $success = tripal_validate_phylotree('insert', $options, $errors, $warnings);
  277. if (!$success) {
  278. foreach ($errors as $field => $message) {
  279. tripal_report_error('tripal_phylogeny', TRIPAL_ERROR, $message);
  280. }
  281. return FALSE;
  282. }
  283. // If we're here then all is good, so add the phylotree record.
  284. $values = array(
  285. 'analysis_id' => $options['analysis_id'],
  286. 'name' => $options['name'],
  287. 'dbxref_id' => $options['dbxref_id'],
  288. 'comment' => $options['description'],
  289. 'type_id' => $options['type_id'],
  290. );
  291. $phylotree = chado_insert_record('phylotree', $values);
  292. if (!$phylotree) {
  293. drupal_set_message(t('Unable to add phylotree.'), 'warning');
  294. tripal_report_error('tripal_phylogeny', TRIPAL_WARNING, 'Insert phylotree: Unable to create phylotree where values: %values',
  295. array('%values' => print_r($values, TRUE)));
  296. return FALSE;
  297. }
  298. $phylotree_id = $phylotree['phylotree_id'];
  299. $options['phylotree_id'] = $phylotree_id;
  300. // If the tree_file is numeric then it is a Drupal managed file and
  301. // we want to make the file permanent and associated with the tree.
  302. if (is_numeric($options['tree_file'])) {
  303. $file = NULL;
  304. $file = file_load($options['tree_file']);
  305. $file->status = FILE_STATUS_PERMANENT;
  306. $file = file_save($file);
  307. file_usage_add($file, 'tripal_phylogeny', $options['format'], $phylotree_id);
  308. $real_file_path = drupal_realpath($file->uri);
  309. }
  310. else {
  311. $real_file_path = $options['tree_file'];
  312. }
  313. // If caller has requested to load the file now then do so, otherwise
  314. // submit using a Tripal job.
  315. if (!array_key_exists('no_load', $options) or !$options['no_load']) {
  316. if (array_key_exists('load_now', $options) and $options['load_now']) {
  317. $args = array(
  318. 'phylotree_id' => $phylotree_id,
  319. 'leaf_type' => $options['leaf_type'],
  320. 'match' => $options['match'] ? 'uniquename' : 'name',
  321. 'name_re' => $options['name_re'],
  322. );
  323. tripal_phylogeny_import_tree_file($real_file_path, $options['format'], $args);
  324. }
  325. else {
  326. $args = array(
  327. $real_file_path,
  328. 'newick',
  329. array(
  330. 'phylotree_id' => $phylotree_id,
  331. 'leaf_type' => $options['leaf_type'],
  332. 'match' => $options['match'] ? 'uniquename' : 'name',
  333. 'name_re' => $options['name_re'],
  334. ),
  335. );
  336. if (tripal_add_job("Import Tree File: " . $file->filename, 'tripal_phylogeny',
  337. 'tripal_phylogeny_import_tree_file', $args, $user->uid)) {
  338. drupal_set_message(t('The tree visualizations will appear once the tree is fully imported.'));
  339. }
  340. }
  341. }
  342. return TRUE;
  343. }
  344. /**
  345. * Updates a phylotree record into Chado.
  346. *
  347. * This function validates the options passed prior to update of the record
  348. * and if validation passes then any values in the options array that needed
  349. * validation lookups (such as the dbxref, analysis, leaf_type, etc) will have
  350. * their approriate primary key values added to the options array. A Drupal
  351. * File object will be added to the options array for the tree file if one
  352. * is provided.
  353. *
  354. *
  355. * @param $phylotree_id
  356. * The ID of the phylotree to update.
  357. * @param $options
  358. * An array of key value pairs with the following optional keys:
  359. * 'name': The name of the tree. This will be displayed to users.
  360. * 'description: A description about the tree
  361. * 'anlaysis_id: The ID of the analysis to which this phylotree should be
  362. * associated.
  363. * 'analysis': If the analysis_id key is not used then the analysis name
  364. * may be provided to identify the analysis to which the tree
  365. * should be associated.
  366. * 'leaf_type': A sequence ontology term or the word 'organism'. If the
  367. * type is 'organism' then this tree represents a
  368. * taxonomic tree. The default, if not specified, is the
  369. * term 'polypeptide'.
  370. * 'tree_file': The path of the file containing the phylogenetic tree to
  371. * import or a Drupal managed_file numeric ID.
  372. * 'format': The file format. Currently only 'newick is supported'
  373. * 'dbxref': A database cross-reference of the form DB:ACCESSION.
  374. * Where DB is the database name, which is already present
  375. * in Chado, and ACCESSION is the unique identifier for
  376. * this tree in the remote database.
  377. * 'name_re': If the leaf type is NOT 'taxonomy', then the value of
  378. * this field can be a regular expression to pull out
  379. * the name of the feature from the node label in the
  380. * intput tree. If no value is provided the entire label is
  381. * used.
  382. * 'match': Set to 'uniquename' if the leaf nodes should be matched
  383. * with the feature uniquename.
  384. * 'load_now': If set, the tree will be loaded immediately if a tree_file
  385. * is provided. Otherwise, the tree will be loaded via
  386. * a Tripal jobs call.
  387. *
  388. * @ingroup tripal_phylotree_api
  389. */
  390. function tripal_update_phylotree($phylotree_id, &$options) {
  391. global $user;
  392. // Validate the incoming options.
  393. $errors = array();
  394. $warnings = array();
  395. $success = tripal_validate_phylotree('update', $options, $errors, $warnings);
  396. if (!$success) {
  397. foreach ($errors as $field => $message) {
  398. tripal_report_error('tripal_phylogeny', TRIPAL_ERROR, $message);
  399. }
  400. return FALSE;
  401. }
  402. // If we're here then all is good, so update the phylotree record.
  403. $match = array(
  404. 'phylotree_id' => $phylotree_id,
  405. );
  406. if (array_key_exists('name', $options) and $options['name']) {
  407. $values['name'] = $options['name'];
  408. }
  409. if (array_key_exists('analysis_id', $options) and $options['analysis_id']) {
  410. $values['analysis_id'] = $options['analysis_id'];
  411. }
  412. if (array_key_exists('dbxref_id', $options) and $options['dbxref_id']) {
  413. $values['dbxref_id'] = $options['dbxref_id'];
  414. }
  415. if (array_key_exists('description', $options) and $options['description']) {
  416. $values['comment'] = $options['description'];
  417. }
  418. if (array_key_exists('type_id', $options) and $options['type_id']) {
  419. $values['type_id'] = $options['type_id'];
  420. }
  421. $phylotree = chado_update_record('phylotree', $match, $values, array('return_record' => TRUE));
  422. if (!$phylotree) {
  423. drupal_set_message(t('Unable to update phylotree.'), 'warning');
  424. tripal_report_error('tripal_phylogeny', TRIPAL_WARNING,
  425. 'Update phylotree: Unable to update phylotree where values: %values',
  426. array('%values' => print_r($values, TRUE))
  427. );
  428. }
  429. // If we have a tree file, then import the tree
  430. if (array_key_exists('tree_file', $options) and $options['tree_file']) {
  431. // Remove any existing nodes
  432. chado_delete_record('phylonode', array('phylotree_id' => $options['phylotree_id']));
  433. // Make sure if we already have a file that we remove the old one.
  434. $sql = "
  435. SELECT FM.fid
  436. FROM {file_managed} FM
  437. INNER JOIN {file_usage} FU on FM.fid = FU.fid
  438. WHERE FU.id = :id and FU.module = 'tripal_phylogeny'
  439. ";
  440. $fid = db_query($sql, array(':id' => $options['phylotree_id']))->fetchField();
  441. if ($fid) {
  442. $file = file_load($fid);
  443. file_delete($file, TRUE);
  444. }
  445. // If the tree_file is numeric then it is a Drupal managed file and
  446. // we want to make the file permanent and associated with the tree.
  447. if (is_numeric($options['tree_file'])) {
  448. $file = file_load($options['tree_file']);
  449. $file->status = FILE_STATUS_PERMANENT;
  450. $file = file_save($file);
  451. file_usage_add($file, 'tripal_phylogeny', 'newick', $options['phylotree_id']);
  452. // Add a job to parse the new node tree.
  453. $real_file_path = drupal_realpath($file->uri);
  454. }
  455. else {
  456. $real_file_path = $options['tree_file'];
  457. }
  458. // If caller has requested to load the file now then do so, otherwise
  459. // submit using a Tripal job.
  460. if (array_key_exists('load_now', $options) and $options['load_now']) {
  461. $args = array(
  462. 'phylotree_id' => $options['phylotree_id'],
  463. 'leaf_type' => $options['leaf_type'],
  464. 'match' => $options['match'] ? 'uniquename' : 'name',
  465. 'name_re' => $options['name_re'],
  466. );
  467. tripal_phylogeny_import_tree_file($real_file_path, $options['format'], $args);
  468. }
  469. else {
  470. $args = array(
  471. $real_file_path,
  472. 'newick',
  473. array(
  474. 'phylotree_id' => $options['phylotree_id'],
  475. 'leaf_type' => $options['leaf_type'],
  476. 'match' => $options['match'] ? 'uniquename' : 'name',
  477. 'name_re' => $options['name_re'],
  478. ),
  479. );
  480. if (tripal_add_job("Import Tree File: " . $file->filename, 'tripal_phylogeny',
  481. 'tripal_phylogeny_import_tree_file', $args, $user->uid)) {
  482. drupal_set_message(t('The tree visualizations will appear once the tree is fully imported.'));
  483. }
  484. }
  485. }
  486. return TRUE;
  487. }
  488. /**
  489. * Deletes a phylotree record from Chado.
  490. *
  491. * @param $phylotree_id
  492. *
  493. * @return
  494. * TRUE on success, FALSE on failure.
  495. *
  496. * @ingroup tripal_phylotree_api
  497. */
  498. function tripal_delete_phylotree($phylotree_id) {
  499. // if we don't have a phylotree id for this node then this isn't a node of
  500. // type chado_phylotree or the entry in the chado_phylotree table was lost.
  501. if (!$phylotree_id) {
  502. tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
  503. 'Please provide a phylotree_id to delete a tree.');
  504. return FALSE;
  505. }
  506. // Remove the tree
  507. $values = array('phylotree_id' => $phylotree_id);
  508. return chado_delete_record('phylotree', $values);
  509. }
  510. /**
  511. * Iterates through the tree and sets the left and right indicies .
  512. *
  513. * @param $tree
  514. * The tree array.
  515. * @param $index
  516. * This parameters is not used when the function is first called. It
  517. * is used for recursive calls.
  518. *
  519. * @ingroup tripal_phylotree_api
  520. */
  521. function tripal_assign_phylogeny_tree_indices(&$tree, &$index = 1) {
  522. // Assign a left and right index to each node. The child node must
  523. // have a right and left index less than that of it's parents. We
  524. // increment the index by 100 to give space for new nodes that might
  525. // be added later.
  526. if (array_key_exists('name', $tree)) {
  527. $tree['left_index'] = $index += 100;
  528. if (array_key_exists('is_leaf', $tree)) {
  529. $tree['right_index'] = $index += 100;
  530. }
  531. }
  532. if (array_key_exists('branch_set', $tree)) {
  533. foreach ($tree['branch_set'] as $key => $node) {
  534. tripal_assign_phylogeny_tree_indices($tree['branch_set'][$key], $index);
  535. $tree['right_index'] = $index += 100;
  536. }
  537. }
  538. }
  539. /**
  540. * Iterates through the tree array and creates phylonodes in Chado.
  541. *
  542. * The function iterates through the tree in a top-down approach adding
  543. * parent internal nodes prior to leaf nodes. Each node of the tree should have
  544. * the following fields:
  545. *
  546. * -name: The name (or label) for this node.
  547. * -depth: The depth of the node in the tree.
  548. * -is_root: Set to 1 if this node is a root node.
  549. * -is_leaf: Set to 1 if this node is a leaf node.
  550. * -is_internal: Set to 1 if this node is an internal node.
  551. * -left_index: The index of the node to the left in the tree.
  552. * -right_index: The index of the node to the right in the tree.
  553. * -branch_set: An array containing a list of nodes of that are children
  554. * of the node.
  555. * -parent: The name of the parent node.
  556. * -organism_id: The organism_id for associtating the node with an organism.
  557. * -properties: An array of key/value pairs where the key is the cvterm_id
  558. * and the value is the property value. These properties
  559. * will be assocaited with the phylonode.
  560. *
  561. * Prior to importing the tree the indicies can be set by using the
  562. * tripal_assign_phylogeny_tree_indices() function.
  563. *
  564. * @param $tree
  565. * The tree array.
  566. * @param $phylotree.
  567. * The phylotree object (from Chado).
  568. * @param $options
  569. * The options provide some direction for how the tree is imported. The
  570. * following keys can be used:
  571. * -taxonomy: Set to 1 if this tree is a taxonomic tree. Set to 0
  572. * otherwise.
  573. * -leaf_type: Set to the leaf type name. If this is a non-taxonomic tree
  574. * that is associated with features, then this should be the
  575. * Sequence Ontology term for the feature (e.g. polypeptide).
  576. * If this is a taxonomic tree then this option is not needed.
  577. * -match: Set to either 'name' or 'uniquename'. This is used for
  578. * matching the feature name or uniquename with the node name.
  579. * This is not needed for taxonomic trees.
  580. * -match_re: Set to a regular that can be used for matching the node
  581. * name with the feature name if the node name is not
  582. * identical to the feature name.
  583. * @param $vocab
  584. * Optional. An array containing a set of key/value pairs that maps node
  585. * types to CV terms. The keys must be 'root', 'internal' or 'leaf'. If
  586. * no vocab is provded then the terms provided by the tripal_phylogeny
  587. * CV will be used.
  588. * @param $parent
  589. * This argument is not needed when the funtion is first called. This
  590. * function is recursive and this argument is used on recursive calls.
  591. *
  592. * @ingroup tripal_phylotree_api
  593. */
  594. function tripal_phylogeny_import_tree(&$tree, $phylotree, $options, $vocab = array(), $parent = NULL) {
  595. // Get the vocabulary terms used to describe nodes in the tree if one
  596. // wasn't provided.
  597. if (count($vocab) == 0) {
  598. $vocab = tripal_phylogeny_get_node_types_vocab();
  599. }
  600. if (is_array($tree) and array_key_exists('name', $tree)) {
  601. $values = array(
  602. 'phylotree_id' => $phylotree->phylotree_id,
  603. 'left_idx' => $tree['left_index'],
  604. 'right_idx' => $tree['right_index'],
  605. );
  606. // Add in any optional values to the $values array if they are present
  607. if (!empty($tree['name']) and $tree['name'] != '') {
  608. $values['label'] = $tree['name'];
  609. }
  610. if (!empty($tree['length']) and $tree['length'] != '') {
  611. $values['distance'] = $tree['length'];
  612. }
  613. // Set the type of node
  614. if ($tree['is_root']) {
  615. $values['type_id'] = $vocab['root']->cvterm_id;
  616. }
  617. else if ($tree['is_internal']) {
  618. $values['type_id'] = $vocab['internal']->cvterm_id;
  619. $values['parent_phylonode_id'] = $parent['phylonode_id'];
  620. // TOOD: a feature may be associated here but it is recommended that it
  621. // be a feature of type SO:match and should represent the alignment of
  622. // all features beneath it.
  623. }
  624. else if ($tree['is_leaf']) {
  625. $values['type_id'] = $vocab['leaf']->cvterm_id;
  626. $values['parent_phylonode_id'] = $parent['phylonode_id'];
  627. // Match this leaf node with an organism or feature depending on the
  628. // type of tree. But we can't do that if we don't have a name.
  629. if (!empty($tree['name']) and $tree['name'] != '') {
  630. if (!$options['taxonomy']) {
  631. // This is a sequence-based tree. Try to match leaf nodes with features.
  632. // First, Get the Name and uniquename for the feature
  633. $matches = array();
  634. $sel_values = array();
  635. if ($options['match'] == "name") {
  636. $sel_values['name'] = $tree['name'];
  637. $re = $options['name_re'];
  638. if (preg_match("/$re/", $tree['name'], $matches)) {
  639. $sel_values['name'] = $matches[1];
  640. }
  641. }
  642. else {
  643. $sel_values['uniquename'] = $tree['name'];
  644. $re = $options['name_re'];
  645. if (preg_match("/$re/", $tree['name'], $matches)) {
  646. $sel_values['uniquename'] = $matches[1];
  647. }
  648. }
  649. $sel_values['type_id'] = array(
  650. 'name' => $options['leaf_type'],
  651. 'cv_id' => array(
  652. 'name' => 'sequence'
  653. ),
  654. );
  655. $sel_columns = array('feature_id');
  656. $feature = chado_select_record('feature', $sel_columns, $sel_values);
  657. if (count($feature) > 1) {
  658. // Found multiple features, cannot make an association.
  659. }
  660. else if (count($feature) == 1) {
  661. $values['feature_id'] = $feature[0]->feature_id;
  662. }
  663. else {
  664. // Could not find a feature that matches the name or uniquename
  665. }
  666. }
  667. }
  668. }
  669. // Insert the new node and then add it's assigned phylonode_id to the node
  670. $phylonode = chado_insert_record('phylonode', $values);
  671. $tree['phylonode_id'] = $phylonode['phylonode_id'];
  672. // This is a taxonomic tree, so assocaite this node with an
  673. // organism if one is provided.
  674. if (array_key_exists('organism_id', $tree)) {
  675. $values = array(
  676. 'phylonode_id' => $tree['phylonode_id'],
  677. 'organism_id' => $tree['organism_id']
  678. );
  679. $pylonode_organism = chado_insert_record('phylonode_organism', $values);
  680. }
  681. // Associate any properties
  682. if (array_key_exists('properties', $tree)) {
  683. foreach ($tree['properties'] as $type_id => $value) {
  684. $values = array(
  685. 'phylonode_id' => $tree['phylonode_id'],
  686. 'type_id' => $type_id,
  687. 'value' => $value,
  688. );
  689. $pylonode_organism = chado_insert_record('phylonodeprop', $values);
  690. }
  691. }
  692. }
  693. if (is_array($tree) and array_key_exists('branch_set', $tree)) {
  694. foreach ($tree['branch_set'] as $key => $node) {
  695. tripal_phylogeny_import_tree($tree['branch_set'][$key], $phylotree, $options, $vocab, $tree);
  696. }
  697. }
  698. }
  699. /**
  700. *
  701. * @return
  702. *
  703. * @ingroup tripal_phylotree_api
  704. */
  705. function tripal_phylogeny_get_node_types_vocab() {
  706. // Get the vocabulary terms used to describe nodes in the tree
  707. $values = array(
  708. 'name' => 'phylo_leaf',
  709. 'cv_id' => array(
  710. 'name' => 'tripal_phylogeny',
  711. ),
  712. );
  713. $leaf = chado_generate_var('cvterm', $values);
  714. if (!$leaf) {
  715. tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
  716. "Could not find the leaf vocabulary term: 'phylo_leaf'. It should " .
  717. "already be present as part of the tripal_phylogeny vocabulary.");
  718. return FALSE;
  719. }
  720. $values['name'] = 'phylo_interior';
  721. $internal = chado_generate_var('cvterm', $values);
  722. if (!$internal) {
  723. tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
  724. "Could not find the leaf vocabulary term: 'phylo_interior'. It should " .
  725. "already be present as part of the tripal_phylogeny vocabulary.");
  726. return FALSE;
  727. }
  728. $values['name'] = 'phylo_root';
  729. $root = chado_generate_var('cvterm', $values);
  730. if (!$root) {
  731. tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
  732. "Could not find the leaf vocabulary term: 'phylo_root'. It should " .
  733. "already be present as part of the tripal_phylogeny vocabulary.");
  734. return FALSE;
  735. }
  736. $vocab = array(
  737. 'leaf' => $leaf,
  738. 'internal' => $internal,
  739. 'root' => $root,
  740. );
  741. return $vocab;
  742. }
  743. /**
  744. * Imports a tree file.
  745. *
  746. * This function is used as a wrapper for loading a phylogenetic tree using
  747. * any number of file loaders.
  748. *
  749. * @param $file_name
  750. * The name of the file containing the phylogenetic tree to import.
  751. * @param $format
  752. * The format of the file. Currently only the 'newick' file format is
  753. * supported.
  754. * @param $options
  755. * Options if the phylotree record already exists:
  756. * 'phylotree_id': The imported nodes will be associated with this tree.
  757. * 'leaf_type': A sequence ontology term or the word 'organism'. If the
  758. * type is 'organism' then this tree represents a
  759. * taxonomic tree. The default, if not specified, is the
  760. * term 'polypeptide'.
  761. * 'name_re': If the leaf type is NOT 'taxonomy', then the value of
  762. * this field can be a regular expression to pull out
  763. * the name of the feature from the node label in the
  764. * intput tree. If no value is provided the entire label is
  765. * used.
  766. * 'match': Set to 'uniquename' if the leaf nodes should be matched
  767. * with the feature uniquename.
  768. *
  769. * @ingroup tripal_phylotree_api
  770. */
  771. function tripal_phylogeny_import_tree_file($file_name, $format, $options = array(), $job_id = NULL) {
  772. // Set some option details.
  773. if (!array_key_exists('leaf_type', $options)) {
  774. $options['leaf_type'] = 'polypeptide';
  775. }
  776. if (!array_key_exists('match', $options)) {
  777. $options['match'] = 'name';
  778. }
  779. if (!array_key_exists('name_re', $options)) {
  780. $options['name_re'] = '^(.*)$';
  781. }
  782. $options['name_re'] = trim($options['name_re']);
  783. // If a phylotree ID is not passed in then make sure we have the other
  784. // required fields for creating a tree.
  785. if (!array_key_exists('phylotree_id', $options)) {
  786. if (!array_key_exists('name', $options)) {
  787. tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
  788. 'The phylotree_id is required for importing the tree.');
  789. return FALSE;
  790. }
  791. }
  792. // get the phylotree record.
  793. $values = array('phylotree_id' => $options['phylotree_id']);
  794. $phylotree = chado_generate_var('phylotree', $values);
  795. if (!$phylotree) {
  796. tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
  797. 'Could not find the phylotree using the ID provided: %phylotree_id.',
  798. array('%phylotree_id' => $options['phylotree_id']));
  799. return FALSE;
  800. }
  801. $transaction = db_transaction();
  802. print "\nNOTE: Loading of this tree file is performed using a database transaction. \n" .
  803. "If the load fails or is terminated prematurely then the entire set of \n" .
  804. "insertions/updates is rolled back and will not be found in the database\n\n";
  805. try {
  806. // Parse the file according to the format indicated.
  807. if ($format == 'newick') {
  808. // Parse the tree into the expected nested node format.
  809. module_load_include('inc', 'tripal_phylogeny', 'includes/parsers/tripal_phylogeny.newick_parser');
  810. $tree = tripal_phylogeny_parse_newick_file($file_name);
  811. // Assign the right and left indecies to the tree ndoes
  812. tripal_assign_phylogeny_tree_indices($tree);
  813. }
  814. // Iterate through the tree nodes and add them to Chado in accordance
  815. // with the details in the $options array.
  816. tripal_phylogeny_import_tree($tree, $phylotree, $options);
  817. }
  818. catch (Exception $e) {
  819. $transaction->rollback();
  820. watchdog_exception('tripal_phylogeny', $e);
  821. }
  822. }