tripal_phylogeny.drush.inc

  1. 2.x tripal_phylogeny/tripal_phylogeny.drush.inc
  2. 3.x legacy/tripal_phylogeny/tripal_phylogeny.drush.inc

Contains function relating to drush-integration of this module.

File

tripal_phylogeny/tripal_phylogeny.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains function relating to drush-integration of this module.
  5. */
  6. /**
  7. * Describes each drush command implemented by the module
  8. *
  9. * @return
  10. * The first line of description when executing the help for a given command
  11. *
  12. * @ingroup tripal_drush
  13. */
  14. function tripal_phylogeny_drush_help($command) {
  15. switch ($command) {
  16. case 'trp-insert-phylotree':
  17. return dt('Adds a new phylotree record.');
  18. break;
  19. case 'trp-update-phylotree':
  20. return dt('Updates an existing phylotree record.');
  21. break;
  22. case 'trp-delete-phylotree':
  23. return dt('Deletes an existing phylotree record.');
  24. break;
  25. }
  26. }
  27. /**
  28. * Registers a drush command and constructs the full help for that command
  29. *
  30. * @return
  31. * And array of command descriptions
  32. *
  33. * @ingroup tripal_drush
  34. */
  35. function tripal_phylogeny_drush_command() {
  36. $items = array();
  37. $items['trp-insert-phylotree'] = array(
  38. 'description' => dt('Adds a new phylotree record.'),
  39. 'arguments' => array(),
  40. 'examples' => array(),
  41. 'options' => array(
  42. 'username' => array(
  43. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  44. 'required' => TRUE,
  45. ),
  46. 'name' => array(
  47. 'description' => dt('The name of the tree. The name of the tree must be unique.'),
  48. 'required' => TRUE,
  49. ),
  50. 'description' => array(
  51. 'description' => dt('A description for the tree. Use quotes.'),
  52. 'required' => TRUE,
  53. ),
  54. 'analysis' => array(
  55. 'description' => dt('The name of the analysis used to generate this tree. This should be the name of an analysis record already present in Chado. Use quotes.'),
  56. 'required' => TRUE,
  57. ),
  58. 'leaf_type' => array(
  59. 'description' => dt('The Sequence Ontology term for the leaf node type of the tree (e.g. polypeptide). If this is a taxonomic the use the word "taxonomy".'),
  60. 'required' => TRUE,
  61. ),
  62. 'tree_file' => array(
  63. 'description' => dt('The full path to the file containing the tree.'),
  64. 'required' => TRUE,
  65. ),
  66. 'format' => array(
  67. 'description' => dt('The format of the input file. Currently, only the "newick" file format is supported.'),
  68. 'required' => TRUE,
  69. ),
  70. 'dbxref' => dt('A database cross-reference of the form DB:ACCESSION. Where DB is the database name, which is already present in Chado, and ACCESSION is the unique identifier for this tree in the remote database.'),
  71. 'sync' => dt('Set to 1 if this tree should be synced with Drupal.'),
  72. 'match' => dt('Set to "uniquename" if the leaf nodes should be matched with the feature uniquename'),
  73. 'name_re' => dt('If the leaf type is NOT "taxonomy", then this option can be a regular expression used pull out the name of the feature from the node label in theintput tree.'),
  74. ),
  75. );
  76. $items['trp-update-phylotree'] = array(
  77. 'description' => dt('Adds a new phylotree record. If a new file is provided then the entire existing tree will be rebuilt using the file provided.'),
  78. 'arguments' => array(),
  79. 'examples' => array(),
  80. 'options' => array(
  81. 'username' => array(
  82. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  83. 'required' => TRUE,
  84. ),
  85. 'phylotree_id' => dt('The unique phylotree ID assigned within Chado for the tree that should be updated.'),
  86. 'name' => dt('A unique name for the tree. If the phylotree_id is not provided then the phylotree matching this name will be updated.'),
  87. 'description' => dt('A description for the tree. Use quotes.'),
  88. 'analysis' => dt('The name of the analysis used to generate this tree. This should be the name of an analysis record already present in Chado. Use quotes.'),
  89. 'leaf_type' => dt('The Sequence Ontology term for the leaf node type of the tree (e.g. polypeptide). If this is a taxonomic the use the word "taxonomy".'),
  90. 'tree_file' => dt('The full path to the file containing the tree.'),
  91. 'format' => dt('The format of the input file. Currently, only the "newick" file format is supported.'),
  92. 'dbxref' => dt('A database cross-reference of the form DB:ACCESSION. Where DB is the database name, which is already present in Chado, and ACCESSION is the unique identifier for this tree in the remote database.'),
  93. 'sync' => dt('Set to 1 if this tree should be synced with Drupal.'),
  94. 'match' => dt('Set to "uniquename" if the leaf nodes should be matched with the feature uniquename'),
  95. 'name_re' => dt('If the leaf type is NOT "taxonomy", then this option can be a regular expression used pull out the name of the feature from the node label in theintput tree.'),
  96. ),
  97. );
  98. $items['trp-delete-phylotree'] = array(
  99. 'username' => array(
  100. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  101. 'required' => TRUE,
  102. ),
  103. 'description' => dt('Deletes a phylotree record and it\'s corresponding tree nodes.'),
  104. 'arguments' => array(),
  105. 'examples' => array(),
  106. 'options' => array(
  107. 'phylotree_id' => dt('The unique phylotree ID assigned within Chado for the tree that should be deleted.'),
  108. 'name' => dt('If the phylotree_id is not provided then the phylotree matching this name will be deleted.'),
  109. ),
  110. );
  111. return $items;
  112. }
  113. /**
  114. * Deletes a phylotree record.
  115. *
  116. * Executed when 'drush trp-delete-phylotree' is called.
  117. *
  118. * @ingroup tripal_drush
  119. */
  120. function drush_tripal_phylogeny_trp_insert_phylotree() {
  121. $username = drush_get_option('username');
  122. drush_tripal_core_set_user($username);
  123. $options = array(
  124. 'name' => drush_get_option('name'),
  125. 'description' => drush_get_option('description'),
  126. 'analysis' => drush_get_option('analysis'),
  127. 'leaf_type' => drush_get_option('leaf_type'),
  128. 'tree_file' => drush_get_option('tree_file'),
  129. 'format' => drush_get_option('format'),
  130. 'dbxref' => drush_get_option('dbxref'),
  131. 'sync' => drush_get_option('sync'),
  132. 'match' => drush_get_option('match'),
  133. 'name_re' => drush_get_option('name_re'),
  134. 'load_now' => TRUE,
  135. );
  136. $errors = array();
  137. $warnings = array();
  138. if (tripal_insert_phylotree($options, $errors, $warnings)) {
  139. if ($options['sync']) {
  140. chado_node_sync_records('phylotree', FALSE, FALSE,
  141. array(), $ids = array($options['phylotree_id']));
  142. $nid = chado_get_nid_from_id('phylotree', $options['phylotree_id']);
  143. tripal_add_node_variable($nid, 'phylotree_name_re', $options['name_re']);
  144. tripal_add_node_variable($nid, 'phylotree_use_uniquename', $options['match']);
  145. tripal_add_node_variable($nid, 'phylotree_tree_file', basename($options['tree_file']));
  146. }
  147. }
  148. drush_print("Done.");
  149. }
  150. /**
  151. *
  152. */
  153. function drush_tripal_phylogeny_trp_update_phylotree() {
  154. $username = drush_get_option('username');
  155. drush_tripal_core_set_user($username);
  156. $options = array(
  157. 'phylotree_id' => drush_get_option('phylotree_id'),
  158. 'name' => drush_get_option('name'),
  159. 'description' => drush_get_option('description'),
  160. 'analysis' => drush_get_option('analysis'),
  161. 'leaf_type' => drush_get_option('leaf_type'),
  162. 'tree_file' => drush_get_option('tree_file'),
  163. 'format' => drush_get_option('format'),
  164. 'dbxref' => drush_get_option('dbxref'),
  165. 'sync' => drush_get_option('sync'),
  166. 'match' => drush_get_option('match'),
  167. 'name_re' => drush_get_option('name_re'),
  168. 'load_now' => TRUE,
  169. );
  170. if (!$options['phylotree_id'] and $options['name']) {
  171. $phylotree = chado_select_record('phylotree', array('phylotree_id'), array('name' => $options['name']));
  172. if (count($phylotree) > 0) {
  173. $options['phylotree_id'] = $phylotree[0]->phylotree_id;
  174. }
  175. else {
  176. drush_print('A phylotree record with this name does not exists.');
  177. }
  178. }
  179. if($options['phylotree_id'] and tripal_update_phylotree($options['phylotree_id'], $options)) {
  180. if ($options['sync']) {
  181. chado_node_sync_records('phylotree', FALSE, FALSE,
  182. array(), $ids = array($options['phylotree_id']));
  183. }
  184. $nid = chado_get_nid_from_id('phylotree', $options['phylotree_id']);
  185. if ($nid) {
  186. if ($options['name_re']) {
  187. tripal_delete_node_variables($nid, 'phylotree_name_re');
  188. tripal_add_node_variable($nid, 'phylotree_name_re', $options['name_re']);
  189. }
  190. if ($options['match']) {
  191. tripal_delete_node_variables($nid, 'phylotree_use_uniquename');
  192. tripal_add_node_variable($nid, 'phylotree_use_uniquename', $options['match']);
  193. }
  194. if ($options['tree_file']) {
  195. tripal_delete_node_variables($nid, 'phylotree_tree_file');
  196. tripal_add_node_variable($nid, 'phylotree_tree_file', basename($options['tree_file']));
  197. }
  198. }
  199. }
  200. drush_print("Done.");
  201. }
  202. /**
  203. * Deletes a phylotree record.
  204. *
  205. * Executed when 'drush trp-delete-phylotree' is called.
  206. *
  207. * @ingroup tripal_drush
  208. */
  209. function drush_tripal_phylogeny_trp_delete_phylotree() {
  210. $username = drush_get_option('username');
  211. drush_tripal_core_set_user($username);
  212. $options = array(
  213. 'phylotree_id' => drush_get_option('phylotree_id'),
  214. 'name' => trim(drush_get_option('name')),
  215. );
  216. if (!$options['phylotree_id'] and $options['name']) {
  217. $phylotree = chado_select_record('phylotree', array('phylotree_id'), array('name' => $options['name']));
  218. if (count($phylotree) > 0) {
  219. $options['phylotree_id'] = $phylotree[0]->phylotree_id;
  220. }
  221. else {
  222. drush_print('A phylotree record with this name does not exists.');
  223. }
  224. }
  225. if ($options['phylotree_id'] and tripal_delete_phylotree($options['phylotree_id'])) {
  226. chado_cleanup_orphaned_nodes('phylotree');
  227. }
  228. drush_print("Done.");
  229. }