-
Notifications
You must be signed in to change notification settings - Fork 2
/
rawpheno.module
executable file
·1188 lines (1024 loc) · 41 KB
/
rawpheno.module
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
* The main functionality for this module.
* Credits to:
* http://jamesdavidson.io/blog/how-create-multi-step-form-drupal-7
* https://github.com/nuovo/spreadsheet-reader
* https://github.com/SystemDevil/PHP_XLSXWriter_plus
* http://www.d3js.org
* https://laceysanderson.github.io/2016/07/23/Drupal-progress-bar.html
*/
// Include function to manage column headers.
module_load_include('inc', 'rawpheno', 'includes/rawpheno.function.measurements');
// Include functions required in processing spreadsheet file.
module_load_include('inc', 'rawpheno', 'includes/rawpheno.upload.excel');
module_load_include('inc', 'rawpheno', 'includes/rawpheno.validation');
module_load_include('inc', 'rawpheno', 'includes/rawpheno.upload.form');
module_load_include('inc', 'rawpheno', 'includes/rawpheno.tripaldownload');
// Prepare term used by Germplasm Raw Phenotypes Field.
module_load_include('inc', 'rawpheno', 'includes/TripalFields/rawpheno.fields');
/**
* Implements hook_menu().
*/
function rawpheno_menu() {
// The following menu items will default in Navigation menu block.
// RAW DATA
// - DOWNLOAD DATA
// - INSTRUCTIONS
// - UPLOAD DATA
// RAW DATA PAGE
// A summary page of the raw data currently available.
$items['phenotypes/raw'] = array(
'title' => 'Raw Data',
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_rawdata'),
'access arguments' => array('access rawpheno'),
'file' => 'includes/rawpheno.rawdata.form.inc',
'type' => MENU_NORMAL_ITEM,
);
// Menu callback which generates the summary data in JSON,
// required by the heat map in rawdata page.
$items['rawdata'] = array(
'page callback' => 'rawpheno_rawdata_summary_json',
'access callback' => true,
'delivery callback' => 'drupal_json_output',
'type' => MENU_CALLBACK,
);
// Menu callback which returns summary data for a trait (for histogram).
$items['rawdata_trait'] = array(
'page callback' => 'rawpheno_rawdata_trait_json',
'access callback' => true,
'delivery callback' => 'drupal_json_output',
'type' => MENU_CALLBACK,
);
// Menu callback that returns year and location given a trait use to populate select boxes
// to categorize barchart in rawdata page.
$items['rawdata_trait_category'] = array(
'page callback' => 'rawpheno_rawdata_trait_category_json',
'access callback' => true,
'delivery callback' => 'drupal_json_output',
'type' => MENU_CALLBACK,
);
// INSTRUCTIONS PAGE
// A page containing standard phenotyping procedure and
// providing data collection spreadsheet.
$items['phenotypes/raw/instructions'] = array(
'title' => 'Instructions',
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_instructions'),
'access arguments' => array('access rawpheno'),
'file' => 'includes/rawpheno.instructions.form.inc',
'type' => MENU_NORMAL_ITEM,
'weight' => 0,
);
// Menu callback which generates a project specific data collection spreadsheet.
$items['phenotypes/raw/instructions/%'] = array(
'title' => 'Instructions',
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_instructions', 3),
'access arguments' => array('access rawpheno'),
'file' => 'includes/rawpheno.instructions.form.inc',
'type' => MENU_CALLBACK,
);
// Menu callback which generates a list of headers in JSON,
// used to autocomplete a search field and process search action
// in instructions page.
$items['phenotypes/raw/instructions/autocomplete/%'] = array(
'page callback' => 'rawpheno_instructions_autocomplete_search',
'page arguments' => array(4),
'access arguments' => array('access rawpheno'),
'type' => MENU_CALLBACK,
);
$items['phenotypes/raw/instructions/spreadsheet/%'] = array(
'page callback' => 'rawpheno_instructions_create_spreadsheet',
'page arguments' => array(4),
'access arguments' => array('access rawpheno'),
'file' => 'includes/rawpheno.instructions.form.inc',
'type' => MENU_CALLBACK,
);
// DOWNLOADS PAGE
// A page providing export data options.
// NOTE: Page below uses as different access arguments than
// the rest of the pages. Not everyone can download.
$items['phenotypes/raw/download'] = array(
'title' => 'Download Data',
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_download'),
'access arguments' => array('download rawpheno'),
'file' => 'includes/rawpheno.download.form.inc',
'type' => MENU_NORMAL_ITEM,
'weight' => 6,
);
$items['phenotypes/raw/csv'] = array(
'title' => 'Download Raw Phenotypic Data: CSV/TAR',
'page callback' => 'trpdownload_download_page',
'page arguments' => array('rawpheno_csv'),
'access arguments' => array('download rawpheno'),
'type' => MENU_CALLBACK,
);
// DRAG AND DROP UPLOAD PAGE
// A page for uploading and validating data collection spreadsheet.
$items['phenotypes/raw/upload'] = array(
'title' => 'Upload Data',
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_upload_form_master'),
'access arguments' => array('access rawpheno'),
'file' => 'includes/rawpheno.upload.form.inc',
'type' => MENU_NORMAL_ITEM,
'weight' => 3,
);
// This callback will generate a job progress and status information
// in JSON format to be used by the progress bar in the last stage of the upload process.
$items['phenotypes/raw/upload/job_summary/%'] = array(
'page callback' => 'rawpheno_upload_job_progress_json',
'page arguments' => array(4),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
// BACKUP
// A page for uploading spreadsheet.
$items['phenotypes/raw/backup'] = array(
'title' => 'Backup',
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_backup'),
'access arguments' => array('access rawpheno'),
'file' => 'includes/rawpheno.backup.form.inc',
'type' => MENU_NORMAL_ITEM,
'weight' => 7,
);
// List traits available in a project.
$items['phenotypes/raw/backup/%/%/%'] = array(
'title' => 'Backup',
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_backup', 3, 4, 5),
'access arguments' => array('access rawpheno'),
'file' => 'includes/rawpheno.backup.form.inc',
'type' => MENU_CALLBACK,
);
// ADMININSTRATIVE PAGE OF THIS MODULE
// A page for changing page colour and page title.
$items['admin/tripal/extension/rawphenotypes'] = array(
'title' => 'Rawphenotypes',
'description' => 'Provides an interface for managing projects, users and column headers used in Rawphenotypes',
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_admin_main_page'),
'access arguments' => array('access administration pages'),
'file' => 'includes/rawpheno.admin.form.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/tripal/extension/rawphenotypes/rawpheno_config'] = array(
'title' => 'Page Configurations',
'description' => 'Apply a colour scheme and set page title.',
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_admin_page'),
'access arguments' => array('access administration pages'),
'file' => 'includes/rawpheno.admin.form.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
// A page for managing R Transformation rules.
$items['admin/tripal/extension/rawphenotypes/rawpheno_rheaders'] = array(
'title' => 'Define R Transformation Rules',
'description' => 'Define R Transfomation Rules to be applied when generating R Friendly alternative of column header.',
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_admin_rheaders'),
'access arguments' => array('access administration pages'),
'file' => 'includes/rawpheno.admin.form.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
// A page for managing project and traits.
// List all projects and count traits available.
$items['admin/tripal/extension/rawphenotypes/all_projects'] = array(
'title' => 'Manage Experiments',
'description' => 'Create experiment, define column headers and appoint users to experiment.',
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_admin_all_projects'),
'access arguments' => array('access administration pages'),
'file' => 'includes/rawpheno.admin.form.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 3,
);
// Edit trait in a project.
$items['admin/tripal/extension/rawphenotypes/all_projects/%/%/%'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('rawpheno_admin_project_management', 5, 6, 7),
'access arguments' => array('access administration pages'),
'file' => 'includes/rawpheno.admin.form.inc',
'type' => MENU_CALLBACK,
);
// Menu callback used to search user when appointing the user to a project.
$items['admin/tripal/extension/rawphenotypes/username/%'] = array(
'page callback' => 'rawpheno_manageproject_autocomplete_search',
'page arguments' => array(5),
'access arguments' => array('access administration pages'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function rawpheno_permission() {
return array(
'access rawpheno' => array(
'title' => t('Access Raw Phenotypic pages (Instructions, Upload, Backup and Summary page'),
),
'download rawpheno' => array(
'title' => t('Download Phenotypic Data (CSV format) in Rawpheno Download Data page'),
)
);
}
/**
* Implements a hook_preprocess_HOOK().
* This function prepares variables for Raw Phenotypes Rawdata Page.
*/
function rawpheno_preprocess_rawpheno_rawdata(&$variables, $hook) {
// Colour scheme, project check and data check.
$rawpheno_load = rawpheno_module_defaults();
list($variables['theme_colour'], $variables['my_projects'], $variables['has_prj'], $variables['has_data']) = $rawpheno_load;
// Cross-link rawdata page to download page.
$variables['rel_url'] = url('phenotypes/raw/download');
// Page Title
$variables['page_title'] = variable_get('rawpheno_rawdata_title');
// D3 version
$ver = rawpheno_function_d3_version();
$ver = explode('.', $ver);
// Version
$variables['d3ver'] = (isset($ver[0])) ? trim($ver[0]) : 0;
// Release
$variables['d3rel'] = (isset($ver[1])) ? trim($ver[1]) : 0;
// Does D3 exists
$variables['d3_in'] = libraries_load('d3js');
}
/**
* Implements a hook_preprocess_HOOK().
* This function prepares variables for Raw Phenotypes Instuctions Page.
*/
function rawpheno_preprocess_rawpheno_instructions(&$variables, $hook) {
// Colour scheme, project check and data check.
$rawpheno_load = rawpheno_module_defaults();
list($variables['theme_colour'], $variables['my_projects'], $variables['has_prj'], $variables['has_data']) = $rawpheno_load;
// Cross-link instructions page to upload page.
$variables['rel_url'] = url('phenotypes/raw/upload');
// Page Title
$variables['page_title'] = variable_get('rawpheno_instructions_title');
// Path to module used to embed image in appendix tab.
$variables['path'] = drupal_get_path('module', 'rawpheno');
// Spreadsheet writer is in?
$is_in = libraries_get_path('PHP_XLSXWriter_plus');
$variables['writer_in'] = $is_in;
}
/**
* Implements a hook_preprocess_HOOK().
* This function prepares variables for Raw Phenotypes Upload Page.
*/
function rawpheno_preprocess_rawpheno_upload_form_master(&$variables, $hook) {
// Colour scheme, project check and data check.
$rawpheno_load = rawpheno_module_defaults();
list($variables['theme_colour'], $variables['my_projects'], $variables['has_prj'], $variables['has_data']) = $rawpheno_load;
// Directory permission used in upload and backup.
$pub_dir = 'public://';
$perm = file_prepare_directory($pub_dir);
$variables['dir_permission'] = $perm;
// Does drag and drop module exist?
$is_in = module_exists('dragndrop_upload_element');
$variables['dnd_in'] = $is_in;
// Does spreadsheet reader exist?
$is_in = libraries_get_path('spreadsheet-reader');
$variables['reader_in'] = $is_in;
// Cross-link upload page to instructions page.
$variables['rel_url'] = url('phenotypes/raw/instructions');
// Page Title
$variables['page_title'] = variable_get('rawpheno_upload_title');
// Support Email.
$support_email = rawpheno_function_get_support_email();
$variables['support_email'] = $support_email;
// Needs url in help window and links in stage 3.
$variables['page_url'] =
array(
'rawpheno_rawdata' => url('phenotypes/raw'),
'rawpheno_download' => url('phenotypes/raw/download'),
'rawpheno_instructions' => url('phenotypes/raw/instructions'),
'rawpheno_upload' => url('phenotypes/raw/upload'),
'rawpheno_backup' => url('phenotypes/raw/backup'),
'rawpheno_demo' => url('phenotypes/raw/videos'),
);
// Stages in upload page.
$variables['upload_stages'] = array('check' => 'Validate Spreadsheet',
'review' => 'Describe New Trait',
'save' => 'Save Spreadsheet');
}
/**
* Implements a hook_preprocess_HOOK().
* This function prepares variables for Raw Phenotypes Backup Page.
*/
function rawpheno_preprocess_rawpheno_backup(&$variables, $hook) {
// Colour scheme, project check and data check.
$rawpheno_load = rawpheno_module_defaults();
list($variables['theme_colour'], $variables['my_projects'], $variables['has_prj'], $variables['has_data']) = $rawpheno_load;
// Directory permission used in upload and backup.
$pub_dir = 'public://';
$perm = file_prepare_directory($pub_dir);
$variables['dir_permission'] = $perm;
// Does drag and drop module exist?
$is_in = module_exists('dragndrop_upload_element');
$variables['dnd_in'] = $is_in;
// Does spreadsheet reader exist?
$is_in = libraries_get_path('spreadsheet-reader');
$variables['reader_in'] = $is_in;
// Cross-link backup page to instructions page.
$variables['rel_url'] = url('phenotypes/raw/instructions');
// Page Title
$variables['page_title'] = variable_get('rawpheno_backup_title');
}
/**
* Implements a hook_preprocess_HOOK().
* This function prepares variables for Raw Phenotypes Download Page.
*/
function rawpheno_preprocess_rawpheno_download(&$variables, $hook) {
// Colour scheme, project check and data check.
$rawpheno_load = rawpheno_module_defaults();
list($variables['theme_colour'], $variables['my_projects'], $variables['has_prj'], $variables['has_data']) = $rawpheno_load;
// Cross-link download page to download page.
$variables['rel_url'] = url('phenotypes/raw');
// Page Title
$variables['page_title'] = variable_get('rawpheno_download_title');
}
/**
* Helper function: preforms module page load check and gets module colour scheme
* used in initiating variables in hook_preprocess_HOOK() implementation above.
*
* @return array
* An array containing the default colour scheme, projects appointed to a user,
* check if module has an active project as well as if module has data.
*/
function rawpheno_module_defaults() {
// Colour scheme selected by the user from the admin/configuration.
$colour = variable_get('rawpheno_colour_scheme');
// This colour will fill background of header and top right button, and border of content area.
$colour = (empty($colour)) ? '#304356' : $colour;
// Projects assigned to user.
$log_user = $GLOBALS['user']->uid;
$my_projects = rawpheno_function_user_project($log_user);
// Does the module contain project?
$has_project = rawpheno_function_project();
// If it has project, does it have data?
$has_data = rawpheno_function_data();
return array(
$colour, // Colour scheme.
$my_projects, // User appointed project.
$has_project, // Module has at least a project.
$has_data, // Module has data.
);
}
/**
* Implements hook_theme().
*
* Returns HTML for rawpheno pages.
*/
function rawpheno_theme($existing, $type, $theme, $path) {
// Rawdata page.
$items['rawpheno_rawdata'] = array(
'render element' => 'form',
'template' => 'rawpheno_pages',
'path' => $path . '/theme',
);
// Instructions page.
$items['rawpheno_instructions'] = array(
'render element' => 'form',
'template' => 'rawpheno_pages',
'path' => $path . '/theme',
);
// Download page.
$items['rawpheno_download'] = array(
'render element' => 'form',
'template' => 'rawpheno_pages',
'path' => $path . '/theme',
);
// Backup page.
$items['rawpheno_backup'] = array(
'render element' => 'form',
'template' => 'rawpheno_pages',
'path' => $path . '/theme',
);
// Upload page.
$items['rawpheno_upload_form_master'] = array(
'render element' => 'form',
'template' => 'rawpheno_pages',
'path' => $path . '/theme',
);
// Upload Errors.
$items['rawpheno_upload_validation_report'] = array(
'template' => 'rawpheno_upload_validation_report',
'path' => $path . '/theme',
);
// Notification Block.
$items['rawpheno_notification_block'] = array(
'template' => 'rawpheno_notification_block',
'path' => $path . '/theme',
'variables' => array(
'alert' => null,
'username' => null,
'hostname' => null,
'file_count' => null,
'path_module' => null,
'path_phenotypes_raw' => null,
'path_phenotypes_video' => null,
),
);
// Raw data field.
$items['rawpheno_germplasm_field'] = array(
'template' => 'rawpheno_germplasm_field',
'path' => $path . '/theme',
'variables' => array('raw_data' => '')
);
return $items;
}
/**
* Implements hook_libraries_info().
*
* Define external libraries: Spreadsheet Reader and D3JS
*/
function rawpheno_libraries_info() {
// Spreadsheet reader
// File option is empty since library files are included
// individually using include_once().
// files in: sites/all/libraries/spreadsheet-reader
// - SpreadsheetReader.php
// - SpreadsheetReaderXLSX.php
// - SpreadsheetReaderXLS.php
// - php-excel-reader/excel_reader2.php
// NOTE: To prevent library form auto formatting data to MM/DD/YYYY,
// suggest a new data format YYYY-mm-dd in the source code in line:
// * line 678 in excel_reader2.php
// 0xe = "m/d/Y to 0xe => "Y-m-d"
// * line 834 in SpreadsheetReader_XLSX.php
// $Value = $Value -> format($Format['Code']); to $Value = $Value -> format('Y-m-d');
// Let Drupal decide where library is.
$lib_reader = libraries_get_path('spreadsheet-reader');
$libraries['spreadsheet_reader'] = array(
'name' => 'NUOVO Spreadsheet',
'vendor url' => 'https://github.com/nuovo/spreadsheet-reader',
'version callback' => 'rawpheno_function_reader_version_callback',
'download url' => 'https://github.com/nuovo/spreadsheet-reader/archive/master.zip',
'library path' => $lib_reader,
'path' => $lib_reader . '/',
'files' => array(),
);
// Spreadsheet writer.
// Let Drupal decide where library is.
$lib_writer = libraries_get_path('PHP_XLSXWriter_plus');
$libraries['spreadsheet_writer'] = array(
'name' => 'PHP_XLSXWriter_plus Spreadsheet Writer',
'vendor url' => 'https://github.com/SystemDevil/PHP_XLSXWriter_plus',
'version' => 1,
'download url' => 'https://github.com/SystemDevil/PHP_XLSXWriter_plus/archive/master.zip',
'library path' => $lib_writer,
'files' => array('xlsxwriter.class.php'),
);
// D3 JavaScript.
// Let Drupal decide where library is
$lib_d3 = libraries_get_path('d3');
$libraries['d3js'] = array(
'name' => 'D3 Data-Driven Documents',
'vendor url' => 'https://d3js.org/',
'version callback' => 'rawpheno_function_d3_version_callback',
'download url' => 'https://github.com/d3/d3/releases/download/v3.5.14/d3.zip',
'library path' => $lib_d3,
'files' => array(
'js' => array('d3.js')
),
);
return $libraries;
}
/**
* Function callback: return the version of the spreadsheet reader library installed.
*
* @see implementation of hook_libraries_info().
*/
function rawpheno_function_reader_version_callback() {
$ver = rawpheno_function_sreader_version();
return $ver;
}
/**
* Function callback: return the version of the d3 library installed.
*
* @see implementation of hook_libraries_info().
*/
function rawpheno_function_d3_version_callback() {
$ver = rawpheno_function_d3_version();
return $ver;
}
/**
* Function callback: used by autcomplete search functionality
* used in manage project page - appoint a user to the project.
*/
function rawpheno_manageproject_autocomplete_search($project_id, $key = '') {
// Array to hold all user.
$arr_user = array();
$sql = "SELECT name, mail FROM {users}
WHERE
status = 1
AND LOWER(name) LIKE :name
ORDER BY name DESC LIMIT 10";
$args = array(':name' => '%' . strtolower($key) . '%');
$user = db_query($sql, $args);
foreach($user as $u) {
$arr_user[$u->name] = $u->name . ' - ' . $u->mail;
}
drupal_json_output($arr_user);
}
/**
* Function callback: Generate Tripal Job progress JSON.
*
* @param $job_id
* An integer containing the job id number of the job process to monitor.
*
* @retun
* A JSON and containing progress and status of job object.
*/
function rawpheno_upload_job_progress_json($job_id) {
// This is important as field requires the job id to be numeric value.
$job_id = (int)$job_id;
// Get the Tripal Job Object.
$job = tripal_get_job($job_id);
// Test if the Tripal Job exists and is that the status is not completed.
if ($job) {
if(trim(strtolower($job->status)) != 'completed') {
// Read the file containing the upload file progress of the Tripal Job.
// Please see rawpheno.upload.excel.inc - line: 69.
$tmp = file_directory_temp();
$filename = $tmp . '/' . 'job-progress' . $job_id . '.txt';
$percent = file_get_contents($filename);
// In the event of file collision or file writing process and file read returns nothing
// show ... (indicate calculating) instead. This will correct itself the next file read.
$percent_completed = empty($percent) ? '...' : $percent;
$message = ($percent == 100 OR trim(strtolower($job->status)) == 'completed')
? 'Completed'
: $job->status;
}
else {
$percent_completed = 100;
$message = $job->status;
if ($job->progress != 100) {
// Update Drupal Jobs Table when 100 percent only. This is to ensure that
// the progress wont get non numeric value when file read returns nothing
// usually happens when read and write not in sync.
db_update('tripal_jobs')
->fields(array('progress' => (int)$progress['percentage']))
->condition('job_id', $job_id, '=')
->execute();
}
}
// This is the exact format expected by the progress bar so do not alter it.
$progress = array(
'percentage' => $percent_completed,
'message' => $message,
);
// Print the array as JSON to the screen.
print drupal_json_output($progress);
// Do not show the Drupal headers and formatting.
// This is critical as if anything else is printed to the screen you will see
// an AJAX error instead of your progress bar ;-).
exit();
}
}
/**
* Function callback: Autocomplete search field.
*
* @see hook_menu()
*
* @param $key
* A string containing the search keywords entered in the search field.
* @param $project_id
* An integer containing the project id number.
*
* @return
* A JSON where each trait is both the key and the value. The key is what is
* sent to JavaScript processing the search, while the value is what is suggested
* to user while typing in the seach field.
*/
function rawpheno_instructions_autocomplete_search($project_id, $key = '') {
$trait_type = rawpheno_function_trait_types();
$sql = "SELECT name
FROM {cvterm} RIGHT JOIN pheno_project_cvterm USING(cvterm_id)
WHERE project_id = :project_id AND type <> :plantprop";
$args = array(':project_id' => $project_id, ':plantprop' => $trait_type['type4']);
$m = chado_query($sql, $args);
// Array to hold list of traits.
$arr_headers = array();
// Determine if keyword is provided.
if (empty($key)) {
foreach($m as $n) {
$arr_headers[] = $n->name;
}
}
else {
foreach($m as $n) {
if (stristr($n->name, $key)) {
$arr_headers[$n->name] = $n->name;
}
}
}
drupal_json_output($arr_headers);
}
/**
* Function callback: Generate data for the heat map.
*
* @see hook_menu()
*
* @return
* A JSON and each member has the following objects:
* location, year, rep, # traits, list of traits with value.
*/
function rawpheno_rawdata_summary_json() {
// Ensure project id is valid.
// Must exists, must not be empty and must not be more that 10 chars/digits.
if (!isset($_GET['project_id']) OR empty($_GET['project_id']) OR strlen($_GET['project_id']) >= 10) {
// No data.
return 0;
}
else {
// Get data for this project.
$project_id = (int)trim(strip_tags($_GET['project_id']));
// Get trait id of planting data.
$planting_date = rawpheno_get_trait_id('Planting Date (date)');
// get trait id of # of seeds planted
$seeds_planted = rawpheno_get_trait_id('# of Seeds Planted (count)');
// Minimum rep per year per location.
$min_set = 3;
// Select all location present in the materialized view.
$sql_location = "SELECT DISTINCT location
FROM chado.rawpheno_rawdata_mview INNER JOIN {pheno_plant_project} USING(plant_id)
WHERE project_id = :project
ORDER BY location ASC";
$result_location = db_query($sql_location, array(':project' => (int)$project_id));
$count_location = $result_location->rowCount();
if ($count_location > 0) {
// Array to hold data.
$data_json = array();
// Array to hold planting years and rep.
$arr_range['year'] = array();
$arr_range['rep'] = array();
// Define range of planting years and rep.
for($i = 0; $i < 2; $i++) {
$range = ($i == 0) ? 'year' : 'rep';
// Query materialized view.
$sql_range = sprintf("
SELECT DISTINCT %s AS data_range FROM chado.rawpheno_rawdata_mview
WHERE plant_id IN (SELECT plant_id FROM {pheno_plant_project} WHERE project_id = :project_id)
ORDER BY data_range ASC", ($range == 'year') ? 'SUBSTRING(planting_date, 1, 4)' : 'rep');
$r = db_query($sql_range, array(':project_id' => $project_id));
$count_data = $r->rowCount();
// Determine if total number of years is less than minimum number.
if ($count_data < $min_set) {
// Year/rep count is less than minimum, fill the gap to make the
// number always to minimum.
// Initial/starting year/rep.
$start_value = $r->fetchField();
$arr_range[$range][] = $start_value;
// Fill the missing years/rep and push to array.
for($c = 1; $c < $min_set; $c++) {
// Next year in the SQL result.
$d = $r->fetchField();
if (empty($d)) {
// If it is empty, compute the next year.
$start_value += 1;
$arr_range[$range][] = $start_value;
}
else {
// Year is present, store and set the start value to this year.
$arr_range[$range][] = $d;
$start_value = $d;
}
}
}
else {
// There is more than the minimum year/rep returned by the query.
// Store all values to years/rep array.
foreach($r as $range_value) {
$arr_range[$range][] = $range_value->data_range;
}
}
}
// Construct data set.
// Read each location available and construct data set.
$sql = "SELECT ARRAY_TO_STRING(ARRAY_AGG(DISTINCT all_traits), ',') AS all_traits
FROM chado.rawpheno_rawdata_mview
WHERE
location = :location
AND SUBSTRING(planting_date, 1, 4) = :year
AND rep = :rep
AND plant_id IN
(SELECT plant_id FROM {pheno_plant_project} WHERE project_id = :project_id)
LIMIT 1";
while($data = $result_location->fetchAssoc()) {
// In each location, read each planting year.
foreach($arr_range['year'] as $year) {
// In each year, read each rep.
foreach($arr_range['rep'] as $rep) {
// Each location in the materialized view
$location = $data['location'];
$args = array(':year' => $year, ':rep' => $rep, ':location' => $location, ':project_id' => $project_id);
// Get the entry with the most traits for this particular location, year and rep.
$traits = db_query($sql, $args)
->fetchObject();
if (!empty($traits->all_traits)) {
$trait_list = explode(',', $traits->all_traits);
$trait_list = array_unique($trait_list);
// NOTE:
// Less 2 to exclude trait Planting Data (date) and # of Seeds Planted (count).
// Before doing that, test if these two traits were part of the traits the count is based on.
$less = 0;
if (in_array($planting_date, $trait_list)) {
$less++;
}
if (in_array($seeds_planted, $trait_list)) {
$less++;
}
$trait_count = ((count($trait_list) - $less) < 0) ? 0 : (count($trait_list) - $less);
$trait_list = implode(',', $trait_list);
}
else {
$trait_list = 0;
$trait_count = 0;
}
// Create a json entry with the following keys:
// location, year, rep and trait.
$arr_json[] = array('location' => $location,
'year' => $year,
'rep' => $rep,
'trait' => $trait_count,
'type_id' => $trait_list);
}
}
}
return $arr_json;
}
else {
// No data.
return 0;
}
}
}
/**
* Function callback: Generate year and location used in categorizing barchart.
*
* @see hook_menu()
*
* @return
* A JSON containing the location and year for a given trait.
*/
function rawpheno_rawdata_trait_category_json() {
if ((!isset($_GET['project_id']) OR strlen($_GET['project_id']) >= 10) OR
(!isset($_GET['trait_id']) OR strlen($_GET['trait_id']) >= 10)) {
// No data.
return 0;
}
else {
$project_id = trim(strip_tags($_GET['project_id']));
$trait_id = trim(strip_tags($_GET['trait_id']));
// Location and planting_year (as Year).
$category = array('location', 'planting_date');
$arr_json = array();
foreach($category as $c) {
$col = ($c == 'location') ? 'location' : 'SUBSTRING(planting_date, 1, 4)';
$sql = sprintf("
SELECT DISTINCT %s AS col
FROM {rawpheno_rawdata_mview} WHERE plant_id IN (SELECT plant_id FROM pheno_plant_project WHERE project_id = :project_id)
AND STRPOS(all_traits, :trait_id) > 0
ORDER BY col ASC", $col);
$args = array(':project_id' => $project_id, ':trait_id' => $trait_id);
$r = chado_query($sql, $args);
foreach($r as $d) {
$arr_json[$c][] = $d->col;
}
}
}
return $arr_json;
}
/**
* Function callback: Generate data for the bar chart.
*
* @see hook_menu()
*
* @return
* A JSON containing the bins and data.
* bins is a list of computed bins and each data
* has location and bin objects.
*
* NOTE: The following column headers are excluded from the traits that can be visualized.
* Planting Date (date)
* Disease-specific Comments (text)
* Comments (text)
* # of Seeds Planted (count)
*/
function rawpheno_rawdata_trait_json() {
// When the type of data is txt, we need to ensure that it not sooo long that it will break the chart.
// To ensure that, we use this variable to check if the length is less or more and respond accordingly.
$max_data_length = 10;
// Types of data.
$txt_type = 'txt';
$int_type = 'int';
$scale_type = 'scale';
$ynunsure_type = 'y/n/?';
// Valid categorize options.
$arr_valid_category = array('location', 'year');
// Ensure project id and trait id are valid.
// Must exists, must not be empty and must not be more that 10 chars/digits.
// 10 Digits more will throw a PSQL ERROR.
// Project Id : project id
// Trait Id : trait id
// Option : year or location values
// Category : year or location
if ((!isset($_GET['project_id']) OR empty($_GET['project_id']) OR strlen($_GET['project_id']) >= 10) OR
(!isset($_GET['trait_id']) OR empty($_GET['trait_id']) OR strlen($_GET['trait_id']) >= 10) OR
(!isset($_GET['option']) OR empty($_GET['option']) OR strlen($_GET['option']) >= 20) OR
(!isset($_GET['category']) OR empty($_GET['category']) OR !in_array($_GET['category'], $arr_valid_category))) {
// No data.
return 0;
}
else {
$project_id = (int)trim(strip_tags($_GET['project_id']));
$trait_id = (int)trim(strip_tags($_GET['trait_id']));
// Get cvterm.
$c = array('cvterm_id' => $trait_id, 'cv_id' => array('name' => 'phenotype_measurement_types'));
if (function_exists('chado_get_cvterm')) {
$cvterm = chado_get_cvterm($c);
}
else {
$cvterm = tripal_get_cvterm($c);
}
if (!$cvterm) {
return 0;
}
// Query unique values in in the column, get data type and char length of each unique entry.
// CASE 1 - predict the data type of the value.
// CASE 2 - determine the lenght of the value.
$sql = "SELECT
value AS data,
CHAR_LENGTH(value) AS data_length
FROM {pheno_measurements}
WHERE type_id =