forked from legumeinfo/tripal_phylotree
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tripal_phylotree.install
506 lines (472 loc) · 14.3 KB
/
tripal_phylotree.install
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<?php
/**
* @file
* Installation of the phylotree module
*/
/**
* Implements hook_install().
*
* @ingroup tripal_phylotree
*/
function tripal_phylotree_install() {
// Create the module's data directory.
tripal_create_files_dir('tripal_phylotree');
// Add the vocabularies used by the feature module.
tripal_phylotree_add_cvterms();
// Set the default vocabularies.
tripal_set_default_cv('phylonode', 'type_id', 'tripal_phylogeny');
tripal_set_default_cv('phylotree', 'type_id', 'sequence');
// Add the materializedviews.
tripal_phylotree_add_mview();
// We want to integrate the materialized views so that they
// are available for Drupal Views, upon which our search forms are built.
tripal_phylotree_integrate_view();
$mview_id = tripal_get_mview_id('phylotree_count');
// SPF: commented out automatic populate of MView. If the query fails
// for some reason the install will not properly complete.
// tripal_populate_mview($mview_id);
// Add the custom tables.
tripal_phylotree_add_custom_tables();
// Add an index on the phylonode table.
$exists = chado_index_exists('phylonode', 'parent_phylonode_id');
if (!$exists) {
chado_add_index('phylonode', 'parent_phylonode_id', array('parent_phylonode_id'));
}
// Add in the variables that this module will use to store properties for
// loading of the tree files.
tripal_insert_variable('phylotree_name_re', 'The regular expression for matching a name in a string.');
tripal_insert_variable('phylotree_use_uniquename', 'Set to 1 if features should be matched using the unqiuename rather than the name.');
tripal_insert_variable('phylotree_tree_file', 'Holds the Drupal file ID for the uploaded tree file.');
}
/**
* Implements hook_disable().
*
* Disable default views when module is disabled
*
* @ingroup tripal_phylotree
*/
function tripal_phylotree_disable() {
// Disable all default views provided by this module
require_once("tripal_phylotree.views_default.inc");
$views = tripal_phylotree_views_default_views();
foreach (array_keys($views) as $view_name) {
tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
}
}
/**
* Implementation of hook_requirements().
*
* @ingroup tripal_phylotree
*/
function tripal_phylotree_requirements($phase) {
$requirements = array();
if ($phase == 'install') {
// Make sure chado is installed.
if (!$GLOBALS["chado_is_installed"]) {
$requirements ['tripal_phylotree'] = array(
'title' => "tripal_phylotree",
'value' => "ERROR: Chado must be installed before this module can be enabled",
'severity' => REQUIREMENT_ERROR,
);
}
}
return $requirements;
}
/**
* Implementation of hook_schema().
* Standard tripal linker table between a node and a phylotree record.
* @ingroup tripal_phylotree
*/
function tripal_phylotree_schema() {
$schema['chado_phylotree'] = array(
'fields' => array(
'vid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'phylotree_id' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0
)
),
'indexes' => array(
'chado_phylotree_idx1' => array('phylotree_id')
),
'unique keys' => array(
'chado_phylotree_uq1' => array('nid', 'vid'),
'chado_phylotree_uq2' => array('vid')
),
'primary key' => array('nid'),
);
return $schema;
}
/**
* Adds controlled vocabulary terms needed by this module.
*
* @ingroup tripal_phylotree
*/
function tripal_phylotree_add_cvterms() {
tripal_insert_cv(
'tripal_phylogeny',
'Terms used by the Tripal phylotree module for phylogenetic and taxonomic trees.'
);
// Add the terms used to identify nodes in the tree.
tripal_insert_cvterm(
array(
'name' => 'phylo_leaf',
'definition' => 'A leaf node in a phylogenetic tree.',
'cv_name' => 'tripal_phylogeny',
'is_relationship' => 0,
'db_name' => 'tripal'
),
array('update_existing' => TRUE)
);
// Add the terms used to identify nodes in the tree.
tripal_insert_cvterm(
array(
'name' => 'phylo_root',
'definition' => 'The root node of a phylogenetic tree.',
'cv_name' => 'tripal_phylogeny',
'is_relationship' => 0,
'db_name' => 'tripal'
),
array('update_existing' => TRUE)
);
// Add the terms used to identify nodes in the tree.
tripal_insert_cvterm(
array(
'name' => 'phylo_interior',
'definition' => 'An interior node in a phylogenetic tree.',
'cv_name' => 'tripal_phylogeny',
'is_relationship' => 0,
'db_name' => 'tripal'
),
array('update_existing' => TRUE)
);
// Add the terms used to identify nodes in the tree.
tripal_insert_cvterm(
array(
'name' => 'taxonomy',
'definition' => 'A term used to indicate if a phylotree is a taxonomic tree',
'cv_name' => 'tripal_phylogeny',
'is_relationship' => 0,
'db_name' => 'tripal'
),
array('update_existing' => TRUE)
);
// ----------------------------------------------
// Add the terms for importing NCBI taxonomy data
$values = array(
'name' => 'taxonomy',
'description' => 'The NCBI Taxonomy Database is a curated classification and nomenclature for all of the organisms in the public sequence databases. This currently represents about 10% of the described species of life on the planet.',
'url' => 'http://www.ncbi.nlm.nih.gov/taxonomy',
'urlprefix' => 'http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=',
);
tripal_insert_db($values);
tripal_insert_cv('taxonomy', 'A taxonomy for NCBI taxonomy ranks');
$terms = array('rank', 'taxid', 'class', 'cohort', 'family', 'forma', 'genus', 'infraclass',
'infraorder', 'kingdom', 'no rank', 'order', 'parvorder', 'phylum',
'species', 'species group', 'species subgroup', 'subclass', 'subcohort',
'subfamily', 'subgenus', 'subkingdom', 'suborder', 'subphylum',
'subspecies', 'subtribe', 'superclass', 'superfamily', 'superkingdom',
'superorder', 'superphylum', 'tribe', 'varietas');
$options = array('update_existing' => TRUE);
foreach ($terms as $term) {
$value = array(
'name' => $term,
'definition' => '',
'cv_name' => 'taxonomy',
'is_relationship' => 0,
'db_name' => 'taxonomy'
);
tripal_insert_cvterm($value, $options);
}
$terms = array(
'lineage',
'genetic_code',
'genetic_code_name',
'mitochondrial_genetic_code',
'mitochondrial_genetic_code_name',
'division',
'genbank_common_name',
'synonym',
'other_name',
'equivalent_name',
'anamorph'
);
$options = array('update_existing' => TRUE);
foreach ($terms as $term) {
$value = array(
'name' => $term,
'definition' => '',
'cv_name' => 'organism_property',
'is_relationship' => 0,
'db_name' => 'tripal'
);
tripal_insert_cvterm($value, $options);
}
}
/**
* Implementation of hook_uninstall().
*/
function tripal_phylotree_uninstall() {
// Drop the MView table if it exists
$mview_id = tripal_get_mview_id('phylotree_count');
if ($mview_id) {
tripal_delete_mview($mview_id);
}
// Remove views integration.
// Note: tripal_remove_views_intergration accepts table_name and priority in a key value form.
$delete_view=array(
'table_name' => 'phylotree_count',
'priority' => '-1',
);
tripal_remove_views_integration($delete_view);
}
function tripal_phylotree_add_mview(){
// Materialized view addition.
$sql_query="
WITH count_genes as
(SELECT count(*) count, t.phylotree_id
FROM phylotree t, phylonode n
WHERE n.phylotree_id = t.phylotree_id AND n.label is NOT NULL
GROUP BY t.phylotree_id)
SELECT
phylotree.phylotree_id AS phylotree_phylotree_id,
phylotree.name AS phylotree_name,
phylotree.comment AS phylotree_comment,
count_genes.count AS total_count
FROM chado.phylotree phylotree
LEFT JOIN chado_phylotree chado_phylotree ON phylotree.phylotree_id = chado_phylotree.phylotree_id
LEFT JOIN count_genes count_genes ON phylotree.phylotree_id = count_genes.phylotree_id
";
// Table Phylotree User Search description.
$schema = array (
'table' => 'phylotree_count',
'fields' => array(
'phylotree_phylotree_id' => array(
'type' => 'int',
'not null' => FALSE,
),
'phylotree_name' => array(
'type' => 'text',
'not null' => FALSE,
),
'phylotree_comment' => array(
'type' => 'text',
'not null' => FALSE,
),
'total_count' => array(
'type' => 'int',
'not null' => TRUE,
),
),
'primary key' => array('phylotree_phylotree_id'),
);
// Add a comment to make sure this view makes sense to the site administator.
$comment = t('This view is used to provide a table for Phylotree User Search with total count.');
tripal_add_mview('phylotree_count', 'tripal_phylotree', $schema, $sql_query, $comment);
}
/**
* Integrate the qtl_search materialized view for use by Drupal Views and
* our search form
*/
function tripal_phylotree_integrate_view(){
$integration = array (
'table' => 'phylotree_count',
'name' => 'phylotree_count',
'type' => 'chado',
'description' => '',
'priority' => '-1',
'base_table' => '1',
'fields' => array (
'phylotree_phylotree_id' => array (
'name' => 'phylotree_phylotree_id',
'title' => 'Phylotree ID',
'description' => 'Phylotree ID',
'type' => 'int',
'handlers' => array (
'filter' => array (
'name' => 'views_handler_filter_numeric'
),
'field' => array (
'name' => 'views_handler_field_numeric'
),
'sort' => array (
'name' => 'views_handler_sort'
),
'argument' => array (
'name' => 'views_handler_argument_numeric'
),
'relationship' => array (
'name' => 'views_handler_relationship'
)
),
'joins' => array ()
),
'phylotree_name' => array (
'name' => 'phylotree_name',
'title' => 'Family ID',
'description' => 'Family ID',
'type' => 'text',
'handlers' => array (
'filter' => array (
'name' => 'tripal_views_handler_filter_select_string'
),
'field' => array (
'name' => 'views_handler_field'
),
'sort' => array (
'name' => 'views_handler_sort'
),
'argument' => array (
'name' => 'views_handler_argument_string'
),
'relationship' => array (
'name' => 'views_handler_relationship'
)
),
'joins' => array ()
),
'phylotree_comment' => array (
'name' => 'phylotree_comment',
'title' => 'Description',
'description' => 'Description',
'type' => 'text',
'handlers' => array (
'filter' => array (
'name' => 'tripal_views_handler_filter_select_string'
),
'field' => array (
'name' => 'views_handler_field'
),
'sort' => array (
'name' => 'views_handler_sort'
),
'argument' => array (
'name' => 'views_handler_argument_string'
),
'relationship' => array (
'name' => 'views_handler_relationship'
)
),
'joins' => array ()
),
'total_count' => array (
'name' => 'total_count',
'title' => 'Total count',
'description' => 'Total count',
'type' => 'int',
'handlers' => array (
'filter' => array (
'name' => 'views_handler_filter_numeric'
),
'field' => array (
'name' => 'views_handler_field'
),
'sort' => array (
'name' => 'views_handler_sort'
),
'argument' => array (
'name' => 'views_handler_argument_numeric'
),
'relationship' => array (
'name' => 'views_handler_relationship'
)
),
'joins' => array ()
)
)
);
// Add the array above that will integrate our qtl_search materialized view
// for use with Drupal Views.
tripal_add_views_integration($integration);
}
/**
* Add any custom tables needed by this module.
* - phylotreeprop: keep track of properties of phylotree
*
* @ingroup tripal_phylotree
*/
function tripal_phylotree_add_custom_tables() {
$schema = array (
'table' => 'phylotreeprop',
'fields' => array (
'phylotreeprop_id' => array (
'type' => 'serial',
'not null' => true,
),
'phylotree_id' => array (
'type' => 'int',
'not null' => true,
),
'type_id' => array (
'type' => 'int',
'not null' => true,
),
'value' => array (
'type' => 'text',
'not null' => false,
),
'rank' => array (
'type' => 'int',
'not null' => true,
'default' => 0,
),
),
'primary key' => array (
0 => 'phylotreeprop_id',
),
'unique keys' => array (
'phylotreeprop_c1' => array (
0 => 'phylotree_id',
1 => 'type_id',
2 => 'rank',
),
),
'indexes' => array (
'phylotreeprop_idx1' => array (
0 => 'phylotree_id',
),
'phylotreeprop_idx2' => array (
0 => 'type_id',
),
),
'foreign keys' => array (
'cvterm' => array (
'table' => 'cvterm',
'columns' => array (
'type_id' => 'cvterm_id',
),
),
'phylotree' => array (
'table' => 'phylotree',
'columns' => array (
'phylotree_id' => 'phylotree_id',
),
),
),
);
chado_create_custom_table('phylotreeprop', $schema, TRUE);
}
/**
* Adds the vocabulary needed for storing taxonmies.
*/
function tripal_phylotree_update_7200() {
try {
tripal_phylotree_add_cvterms();
}
catch (\PDOException $e) {
$error = $e->getMessage();
throw new DrupalUpdateException('Failed to complete update' . $error);
}
}