-
Notifications
You must be signed in to change notification settings - Fork 0
/
dh_ecology.install
431 lines (396 loc) · 13.3 KB
/
dh_ecology.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
<?php
function dh_ecology_schema() {
//
// temp//
if (db_field_exists('dh_ecology_isolate', 'sequence')) {
$seqcol = 'sequence';
} else {
$seqcol = 'its_sequence';
}
$schema['dh_ecology_isolate'] = array (
'description' => 'Repository of plant sample DNA isolate',
'fields' => array(
'iid' => array(
'description' => 'Isolate ID - Primary key for Variables',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'source' => array(
'description' => 'Data Source (lab or collector)',
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
'default' => '',
),
'isolate' => array(
'description' => 'Isolate Unique Text Identifier',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
),
'species' => array(
'description' => 'Species',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
),
'genus' => array(
'description' => 'Genus',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
),
'host_organism' => array(
'description' => 'Host',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
),
'acquisition_num' => array(
'description' => 'Acquisition Number (Gen Bank)',
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
'default' => '',
),
'primer_f' => array(
'description' => 'Primer (forward)',
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
'default' => '',
),
'primer_r' => array(
'description' => 'Primer (reverse)',
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
'default' => '',
),
$seqcol => array(
'description' => 'DNA Sequence',
'type' => 'text',
'size' => 'big',
'not null' => FALSE,
'default' => '',
'translatable' => false,
),
'ecology' => array(
'description' => 'Ecology Type (pathogen/non-pathogen/unknown))) ',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => 'unknown',
),
'sequence_location' => array(
'description' => 'Sequence Location ',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
'default' => 'undefined',
),
'modified' => array(
'description' => 'Type Specimen',
'type' => 'int',
'not null' => FALSE,
'size' => 'small',
),
'modified' => array(
'description' => 'Modified Date',
'type' => 'int',
'not null' => FALSE,
'size' => 'big',
),
'bundle' => array(
'description' => 'Isolate Type / Location / Bundle (machine name)) ',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => 'dh_ecology_isolate',
),
'type_specimen' => array(
'description' => 'Type Specimen',
'type' => 'int',
'not null' => FALSE,
'size' => 'small',
),
'uid' => array(
'description' => 'Record Owner',
'type' => 'int',
'not null' => FALSE,
'size' => 'big',
),
) + entity_exportable_schema_fields(),
'primary key' => array('iid'),
'indexes' => array(
'de_iso_iix' => array('iid'),
'de_iso_isix' => array('isolate'),
'de_iso_six' => array('source'),
'de_iso_spix' => array('species'),
'de_iso_aix' => array('acquisition_num'),
'de_iso_itsix' => array($seqcol),
)
);
if (db_field_exists('dh_ecology_isolate', 'sequence_location')) {
$schema['dh_ecology_isolate']['sequence_location'] = array(
'description' => 'Sequence Location ',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
'default' => 'undefined',
);
}
$schema['dh_ecology_isolate_type'] = array (
'description' => 'Repository of plant sample DNA isolate',
'fields' => array(
'itid' => array(
'description' => 'Isolate Type ID - Primary key',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'name' => array(
'description' => 'Isolate Type Name ',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
),
'bundle' => array(
'description' => 'Isolate Type / Bundle (machine name)) ',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => 'dh_ecology_isolate',
),
'description' => array(
'description' => 'Description of this Isolate Class',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
) + entity_exportable_schema_fields(),
'primary key' => array('itid'),
'indexes' => array(
'de_isot_iix' => array('itid'),
'de_isot_bix' => array('bundle'),
)
);
return $schema;
}
function dh_ecology_uninstall() {
}
function dh_ecology_references() {
$refdefs = array();
$feature_bundles = dh_feature_get_types();
// add references
$refdefs[] = array(
'fieldname' => 'dh_link_isolate_mps',
'src_type' => 'dh_ecology_isolate',
'src_bundle' => 'dh_ecology_isolate',
'dest_type' => 'dh_feature',
// could link to all features but not yet implemented
//'dest_bundle' => $feature_bundles,
'dest_bundle' => array('plant_tissue_sample'),
'label' => 'Source Plant Tissue Sample',
'select_type' => 'entityreference_autocomplete'
);
// link sample point to submittal
$refdefs[] = array(
'fieldname' => 'dh_link_feature_submittal',
'src_type' => 'dh_adminreg_feature',
'src_bundle' => 'submittal',
'dest_type' => 'dh_feature',
'dest_bundle' => 'plant_tissue_sample',
'label' => 'Link Submittal Record to dH Feature',
'select_type' => 'entityreference_autocomplete'
);
return $refdefs;
}
function dh_ecology_install() {
// shared ID key
//$thistable = 'dh_ecology_isolate';
//db_query("alter table {$thistable} alter column iid set default nextval('{dh_feature}_hydroid_seq')");
// hack to make this work
//db_query("insert into {dh_adminreg_feature} (admincode,name,bundle,status) values('dummy','dummy','facility','expired'); ");
// add bundles
dh_ecology_add_bundles();
dh_ecology_isolate_add_bundles();
//dh_ecology_add_views();
// add references
$refdefs = dh_ecology_references();
dh_add_references($refdefs);
// set up fields here
$fields_fields = array(
'plant_tissue_sample' => array(
'dh_geofield' => array('label' => 'Geometry', 'type' => 'geofield', 'display' => 'geofield_wkt', 'widget' => 'geofield_wkt'),
),
);
foreach ($fields_fields as $key => $thistype) {
dh_initialize_fields($key, '', 'dh_feature', $fields_fields);
}
}
function dh_ecology_add_bundles() {
$typedefs = array(
'plant_tissue_sample' => array('bundle' => 'plant_tissue_sample', 'name' => 'Plant Tissue Sample', 'description' => 'dH Ecology Plant Tissue Sample and Site'),
);
foreach ($typedefs as $key => $thistype) {
dh_base_feature_type_save($thistype);
//dh_initialize_fields($key);
}
}
function dh_ecology_isolate_add_bundles() {
$iso_types = dh_define_isolate_bundles();
foreach ($iso_types as $tn => $thistype) {
if ($e = entity_create('dh_ecology_isolate_type', $thistype)) {
$e->save();
} else {
drupal_set_message("Failed adding bundle $tn - $e: </pre>" . print_r($thistype,1) . "</pre>");
}
}
}
function dh_define_isolate_bundles() {
$typedefs = array(
'dh_ecology_isolate' => array('bundle' => 'dh_ecology_isolate', 'name' => 'Default Isolate Type', 'description' => 'dH Basic Isolate'),
'its' => array('bundle' => 'its', 'name' => 'ITS Sequence', 'description' => 'ITS Sequence'),
);
return $typedefs;
}
function dh_ecology_isolate_type_save($info) {
// set the base dh_ecology_isolate_type entries
// modeled after node_type_save
$existing_type = !empty($info['old_bundle']) ? $info['old_bundle'] : $info['bundle'];
$is_existing = (bool) db_query_range('SELECT 1 FROM {dh_ecology_isolate_type} WHERE bundle = :bundle', 0, 1, array(':bundle' => $existing_type))->fetchField();
$fields = array(
'bundle' => $info['bundle'],
'name' => $info['name'],
'description' => $info['description'],
);
if ($is_existing) {
error_log("Updating an existing bundle");
db_update('dh_ecology_isolate_type')
->fields($fields)
->condition('bundle', $existing_type)
->execute();
if (!empty($info['old_bundle']) && $info['old_bundle'] != $info['bundle']) {
field_attach_rename_bundle('dh_ecology_isolate', $info['old_bundle'], $info['bundle']);
}
// need equivalent?
//module_invoke_all('node_type_update', $type);
$status = SAVED_UPDATED;
}
else {
db_insert('dh_ecology_isolate_type')
->fields($fields)
->execute();
field_attach_create_bundle('dh_ecology_isolate', $info['bundle']);
$status = SAVED_NEW;
}
// Clear the node type cache.
// hmmm ... do we need to supply an equivalent to this?
// node_type_cache_reset();
dh_ecology_isolate_type_cache_reset();
return $status;
}
function dh_ecology_isolate_type_cache_reset() {
cache_clear_all('dh_ecology_isolate_type:', 'cache', TRUE);
//drupal_static_reset('_node_types_build');
}
function dh_ecology_define_views() {
$views = array();
/*
$views['dh_views_last_weather24'] = " create or replace view {dh_views_last_weather24} as ";
$views['dh_views_last_weather24'] .= " select a.featureid, ";
$views['dh_views_last_weather24'] .= " a.tstime as tsendtime, min(b.tstime) as tstime, ";
$views['dh_views_last_weather24'] .= " sum(b.rain) as rain, sum(b.wet_min) as wet_min, ";
$views['dh_views_last_weather24'] .= " avg(b.temp) as temp, avg(b.rh) as rh ";
$views['dh_views_last_weather24'] .= " from ( ";
$views['dh_views_last_weather24'] .= " select a.featureid, max(a.tstime) as tstime ";
$views['dh_views_last_weather24'] .= " from {dh_timeseries_weather} as a ";
$views['dh_views_last_weather24'] .= " left outer join {dh_variabledefinition} as b ";
$views['dh_views_last_weather24'] .= " on ( a.varid = b.varid and b.varkey = 'weather_obs' ) ";
$views['dh_views_last_weather24'] .= " where b.varid is not null ";
$views['dh_views_last_weather24'] .= " ) as a ";
*/
return $views;
}
function dh_ecology_add_views($views = array()) {
$allviews = dh_ecology_define_views();
if (count($views) == 0) {
$views = array_keys($allviews);
}
foreach ($views as $viewname => $viewsql) {
db_query( $viewsql );
}
}
function dh_ecology_update_7101() {
// this seems not to be needed as submittal can connect to any feature
/*
$refdefs = array();
// link sample point to submittal
$refdefs[] = array(
'fieldname' => 'dh_link_feature_submittal',
'src_type' => 'dh_adminreg_feature',
'src_bundle' => 'submittal',
'dest_type' => 'dh_feature',
'dest_bundle' => 'plant_tissue_sample',
'label' => 'Link Submittal Record to dH Feature',
'select_type' => 'entityreference_autocomplete'
);
dh_add_references($refdefs, false);
*/
}
function dh_ecology_update_7102() {
db_query("alter table {dh_ecology_isolate} add column bundle varchar(64) default 'dh_ecology_isolate'");
}
function dh_ecology_update_7103() {
$schema = dh_ecology_schema();
if (!db_table_exists('dh_ecology_isolate_type')) {
db_create_table('dh_ecology_isolate_type', $schema['dh_ecology_isolate_type']);
}
if (!db_field_exists('dh_ecology_isolate', 'bundle')) {
db_query("alter table {dh_ecology_isolate} add column bundle varchar(64) default 'dh_ecology_isolate'");
}
db_query("alter table {dh_ecology_isolate} alter column bundle set default 'dh_ecology_isolate'");
}
function dh_ecology_update_7105() {
$schema = dh_ecology_schema();
if (!db_table_exists('dh_ecology_isolate_type')) {
db_create_table('dh_ecology_isolate_type', $schema['dh_ecology_isolate_type']);
}
}
function dh_ecology_update_7108() {
dh_ecology_isolate_add_bundles();
}
function dh_ecology_update_7109() {
// rename its_sequence to sequence
if (db_field_exists('dh_ecology_isolate', 'its_sequence')) {
db_query("alter table {dh_ecology_isolate} rename column its_sequence TO sequence ");
}
// sequence_location
db_query("alter table {dh_ecology_isolate} add column sequence_location varchar(64) default 'unknown' ");
// create bundle its_sequence - sequence_location = its
$tn = 'its';
$thistype = array('bundle' => 'its', 'name' => 'ITS Sequence', 'description' => 'ITS Sequence');
if ($e = entity_create('dh_ecology_isolate_type', $thistype)) {
$e->save();
} else {
drupal_set_message("Failed adding bundle $tn - $e: </pre>" . print_r($thistype,1) . "</pre>");
}
}
function dh_ecology_update_7110() {
$schema = dh_ecology_schema();
$uid = $schema['dh_ecology_isolate']['fields']['uid'];
dpm($uid, 'Adding uid field');
db_add_field( 'dh_ecology_isolate', 'uid', $uid);
}
function dh_ecology_update_7111() {
// type_specimen
db_query("alter table {dh_ecology_isolate} add column type_specimen INTEGER default 0 ");
}