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