-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyzedphenotypes.install
executable file
·330 lines (284 loc) · 10.3 KB
/
analyzedphenotypes.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
<?php
/**
* Implements hook_install().
*/
// Make the API available during install.
// System variables.
module_load_include('inc', 'analyzedphenotypes', 'api/analyzedphenotypes.systemvars.api');
// Default Ontology Terms API.
module_load_include('inc', 'analyzedphenotypes', 'api/analyzedphenotypes.ontology.api');
// Materialized View API.
module_load_include('inc', 'analyzedphenotypes', 'api/analyzedphenotypes.materializedview.api');
// Genus API.
module_load_include('inc', 'analyzedphenotypes', 'api/analyzedphenotypes.genus.api');
// API.
module_load_include('inc', 'analyzedphenotypes', 'api/analyzedphenotypes.api');
/**
* Implements hook_enable().
*/
function analyzedphenotypes_enable() {
// Check we have the phenotype tables we expect to have.
if (!db_table_exists('chado.phenotype')) {
tripal_report_error('analyzedpenotypes', TRIPAL_CRITICAL,
'Analyzed Phenotypes requires the chado.phenotype table to exists. Make sure you have installed chado v1.2+');
return FALSE;
}
if (!db_table_exists('chado.phenotypeprop')) {
tripal_report_error('analyzedpenotypes', TRIPAL_CRITICAL,
'Analyzed Phenotypes requires the chado.phenotypeprop table to exists. Make sure you have installed chado v1.2+');
return FALSE;
}
// Add a materialized view to calculate the mean for quantitative data.
$mviewquery = ap_construct_mview_phenotypesummary_query();
$mviewschema = ap_construct_mview_phenotypesummary_schema();
tripal_add_mview($mviewschema['table'], 'analyzedphenotypes', $mviewschema, $mviewquery, $mviewschema['description'], FALSE);
// Add a summary materialized view.
$query = "
(SELECT organism_genus, 1 as num, 1 as count
FROM chado.mview_phenotype
GROUP BY organism_genus)
UNION
(SELECT organism_genus, 2 as num, count(1)
FROM (SELECT DISTINCT organism_genus, trait_id FROM chado.mview_phenotype) d2
GROUP BY organism_genus)
UNION
(SELECT organism_genus, 3 as num, count(1)
FROM (SELECT DISTINCT organism_genus, experiment_id FROM chado.mview_phenotype) d3
GROUP BY organism_genus)
UNION
(SELECT organism_genus, 4 as num, count(1)
FROM (SELECT DISTINCT organism_genus, stock_id FROM chado.mview_phenotype) d4
GROUP BY organism_genus)
UNION
(SELECT organism_genus, 5 as num, count(1)
FROM chado.mview_phenotype
GROUP BY organism_genus)
";
$schema = array (
'table' => 'mview_phenotype_summary',
'description' => 'A summary of phenotypes.',
'fields' => array (
'organism_genus' => array (
'type' => 'text',
'not null' => true,
),
'column_num' => array (
'type' => 'int',
'unsigned' => true,
'not null' => true,
),
'count' => array (
'type' => 'int',
'unsigned' => true,
'not null' => true,
),
),
);
tripal_add_mview($schema['table'], 'analyzedphenotypes', $schema, $query, $schema['description'], FALSE);
// Extend chado phenotype/phenotypeprop.
$arr_fields = array(
'project_id' => array('table' => 'phenotype', 'ref' => 'project', 'ref_id' => 'project_id'),
'stock_id' => array('table' => 'phenotype', 'ref' => 'stock', 'ref_id' => 'stock_id'),
'cvalue_id' => array('table' => 'phenotypeprop', 'ref' => 'cvterm', 'ref_id' => 'cvterm_id'),
'unit_id' => array('table' => 'phenotype', 'ref' => 'cvterm', 'ref_id' => 'cvterm_id'),
);
foreach($arr_fields as $field => $items) {
$exists = db_field_exists('chado.' . $items['table'], $field);
if (!$exists) {
$sql = sprintf('ALTER TABLE {%s} ADD COLUMN %s integer REFERENCES {%s} (%s)',
$items['table'], $field, $items['ref'], $items['ref_id']);
chado_query($sql);
}
}
// Helpfull guide the admin.
drupal_set_message(t(
'Analyzed Phenotypes needs to be configured <a href="@url">here</a> in order to be specific to your system.',
array('@url' => url('admin/tripal/extension/analyzedphenotypes/settings'))),
'warning');
// Initialize Variables:
// Load default ontology terms for method, location, replicate, collector, genus, related and year
// in AP configuration page.
$default_ontology = ap_load_defaultontology();
// All system variables, groupped by options, cvdbon and terms.
$sysvars = ap_construct_variablenames();
// Set default.
// Terms.
foreach($sysvars['terms'] as $v => $var) {
// Set the variable to default ontology term.
// base terms: location, replicate, year ...
$v = trim(str_replace('ap_', '', $v));
variable_set($var, $default_ontology[$v]);
}
$has_organism = ap_get_genus();
if ($has_organism) {
// Other system variables.
// Genus - cv, db and ontology.
foreach($sysvars['cvdbon'] as $v => $var) {
foreach($var as $h) {
variable_set($h, 'not set');
}
}
}
// Allow new traits.
// Default to allow new traits to be added.
foreach($sysvars['options'] as $v => $var) {
variable_set($var, 1);
}
$directories = ap_construct_datafile_destinationpath();
foreach($directories as $dir) {
file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
}
}
/**
* Implements hook_uninstall().
*/
function analyzedphenotypes_uninstall() {
$sysvars = ap_construct_variablenames();
foreach($sysvars as $i => $vars) {
foreach($vars as $var_name) {
if ($i == 'cvdbon') {
foreach($var_name as $cvdbon => $var_name2) {
variable_del($var_name2);
}
}
else {
variable_del($var_name);
}
}
}
$mviews = array('mview_phenotype', 'mview_phenotype_summary');
foreach($mviews as $mview) {
$mview_id = tripal_get_mview_id($mview);
if (isset($mview_id) && $mview_id > 0) {
tripal_delete_mview($mview_id);
}
}
}
/**
* Implements hook_requirements().
*/
function analyzedphenotypes_requirements($phase) {
$requirements = array();
if ($phase == 'install') {
// Check D3 and Excel file writer.
$arr_libraries = array(
'D3' => array(
'is_in' => libraries_get_path('d3'),
'source' => 'https://github.com/tripal/tripald3',
'path' => 'i.e., sites/all/libraries/d3',
),
'Excel Writer' => array(
'is_in' => libraries_get_path('PHP_XLSXWriter_plus'),
'source' => 'https://github.com/SystemDevil/PHP_XLSXWriter_plus',
'path' => 'i.e., sites/all/libraries/PHP_XLSXWriter_plus',
)
);
foreach($arr_libraries as $i => $info) {
if (!$info['is_in']) {
$requirements[$i] = array(
'severity' => REQUIREMENT_ERROR,
'description' =>
t('This module uses !library, which is missing in your Drupal site. Please download and extract the entire contents of the archive into Libraries Directory %path directory on your server.',
array('!library' => l(strtoupper(str_replace('_',' ',$i)), $info['source'], array('attributes' => array('target' => '_blank'))), '%path' => $info['path'])),
);
}
}
$directories = ap_construct_datafile_destinationpath();
foreach($directories as $dir) {
$dir_exists = file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
if (!$dir_exists) {
$requirements['file_directory' . $dir] = array(
'severity' => REQUIREMENT_ERROR,
'description' => t('File directory !dir is not writable. Please update directory permission and try again.',
array('!dir' => $dir)
)
);
}
}
}
return $requirements;
}
/**
* Implements hook_schema().
*/
function analyzedphenotypes_schema() {
$tables = [];
$tables['analyzedphenotypes_collections'] = [
'description' => 'Keeps track of phenotypic data collections; specifically, options for display.',
'fields' => [
'collection_id' => [
'description' => 'The primary identifier for a phenotypic data collection.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE
],
'genus' => [
'description' => 'The genus of the germplasm this data was taken on.',
'type' => 'varchar',
'not null' => TRUE,
],
'trait_id' => [
'description' => 'The Trait (e.g. plant height).',
'type' => 'int',
'not null' => TRUE,
],
'method_id' => [
'description' => 'The Method (e.g. stretched primary stem).',
'type' => 'int',
'not null' => TRUE,
],
'unit_id' => [
'description' => 'The Unit (e.g. centimeters).',
'type' => 'int',
'not null' => TRUE,
],
'project_id' => [
'description' => 'The Experiment',
'type' => 'int',
'not null' => TRUE,
],
'chart_type' => [
'description' => 'The type of chart which should be used for display (e.g. "multibar", "violin").',
'type' => 'varchar',
'not null' => TRUE,
],
],
'unique keys' => [
'composite_key' => ['trait_id', 'method_id', 'unit_id', 'project_id'],
],
'indexes' => [],
'primary key' => ['collection_id'],
];
return $tables;
}
/**
* Create analyzedpenotypes_collections table and populate defaults.
*/
function analyzedphenotypes_update_7100() {
// Load the schema from mymodule_schema() so you don't have to duplicate code.
$schema = analyzedphenotypes_schema();
// Pass the table name and schema for that table to db_create_table()
db_create_table(
'analyzedphenotypes_collections',
$schema['analyzedphenotypes_collections']
);
// Populate the table for pre-existing collections.
$sql = "SELECT
pp.value as genus,
p.attr_id as trait_id,
p.assay_id as method_id,
p.unit_id,
p.project_id,
CASE
WHEN (up.value = 'quantitative') THEN 'violin'
ELSE 'multibar' END
as chart_type
FROM chado.phenotype p
LEFT JOIN chado.projectprop pp ON pp.project_id=p.project_id
AND pp.type_id IN (SELECT cvterm_id FROM chado.cvterm WHERE name='genus')
LEFT JOIN chado.cvtermprop up ON up.cvterm_id=p.unit_id
AND up.type_id IN (SELECT cvterm_id FROM chado.cvterm WHERE name='additionalType')
GROUP BY p.attr_id, p.assay_id, p.unit_id, p.project_id, pp.value, up.value";
db_query('INSERT INTO {analyzedphenotypes_collections}
(genus, trait_id, method_id, unit_id, project_id, chart_type)' . $sql);
}