tripal_phylogeny.drush.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 = [];
  37. $items['trp-insert-phylotree'] = [
  38. 'description' => dt('Adds a new phylotree record.'),
  39. 'arguments' => [],
  40. 'examples' => [],
  41. 'options' => [
  42. 'username' => [
  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' => [
  47. 'description' => dt('The name of the tree. The name of the tree must be unique.'),
  48. 'required' => TRUE,
  49. ],
  50. 'description' => [
  51. 'description' => dt('A description for the tree. Use quotes.'),
  52. 'required' => TRUE,
  53. ],
  54. 'analysis' => [
  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' => [
  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' => [
  63. 'description' => dt('The full path to the file containing the tree.'),
  64. 'required' => TRUE,
  65. ],
  66. 'format' => [
  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'] = [
  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' => [],
  79. 'examples' => [],
  80. 'options' => [
  81. 'username' => [
  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'] = [
  99. 'username' => [
  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' => [],
  105. 'examples' => [],
  106. 'options' => [
  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 = [
  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 = [];
  137. $warnings = [];
  138. if (tripal_insert_phylotree($options, $errors, $warnings)) {
  139. if ($options['sync']) {
  140. chado_node_sync_records('phylotree', FALSE, FALSE,
  141. [], $ids = [$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 = [
  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', ['phylotree_id'], ['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 exist.');
  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. [], $ids = [$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 = [
  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', ['phylotree_id'], ['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 exist.');
  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. }