-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrawpheno.install
executable file
·1345 lines (1207 loc) · 45.4 KB
/
rawpheno.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Contains all schema api and hook_install()/hook_uninstall()
* implementations required by this module.
*/
/**
* Implements hook_schema().
*/
function rawpheno_schema() {
// PHENO PLANT
// Since the plant can be described in many different ways,
// we are using a prop-like table to describe the plant.
// This allows for the flexibility needed to describe both
// greenhouse and field pots.
$schema['pheno_plant'] = array(
'description' => t('Table for phenotype plant.'),
'fields' => array(
'plant_id' => array(
'description' => t('A unique ID for each row'),
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'stock_id' => array(
'description' => t('A unique ID for a stock name.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,),
),
);
// PHENO PLANT PROP
// Type should include entry, plot, rep and location.
$schema['pheno_plantprop'] = array(
'description' => t('Table for phenotype plantprop.'),
'fields' => array(
'plantprop_id' => array(
'description' => t('Primary key: A unique ID for each plantprop record.'),
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE
),
'plant_id' => array(
'description' => t('Holds plant ID number.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'type_id' => array(
'description' => t('Term ID number of trait inserted.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE
),
'value' => array(
'description' => t('The value of the trait measured.'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE
,)
,),
'primary key' => array('plantprop_id'),
'foreign keys' => array(
'pheno_plant' => array(
'table' => 'pheno_plant',
'columns' => array('plant_id' => 'plant_id')),),
);
// PHENO SCALE MEMBER
// The scale code is what is written in the spreadsheet,
// whereas the value is what it actually represents.
$schema['pheno_scale_member'] = array(
'description' => t('Table for pheno scale member.'),
'fields' => array(
'member_id' => array(
'description' => t('Primary key: A unique ID for each scale value.'),
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'scale_id' => array(
'description' => t('Scale ID number of a scale value.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'code' => array(
'description' => t('Corresponding code for each scale ID.'),
'type' => 'varchar',
'length' => 255,
),
'value' => array(
'description' => t('Description of a scale value.'),
'type' => 'text',
'not null' => TRUE
,)
,),
'primary key' => array('member_id'),
);
// PHENO MEASUREMENTS
// Modified is the timestamp that it was last modified.
// This allows us to determine when values were updated.
$schema['pheno_measurements'] = array(
'description' => t('Table for phenotype measurements'),
'fields' => array(
'measurement_id' => array(
'description' => t('Primary key: A unique ID for each trait measured.'),
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'plant_id' => array(
'description' => t('Holds plant ID number.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'type_id' => array(
'description' => t('Term ID number of trait inserted.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'unit_id' => array(
'description' => t('Unit ID number of trait inserted.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'value' => array(
'description' => t('The value of the trait measured.'),
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
'cvalue_id' => array(
'description' => t('Scale member code number when unit is scale.'),
'type' => 'varchar',
'length' => 10,
'not null' => FALSE,
),
'modified' => array(
'description' => t('timestamp of the last modification.'),
'type' => 'varchar',
'length' => 30,
'not null' => TRUE,
)
,),
'primary key' => array('measurement_id'),
'foreign keys' => array(
'pheno_plant' => array(
'table' => 'pheno_plant',
'columns' => array('plant_id' => 'plant_id')),),
);
//
// PROJECT-CVTERM
// Group a set of traits/cvterm to a project.
$schema['pheno_project_cvterm'] = array(
'description' => t('Table for grouping set of traits to a project.'),
'fields' => array(
'project_cvterm_id' => array(
'description' => t('Primary key: A unique ID number.)'),
'type' => 'serial',
'not null' => TRUE,
),
'project_id' => array(
'description' => t('ID number of a project from chado.project table'),
'type' => 'int',
'not null' => TRUE,
),
'cvterm_id' => array(
'description' => t('cvterm id number of a trait'),
'type' => 'int',
'not null' => TRUE,
),
// This option will allow for grouping of traits. In AGILE Project, types include:
// Essential Traits, Optional Traits, Subset Trait and Plant Property.
// Use this field to indicate as such.
// A function in rawpheno.function.measurements lists possible types that can be made available
// to admin when adding traits in the admin control panel.
'type' => array(
'description' => t('Indicate a column header is essential, not essential (optional), a plant property and other'),
'type' => 'varchar',
'length' => 20,
),
),
'primary key' => array('project_cvterm_id'),
);
// PROJECT-PLANT/STOCK
$schema['pheno_plant_project'] = array(
'description' => t('Table for mapping stock to a project'),
'fields' => array(
'plant_project_id' => array(
'description' => t('Primary key: A nunique ID number.'),
'type' => 'serial',
'not null' => TRUE,
),
'project_id' => array(
'description' => t('ID number of a project from chado.project table.'),
'type' => 'int',
'not null' => TRUE,
),
'plant_id' => array(
'description' => t('ID number of stock from pheno_plant table.'),
'type' => 'int',
'not null' => TRUE,
),
),
'primary key' => array('plant_project_id'),
);
// PROJECT-USER
$schema['pheno_project_user'] = array(
'description' => t('This table will hold information about user and project'),
'fields' => array(
'project_user_id' => array(
'description' => t('Primary Key: A unique ID number'),
'type' => 'serial',
'not noll' => TRUE,
),
'project_id' => array(
'description' => t('ID number of project from chado.project table'),
'type' => 'int',
'not null' => TRUE,
),
'uid' => array(
'description' => t('ID number of logged in user from user table'),
'type' => 'int',
'not null' => TRUE,
),
),
'primary key' => array('project_user_id'),
);
// PROJECT-BACKUP-FILE
$schema['pheno_backup_file'] = array(
'description' => t('This table will hold information about files uploaded by user to a project'),
'fields' => array(
'file_id' => array(
'description' => t('Primary Key: A unique ID number'),
'type' => 'serial',
'not null' => TRUE,
),
'project_user_id' => array(
'description' => t('The project ID number from table pheno_project_user'),
'type' => 'int',
'not null' => TRUE,
),
'fid' => array(
'description' => t('File ID number from file_managed table'),
'type' => 'int',
'not null' => TRUE,
),
'version' => array(
'description' => t('The sequence number of the file in relation to a project'),
'type' => 'int',
),
'validation_result' => array(
'description' => t('Stores the result of file validation performed'),
'type' => 'text',
),
'notes' => array(
'description' => t('Notes, description and comments made to a file'),
'type' => 'text',
),
'archive' => array(
'description' => t('Indicates whether a file is temporarily removed from a project.'),
'type' => 'varchar',
'length' => 2,
'default' => 'n',
),
),
'primary key' => array('file_id'),
);
return $schema;
}
// Include function to manage column headers, cv terms and variable names.
module_load_include('inc', 'rawpheno', 'includes/rawpheno.function.measurements');
/**
* Function to manage terms used by this module.
*
* @return
* An array of string representing scale measurements, variable names,
* controlled vocabulary and measurement units.
*/
function rawpheno_function_terms($type) {
switch($type) {
case 'project':
return 'AGILE: Application of Genomic Innovation in the Lentil Economy';
break;
case 'defaults':
// Default colour scheme and heading/title.
return array('colour' => '#304356',
'rawdata' => 'Phenotypic data available',
'download' => 'Select locations and traits that you want to download',
'instructions' => 'Standard Phenotyping Procedure',
'upload' => 'Drag and Drop phenotypic data collection spreadsheet',
'backup' => 'My files',
'r-words' => 'of,to,have,on,at',
'r-chars' => '(,),/,-,:,;,%',
'r-replace' => '# = num,/ = div,? = unsure,- = to',);
break;
case 'scales':
// Scale equivalent for scale units.
return array('Lodging (Scale: 1-5) upright - lodged' => array(
1 => 'Vertical/upright',
2 => 'Leaning',
3 => 'Most plants at 45 degrees angle',
4 => 'All plants 10-45 degrees from ground',
5 => 'Most plants flat/prostrate',));
break;
case 'variables':
// Configuration variable names.
return array('rawpheno_colour_scheme',
'rawpheno_rawdata_title',
'rawpheno_download_title',
'rawpheno_instructions_title',
'rawpheno_upload_title',
'rawpheno_backup_title',
'rawpheno_rtransform_words',
'rawpheno_rtransform_characters',
'rawpheno_rtransform_replace',);
break;
case 'vocabularies':
// Controlled vocabularies.
return array('cv_prop' => 'phenotype_plant_property_types',
'cv_unit' => 'phenotype_measurement_units',
'cv_type' => 'phenotype_measurement_types',
'cv_rver' => 'phenotype_r_compatible_version',
'cv_desc' => 'phenotype_collection_method',);
break;
case 'units':
// Units available in the spreadsheet.
return rawpheno_function_default_unit('def');
break;
}
}
/**
* Implements hook_install().
*/
function rawpheno_install() {
// Get variable names and default values.
$vars = rawpheno_function_terms('variables');
$config = rawpheno_function_terms('defaults');
// # 1. INSERT CV.
// Insert all cvs required.
$cv = rawpheno_function_terms('vocabularies');
// Array to hold cv_id and cv_name.
// This will be used when creating cvterm relationships.
$arr_cvid_cvname = array();
$chado_inscv_exists = function_exists('chado_insert_cv');
foreach($cv as $cv_i => $cv_name) {
$def = str_replace('_', ' ', ucfirst($cv_name));
$tmp = ($chado_inscv_exists)
? chado_insert_cv($cv_name, $def)
: tripal_insert_cv($cv_name, $def);
if ($tmp) {
$arr_cvid_cvname[$cv_name] = $tmp->cv_id;
}
else {
tripal_report_error('Raw Phenotypes', TRIPAL_CRITICAL, 'Chado/Tripal failed to insert cv', array(), array('print' => TRUE));
}
}
// # 2. INSERT UNITS.
// Insert units and scale member used by scale.
// NOTE: Scale memeber is link to the column header Lodging (scale 1-5) and not to scale unit.
// When the column header Lodging is inserted, only then we can insert the scale members.
// Array to hold scale members.
$arr_unit = rawpheno_function_terms('units');
// Array to hold cvterm_id of each inserted unit.
// This will be used when creating a cvterm relationship linking a cvterm to a unit.
$arr_cvtermid_unit = array();
$chado_insterm_exists = function_exists('chado_insert_cvterm');
foreach($arr_unit as $unit => $def) {
$ins_unit = array(
'id' => 'rawpheno_tripal:' . $unit,
'name' => $unit,
'definition' => $def,
'cv_name' => $cv['cv_unit']
);
$tmp = ($chado_insterm_exists)
? chado_insert_cvterm($ins_unit)
: tripal_insert_cvterm($ins_unit);
if ($tmp) {
$arr_cvtermid_unit[$unit] = $tmp->cvterm_id;
}
else {
tripal_report_error('Raw Phenotypes', TRIPAL_CRITICAL, 'Chado/Tripal failed to insert cvterm (trait unit)', array(), array('print' => TRUE));
}
}
// # 3. INSERT COLUMN HEADERS.
// Insert column headers.
// When inserting column headers, generate the R Version, add collection method
// and map it to a AGILE (only when this project is available).
// Default Project.
// This is the default project, only when such project exists, otherwise, insert the column headers
// and make them available for use in admin control panel.
// @note project.name must be unique so no need to limit 1.
$default_project = rawpheno_function_terms('project');
$sql = "SELECT project_id FROM {project} WHERE name = :default_project";
$args = array(':default_project' => $default_project);
$prj_id = chado_query($sql, $args);
if ($prj_id->rowCount()) {
$project_ID = $prj_id->fetchField();
// When AGILE is present in this site, add a user to this project.
db_insert('pheno_project_user')
->fields(array('project_id' => $project_ID,
'uid' => $GLOBALS['user']->uid))
->execute();
// If the user is not the superadmin, add superadmin as well.
if ($GLOBALS['user']->uid != 1) {
db_insert('pheno_project_user')
->fields(array('project_id' => $project_ID,
'uid' => 1))
->execute();
}
}
// Initialize R Transformation rules required in generating R version of header.
// The default rules are the rules required in generating R version of headers default to this module.
variable_set($vars[6], $config['r-words']);
variable_set($vars[7], $config['r-chars']);
variable_set($vars[8], $config['r-replace']);
// Array to hold header types.
$trait_type = rawpheno_function_trait_types();
// Array to hold all column headers, plant property headers and essential headers.
$arr_allheaders = rawpheno_function_headers('expected');
$arr_subsetheader = rawpheno_function_headers('subset');
$arr_plantpropheader = rawpheno_function_headers('plantprop');
$arr_essentialheader = rawpheno_function_headers('essential');
foreach($arr_allheaders as $header) {
// Skip when it is Name.
if ($header == 'Name') {
continue;
}
// Get the definition and collection method.
$cvterm_info = rawpheno_function_get_definition($header);
// When no information is retured, default the definition to the term itself.
$cvterm_definition = (!empty($cvterm_info['define'])) ? $cvterm_info['define'] : $header;
// Set the type of header.
$cvterm_type = (in_array($header, $arr_plantpropheader)) ? $cv['cv_prop'] : $cv['cv_type'];
$ins_term = array(
'id' => 'rawpheno_tripal:' . $header,
'name' => $header,
'definition' => $cvterm_definition,
'cv_name' => $cvterm_type
);
// Insert the cvterm.
$cvterm = ($chado_insterm_exists)
? chado_insert_cvterm($ins_term)
: tripal_insert_cvterm($ins_term);
// Insert the cvterm data collection method.
if (!empty($cvterm_info['method']) && $cvterm) {
// Data collection method.
chado_insert_record('cvtermprop',
array(
'cvterm_id' => $cvterm->cvterm_id,
'type_id' => $arr_cvid_cvname[ $cv['cv_desc'] ],
'value' => $cvterm_info['method'],
'rank' => 0
)
);
}
else {
tripal_report_error('Raw Phenotypes', TRIPAL_CRITICAL, 'Chado/Tripal failed to insert cvterm (traits)', array(), array('print' => TRUE));
}
// Relate cvterm and unit.
// No relationship when header is Plant Property Type.
if (!in_array($header, $arr_plantpropheader) && $cvterm) {
// Extract the unit part from the column header. Usually, the information enclosed in a parenthesis.
$header_unit = rawpheno_function_header_unit($header);
$header_unit = (empty($header_unit)) ? 'text' : $header_unit;
chado_insert_record('cvterm_relationship',
array(
'subject_id' => $arr_cvtermid_unit[$header_unit],
'type_id' => $arr_cvid_cvname[ $cv['cv_unit'] ],
'object_id' => $cvterm->cvterm_id
)
);
}
// R version. To be implemented to all column headers.
$r_version = rawpheno_function_make_r_compatible($header);
if ($cvterm) {
chado_insert_record('cvtermprop',
array(
'cvterm_id' => $cvterm->cvterm_id,
'type_id' => $arr_cvid_cvname[ $cv['cv_rver'] ],
'value' => $r_version,
'rank' => 0
)
);
}
// Finally, map this particular header to project, again, only when default project is present.
if (isset($project_ID) AND $project_ID > 0) {
// Indicate the type of header.
if (in_array($header, $arr_plantpropheader)) {
// Type is Plant Property.
$type = $trait_type['type4'];
}
elseif (in_array($header, $arr_essentialheader)) {
// Trait is essential (must be in the spreadsheet).
$type = $trait_type['type1'];
}
elseif (in_array($header, $arr_subsetheader)) {
// Subset Trait
$type = $trait_type['type3'];
}
else {
// Trait is optional
$type = $trait_type['type2'];
}
db_insert('pheno_project_cvterm')
->fields(array(
'project_id' => $project_ID,
'cvterm_id' => $cvterm->cvterm_id,
'type' => $type
)
)
->execute();
}
}
// Lodging (scale 1-5) has been added, so the scale member table can now be generated.
$lodging = rawpheno_function_terms('scales');
$cl = array('name' => array_keys($lodging)[0], 'cv_id' => array('name' => $cv['cv_type']));
if (function_exists('chado_get_cvterm')) {
$cvterm_lodging = chado_get_cvterm($cl);
}
else {
$cvterm_lodging = tripal_get_cvterm($cl);
}
// Insert scale values 1 - 5
foreach (array_values($lodging)[0] as $code => $val) {
db_insert('pheno_scale_member')
->fields(array(
'scale_id' => $cvterm_lodging->cvterm_id,
'code' => $code,
'value' => $val
))
->execute();
}
// # 4. MATERIALIZED VIEW.
// Create materialized view used in phenotypes/rawdata.
// Using Tripal: tripal_add_mview().
$mv_name = 'rawpheno_rawdata_summary';
$mv_module = 'rawpheno';
$mv_comment = 'Materialized view used by rawpheno module to generate summary of traits per location, rep and year.';
// Create a summary of data in the following format:
// Plant id, Location, Rep, Planting Date and Total traits count.
// NOTE: trait count includes planting date.
// NOTE: As a materialized view, this get's executed by chado query.
$mv_sql = "SELECT CAST(t1.plant_id AS numeric) AS plant_id,
t2.value AS location,
t3.value AS rep,
t5.value AS planting_year,
COUNT(DISTINCT t4.type_id) AS total_count,
ARRAY_TO_STRING(ARRAY_AGG(DISTINCT t4.type_id), ',') AS all_traits
FROM pheno_plant AS t1
INNER JOIN [pheno_plantprop] AS t2 USING(plant_id)
INNER JOIN [pheno_plantprop] AS t3 USING(plant_id)
INNER JOIN [pheno_measurements] AS t4 USING(plant_id)
INNER JOIN [pheno_measurements] AS t5 USING(plant_id)
WHERE
t2.type_id = (SELECT cvterm_id FROM {cvterm} cvt LEFT JOIN {cv} cv ON cv.cv_id=cvt.cv_id WHERE cvt.name = 'Location' AND cv.name = 'phenotype_plant_property_types') AND
t3.type_id = (SELECT cvterm_id FROM {cvterm} cvt LEFT JOIN {cv} ON cv.cv_id=cvt.cv_id WHERE cvt.name = 'Rep' AND cv.name = 'phenotype_plant_property_types') AND
t5.type_id = (SELECT cvterm_id FROM {cvterm} cvt LEFT JOIN {cv} ON cv.cv_id=cvt.cv_id WHERE cvt.name = 'Planting Date (date)' AND cv.name = 'phenotype_measurement_types')
GROUP BY t1.plant_id, t4.plant_id, t2.value, t3.value, t5.value";
// Schema array.
$mv_schema = array (
'table' => 'rawpheno_rawdata_mview',
'fields' => array (
'plant_id' => array (
'type' => 'int'),
'location' => array (
'type' => 'varchar',
'length' => 255),
'rep' => array (
'type' => 'varchar',
'length' => 255),
'planting_date' => array (
'type' => 'varchar',
'length' => 255),
'total_count' => array (
'type' => 'int'),
'all_traits' => array(
'type' => 'text'),
),
'indexes' => array(
'plant_id' => array('plant_id'),
'location' => array('location'),
)
);
// Create materialized view.
if (function_exists('chado_add_mview')) {
chado_add_mview($mv_name, $mv_module, $mv_schema, $mv_sql, $mv_comment, FALSE);
}
else {
tripal_add_mview($mv_name, $mv_module, $mv_schema, $mv_sql, $mv_comment, FALSE);
}
// #5. DEFAULT SITE SETTINGS.
// Set default colour scheme and page titles.
variable_set($vars[0], $config['colour']);
variable_set($vars[1], $config['rawdata']);
variable_set($vars[2], $config['download']);
variable_set($vars[3], $config['instructions']);
variable_set($vars[4], $config['upload']);
variable_set($vars[5], $config['backup']);
// Add data collection spreadsheet to public:// directory,
// and link this file for download in instructions page.
$destination = file_default_scheme() . '://';
if (file_prepare_directory($destination, FILE_MODIFY_PERMISSIONS)) {
// Add this file if directory is writable.
// File is in zip format as it is more friendly for download than xlsx.
$file = 'AGILE-PhenotypeDataCollection-v5.xlsx.zip';
$source = drupal_get_path('module', 'rawpheno') . '/theme/' . $file;
// Copy file without creating an entry in drupal database.
// If file is present, replace it with the same file.
file_unmanaged_copy($source, $destination, FILE_EXISTS_REPLACE);
}
}
/**
* Implements hook_uninstall().
*/
function rawpheno_uninstall() {
// # 1. UNREGISTER PERSISTENT VARIABLES.
$vars = rawpheno_function_terms('variables');
foreach($vars as $var) {
if (variable_get($var)) {
variable_del($var);
}
}
// # 2. DROP MATERIALIZED VIEW TABLE.
// Using tripal: tripal_delete_mview().
// Get mview id of materilaized view created by hook_install().
// @note tripal_mviews.mv_table must be unique (constraint).
$sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table = :mv_table";
$mview_id = db_query($sql, array(':mv_table' => 'rawpheno_rawdata_mview'))
->fetchField();
if (isset($mview_id) AND $mview_id > 0) {
// Delete materialized view.
tripal_delete_mview($mview_id);
}
// # 3. DELETE CV AND CVTERM.
$cv = rawpheno_function_terms('vocabularies');
foreach($cv as $cv_i => $cv_name) {
if (function_exists('chado_get_cv')) {
$cv = chado_get_cv(array('name' => $cv_name));
}
else {
$cv = tripal_get_cv(array('name' => $cv_name));
}
$args = array(':cv_id' => $cv->cv_id);
// Delete relationships.
if ($cv_i == 'cv_unit') {
$del = "DELETE FROM {cvterm_relationship} WHERE type_id = :cv_id";
chado_query($del, $args);
}
// Delete R version.
if ($cv_i == 'cv_rver') {
$del = "DELETE FROM {cvtermprop} WHERE type_id = :cv_id";
chado_query($del, $args);
}
// Delete cvterms.
if ($cv_i == 'cv_type') {
$del = "DELETE FROM {cvterm} WHERE cv_id = :cv_id";
chado_query($del, $args);
}
// Finally, delete the cv
$del = "DELETE FROM {cv} WHERE cv_id = :cv_id";
chado_query($del, $args);
}
}
/**
* Implements hook_update_N().
* Map cvterm in tripal database to rawpheno_tripal.
*
* N
* 7 - Drupal core compatibility.
* 2 - Module's major release version.
* 01 - Sequence count.
*/
function rawpheno_update_7201() {
// Rawphenotypes database name.
$rawpheno_dbname = 'rawpheno_tripal';
// db_id
$rawpheno_dbid = '';
// In db table, check to see if rawpheno_tripal exists.
// @note db.name must be unique (constraint).
$sql = "SELECT db_id FROM {db} WHERE name = :rawpheno_dbname";
$args = array(':rawpheno_dbname' => $rawpheno_dbname);
$db_id = chado_query($sql, $args);
if ($db_id->rowCount()) {
// Already created. Save the db_id
$rawpheno_dbid = $db_id
->fetchField();
}
else {
// Not created. Insert a row and get the id.
$db_id = chado_insert_record('db', array('name' => $rawpheno_dbname));
$rawpheno_dbid = $db_id['db_id'];
}
// Get the db id of Tripal db. Use it to futher filter the rows for update
// since previous version use tripal as default db.
// @note db.name must be unique (constraint).
$sql = "SELECT db_id FROM {db} WHERE name = 'tripal'";
$tripal = chado_query($sql);
if ($tripal->rowCount()) {
$tripal_dbid = $tripal
->fetchField();
// With the db_id, update all rawphenotypes cvterms to use this db_id in dbxref.
$t = rawpheno_function_terms('vocabularies');
unset($t['cv_rver'], $t['cv_desc']);
$terms = array_values($t);
// Update db_id in dbxerf table. Map all rawphenotypes cvterms to rapheno_tripal db.
$sql = "
UPDATE {dbxref} SET db_id = :rawpheno_dbid
WHERE
dbxref_id IN (SELECT t2.dbxref_id FROM {cv} AS t1 INNER JOIN {cvterm} AS t2 USING (cv_id) WHERE t1.name IN (:terms))
AND db_id = :tripal_dbid";
$args = array(':rawpheno_dbid' => $rawpheno_dbid, // Map to rawpheno_tripal instead.
':terms' => $terms, // Only for terms phenotype_plant_property, phenotype_measurement_units and phenotype_measurement_type
':tripal_dbid' => $tripal_dbid); // Where previously set to TRIPAL.
chado_query($sql, $args);
}
}
/**
* Implements hook_update_N().
* Update custom table with tables relating a project to set of cvterms
* and project to stock/name.
*
* N
* 7 - Drupal core compatibility.
* 0 - Module's major release version.
* 02 - Sequence count.
*/
function rawpheno_update_7002() {
// Table definition as per definition in hook_schema() above.
// PROJECT-CVTERM
// Group a set of traits/cvterm to a project.
$schema['pheno_project_cvterm'] = array(
'description' => t('Table for grouping set of traits to a project.'),
'fields' => array(
'project_cvterm_id' => array(
'description' => t('Primary key: A unique ID number.)'),
'type' => 'serial',
'not null' => TRUE,
),
'project_id' => array(
'description' => t('ID number of a project from chado.project table'),
'type' => 'int',
'not null' => TRUE,
),
'cvterm_id' => array(
'description' => t('cvterm id number of a trait'),
'type' => 'int',
'not null' => TRUE,
),
// This option will allow for grouping of traits. In for AGILE Project, types include:
// Essential Traits, Optional Traits, Subset Trait and Plant Property.
// Use this field to indicate as such.
// A function in rawpheno.function.measurements lists possible types that can be made available
// to admin when adding traits in the admin control panel.
'type' => array(
'description' => t('Indicate a column header is essential, not essential (optional), a plant property and other'),
'type' => 'varchar',
'length' => 20,
),
),
'primary key' => array('project_cvterm_id'),
);
db_create_table('pheno_project_cvterm', $schema['pheno_project_cvterm']);
// PROJECT-PLANT/STOCK
$schema['pheno_plant_project'] = array(
'description' => t('Table for mapping stock to a project'),
'fields' => array(
'plant_project_id' => array(
'description' => t('Primary key: A nunique ID number.'),
'type' => 'serial',
'not null' => TRUE,
),
'project_id' => array(
'description' => t('ID number of a project from chado.project table.'),
'type' => 'int',
'not null' => TRUE,
),
'plant_id' => array(
'description' => t('ID number of stock from pheno_plant table.'),
'type' => 'int',
'not null' => TRUE,
),
),
'primary key' => array('plant_project_id'),
);
db_create_table('pheno_plant_project', $schema['pheno_plant_project']);
// PROJECT-USER
$schema['pheno_project_user'] = array(
'description' => t('This table will hold information about user and project that'),
'fields' => array(
'project_user_id' => array(
'description' => t('Primary Key: A unique ID number'),
'type' => 'serial',
'not noll' => TRUE,
),
'project_id' => array(
'description' => t('ID number of project from chado.project table'),
'type' => 'int',
'not null' => TRUE,
),
'uid' => array(
'description' => t('ID number of logged in user from user table'),
'type' => 'int',
'not null' => TRUE,
),
),
'primary key' => array('project_user_id'),
);
db_create_table('pheno_project_user', $schema['pheno_project_user']);
// PROJECT-BACKUP-FILE
$schema['pheno_backup_file'] = array(
'description' => t('This table will hold information about files uploaded by user to a project'),
'fields' => array(
'file_id' => array(
'description' => t('Primary Key: A unique ID number'),
'type' => 'serial',
'not null' => TRUE,
),
'project_user_id' => array(
'description' => t('The project ID number from table pheno_project_user'),
'type' => 'int',
'not null' => TRUE,
),
'fid' => array(
'description' => t('File ID number from file_managed table'),
'type' => 'int',
'not null' => TRUE,
),
'version' => array(
'description' => t('The sequence number of the file in relation to a project'),
'type' => 'int',
),
'validation_result' => array(
'description' => t('Stores the result of file validation performed'),
'type' => 'text',
),
'notes' => array(
'description' => t('Notes, description and comments made to a file'),
'type' => 'text',
),
'archive' => array(
'description' => t('Indicates whether a file is temporarily removed from a project.'),
'type' => 'varchar',
'length' => 2,
'default' => 'n',
),
),
'primary key' => array('file_id'),
);
db_create_table('pheno_backup_file', $schema['pheno_backup_file']);
// UPDATE First Release to Project Based.
// When updating the first release to project based, first update page configuration
// to add page title to back up page at the same time initialize R transformation rules
// with default rules (rules used to generate R compatible verstion of all AGILE-specific
// traits). Next, update cvterms definition, create a relationship entry relating a unit to cvterm,
// add method of collection method description and transform a R compatible version
// of the cvterm. Finally, tell if AGILE project is present in the host site and if not, create the project
// and reference the project id number. When the module has data, map everything to this
// project and appoint the user running the update and super admin.
// Update - Add backup page title.
$vars = rawpheno_function_terms('variables');
$config = rawpheno_function_terms('defaults');
variable_set($vars[5], $config['backup']);
// Update - Add default R transformation rules.
variable_set($vars[6], $config['r-words']);
variable_set($vars[7], $config['r-chars']);
variable_set($vars[8], $config['r-replace']);
// AGILE Project.
$default_project = rawpheno_function_terms('project');
// @note project.name must be unique (constraint).
$sql = "SELECT project_id FROM {project} WHERE name = :default_project";
$args = array(':default_project' => $default_project);
$prj_id = chado_query($sql, $args);
if ($prj_id->rowCount()) {
$project_ID = $prj_id->fetchField();
}
else {
// Create AGILE Project when not present in this site.
$tmp = chado_insert_record('project',
array('name' => $default_project)
);
$project_ID = $tmp->project_id;
}
// Trait types array.
$trait_type = rawpheno_function_trait_types();
// Subset trait types array.
$arr_subsetheader = rawpheno_function_headers('subset');
// Plant Property trait types array.
$arr_plantpropheader = rawpheno_function_headers('plantprop');
// Essential trait types array.
$arr_essentialheader = rawpheno_function_headers('essential');
// Measurements type and Plant Property type.
$voc = rawpheno_function_terms('vocabularies');
// CV R version.
$cv_rver = tripal_insert_cv($voc['cv_rver'], str_replace('_', ' ', ucfirst($voc['cv_rver'])));