forked from tripal/tripal_galaxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tripal_galaxy.module
executable file
·1461 lines (1335 loc) · 46.4 KB
/
tripal_galaxy.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 module for for the tripal_galaxy module.
*/
require 'theme/tripal_galaxy.theme.inc';
require 'api/tripal_galaxy.api.inc';
require 'includes/tripal_galaxy.form_elements.inc';
require 'includes/tripal_galaxy.webform.inc';
require 'includes/tripal_galaxy.results.inc';
/**
* Implements hook_init().
*/
function tripal_galaxy_init() {
global $user;
$library = libraries_detect('blend4php');
if (user_access('administer galaxy', $user)) {
if (!$library) {
drupal_set_message(t('The blend4php library is not installed. The Galaxy module requires it.'), 'warning');
}
}
}
/**
* Implements hook_libraries_info().
*/
function tripal_galaxy_libraries_info() {
// A very simple library. No changing APIs (hence, no versions), no variants.
// Expected to be extracted into 'sites/all/libraries/simple'.
$libraries['blend4php'] = [
'name' => 'blend4php',
'vendor url' => 'https://github.com/galaxyproject/blend4php',
'download url' => 'https://github.com/galaxyproject/blend4php',
'version arguments' => [
'file' => 'version.txt',
'pattern' => '/^(.*)$/',
'lines' => 1,
],
'files' => [
'php' => [
'galaxy.inc',
],
],
];
return $libraries;
}
/**
* Implements hook_permissions().
*
* Set the permission types that this module uses.
*/
function tripal_galaxy_permission() {
return [
'use galaxy' => [
'title' => t('Execute Published Galaxy Workflows'),
'description' => t('Allows a user to submit a published workflow for execution on a remote Galaxy Instance'),
],
'administer galaxy' => [
'title' => t('Administer Galaxy'),
'description' => t('Allows a user to configure site-wide default Galaxy instances.'),
],
];
}
/**
* Implements hook_menu().
*
* Specifies menu items and URLs used by this module.
*/
function tripal_galaxy_menu() {
$items = [];
// EXPLANATION: all extension modules should have an administrative menu item
// with the path set to 'admin/tripal/extension/[module name]'.
$items['admin/tripal/extension/galaxy'] = [
'title' => 'Galaxy',
'description' => 'Integrate workflows from a remote Galaxy instance with this Tripal site',
'page callback' => 'tripal_galaxy_admin_home',
'access arguments' => [
'administer galaxy',
],
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/tripal_galaxy.admin.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
// Edit Galaxy.
$items['admin/tripal/extension/galaxy/edit/%'] = [
'description' => 'Edit a galaxy instance',
'page callback' => 'drupal_get_form',
'page arguments' => [
'tripal_galaxy_admin_configuration_form',
5,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin_configuration_form.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
// Test Galaxy.
$items['admin/tripal/extension/galaxy/test/%'] = [
'description' => 'Test a galaxy instance',
'page callback' => 'tripal_galaxy_admin_test_server',
'page arguments' => [
5,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
// Disable workflows for this Galaxy.
$items['admin/tripal/extension/galaxy/disable/%'] = [
'description' => 'Disable all workflows for this galaxy server',
'page callback' => 'tripal_galaxy_admin_disable_workflows',
'page arguments' => [
5,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
// Enable workflows for this Galaxy.
$items['admin/tripal/extension/galaxy/enable/%'] = [
'description' => 'Enable all workflows for this galaxy server',
'page callback' => 'tripal_galaxy_admin_enable_workflows',
'page arguments' => [
5,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
// Add Galaxy.
$items['admin/tripal/extension/galaxy/add'] = [
'description' => 'Add a galaxy server instance',
'title' => 'Add Galaxy Instance',
'page callback' => 'drupal_get_form',
'page arguments' => [
'tripal_galaxy_admin_configuration_form',
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/tripal_galaxy.admin_configuration_form.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
$items['admin/tripal/extension/galaxy/delete/%'] = [
'description' => 'Delete a galaxy server instance',
'title' => 'Delete Galaxy Instance',
'page callback' => 'drupal_get_form',
'page arguments' => [
'tripal_galaxy_admin_delete_galaxy_instance_form',
5,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin_configuration_form.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
// Available Workflows.
$items['admin/tripal/extension/galaxy/workflows'] = [
'title' => 'Workflows',
'description' => 'Lists the available Workflows of the user',
'page callback' => 'drupal_get_form',
'page arguments' => [
'tripal_galaxy_admin_workflows_form',
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_LOCAL_TASK,
'file' => 'includes/tripal_galaxy.admin_workflow_form.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
'weight' => 5,
];
// Add Workflows.
$items['admin/tripal/extension/galaxy/workflows/add'] = [
'title' => 'Add Workflows',
'description' => 'Add workflows to the site given the remote galaxy instance',
'page callback' => 'drupal_get_form',
'page arguments' => [
'tripal_galaxy_admin_add_workflow_form',
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/tripal_galaxy.admin_workflow_form_add.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
// Remove Workflows.
$items['admin/tripal/extension/galaxy/workflows/remove/%'] = [
'description' => 'Remove a workflow from site',
'page callback' => 'drupal_get_form',
'page arguments' => [
'tripal_galaxy_admin_confirm_remove_workflow_form',
6,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
'weight' => 10,
];
// Disable Workflows.
$items['admin/tripal/extension/galaxy/workflows/disable/%'] = [
'description' => 'Disable this workflow',
'page callback' => 'tripal_galaxy_admin_disable_workflow',
'page arguments' => [
6,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
'weight' => 10,
];
// Enable Workflows.
$items['admin/tripal/extension/galaxy/workflows/enable/%'] = [
'description' => 'Enable this workflow',
'page callback' => 'tripal_galaxy_admin_enable_workflow',
'page arguments' => [
6,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
'weight' => 10,
];
$items['admin/tripal/extension/galaxy/workflows/settings/%'] = [
'description' => 'Settings for the workflow',
'page callback' => 'drupal_get_form',
'page arguments' => [
'tripal_galaxy_admin_workflow_settings_form',
6,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
'weight' => 10,
];
// Admin submission report.
$items['admin/tripal/extension/galaxy/workflows/report/%'] = [
'title' => 'Workflow Submission Details',
'description' => 'The report for a submitted workflow.',
'page callback' => 'tripal_galaxy_submission_admin_report',
'page arguments' => [
6,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
'weight' => 10,
];
//
// USER SUBMITTED WORKFLOW JOBS
//
// User submission report.
$items['user/%/galaxy-jobs/%'] = [
'title' => 'Analysis Results',
'description' => 'The results for a submitted analysis.',
'page callback' => 'tripal_galaxy_submission_user_report',
'page arguments' => [
3,
],
'access arguments' => [
'use galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.user.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
'weight' => 10,
];
// User report page.
$items['user/%/galaxy-jobs/%/results'] = [
'title' => 'Workflow Submission Files',
'description' => 'The files from a submitted workflow.',
'page callback' => '',
'page arguments' => [
3,
],
'access arguments' => [
'use galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
'weight' => 10,
];
// Usage.
$items['admin/tripal/extension/galaxy/usage'] = [
'title' => 'Usage',
'description' => 'View Usage of Galaxy Workflows',
'page callback' => 'tripal_galaxy_admin_usage_page',
'access arguments' => [
'administer galaxy',
],
'type' => MENU_LOCAL_TASK,
'file' => 'includes/tripal_galaxy.admin_usage.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
'weight' => 20,
];
//
// SITE-WIDE FILES
// .
$items['admin/tripal/extension/galaxy/files'] = [
'title' => 'Files',
'description' => 'Files available site-wide for workflows.',
'page callback' => 'tripal_galaxy_admin_manage_files',
'access arguments' => [
'administer galaxy',
],
'type' => MENU_LOCAL_TASK,
'file' => 'includes/tripal_galaxy.admin_files.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
'weight' => 40,
];
$items['admin/tripal/extension/galaxy/files/add'] = [
'title' => 'Add a New File',
'description' => 'Files available site-wide for workflows.',
'page callback' => 'drupal_get_form',
'page arguments' => [
'tripal_galaxy_admin_add_file_form',
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/tripal_galaxy.admin_files.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
$items['admin/tripal/extension/galaxy/files/disable/%'] = [
'description' => "Disable a file.",
'page callback' => 'tripal_galaxy_admin_disable_file',
'page arguments' => [
6,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin_files.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
$items['admin/tripal/extension/galaxy/files/enable/%'] = [
'description' => "Enable a file.",
'page callback' => 'tripal_galaxy_admin_enable_file',
'page arguments' => [
6,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin_files.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
$items['admin/tripal/extension/galaxy/files/edit/%'] = [
'title' => 'Edit File Details',
'description' => "Edit a site-wide file.",
'page callback' => 'drupal_get_form',
'page arguments' => [
'tripal_galaxy_admin_edit_file_form',
6,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin_files.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
$items['admin/tripal/extension/galaxy/files/delete/%'] = [
'title' => 'Delete File',
'description' => "Deletes a site-wide file.",
'page callback' => 'drupal_get_form',
'page arguments' => [
'tripal_galaxy_admin_delete_file_form',
6,
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_CALLBACK,
'file' => 'includes/tripal_galaxy.admin_files.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
$items['galaxy/proxy/%'] = [
'title' => 'Results Viewer',
'page callback' => 'tripal_galaxy_stream_url_proxy',
'page arguments' => [
2,
],
'access arguments' => [
'use galaxy',
],
'type' => MENU_CALLBACK,
];
$items['galaxy/viewer/%'] = [
'title' => 'Results Viewer',
'page callback' => 'tripal_galaxy_results_viewer_page',
'page arguments' => [
2,
],
'access arguments' => [
'use galaxy',
],
'type' => MENU_CALLBACK,
];
$items['galaxy/viewer-full/%'] = [
'title' => 'Results Viewer',
'page callback' => 'tripal_galaxy_results_viewer_full_page',
'page arguments' => [
2,
],
'access arguments' => [
'use galaxy',
],
'type' => MENU_CALLBACK,
];
$items['galaxy/download/%'] = [
'title' => 'Download',
'page callback' => 'tripal_galaxy_results_download',
'page arguments' => [
2,
],
'access arguments' => [
'use galaxy',
],
'type' => MENU_CALLBACK,
];
// SETTINGS
// Available Workflows.
$items['admin/tripal/extension/galaxy/settings'] = [
'title' => 'Settings',
'description' => 'Galaxy Settings',
'page callback' => 'drupal_get_form',
'page arguments' => [
'tripal_galaxy_admin_settings_form',
],
'access arguments' => [
'administer galaxy',
],
'type' => MENU_LOCAL_TASK,
'file' => 'includes/tripal_galaxy.admin_settings_form.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
'weight' => 50,
];
return $items;
}
/**
* Implements hook_menu_alter().
*/
function tripal_galaxy_menu_alter(&$items) {
$items['node/%webform_menu/webform/export-workflow'] = [
'title' => 'Export',
'page callback' => 'export_tripal_galaxy_workflow',
'page arguments' => [
1,
],
'access arguments' => [
'access content',
],
'weight' => 9,
'type' => MENU_LOCAL_TASK,
];
$items['node/%webform_menu/webform/import-workflow'] = [
'title' => 'Import',
'page callback' => 'import_tripal_galaxy_workflow',
'page arguments' => [
1,
],
'access arguments' => [
'access content',
],
'weight' => 10,
'type' => MENU_LOCAL_TASK,
'file' => 'includes/tripal_galaxy.import_webform.form.inc',
'file path' => drupal_get_path('module', 'tripal_galaxy'),
];
}
/**
* Implements hook_views_api().
*
* This hook tells drupal that there is views
* support for for this module which then automatically includes the
* tripal_db.views.inc where all the views integration code is found.
*
* @ingroup
* tripal_galaxy
*/
function tripal_galaxy_views_api() {
return [
'api' => 3.0,
];
}
/**
* Implements hook_theme().
*
* We need to let drupal know about our theme functions and their arguments.
* We create theme functions to allow users of the module to customize the
* look and feel of the output generated in this module @ingroup tripal_galaxy.
*/
function tripal_galaxy_theme($existing, $type, $theme, $path) {
$items = [
'html__galaxy__viewer_full' => [
'template' => 'html--galaxy--viewer-full',
'render element' => 'html',
'base hook' => 'html',
'path' => drupal_get_path('module', 'tripal_galaxy') . "/theme/templates",
],
'page__galaxy__viewer_full' => [
'template' => 'page--galaxy--viewer-full',
'render element' => 'page',
'base hook' => 'page',
'path' => drupal_get_path('module', 'tripal_galaxy') . "/theme/templates",
],
'tripal_galaxy_admin_add_workflow_form_rows' => [
'render element' => 'form',
],
'tripal_galaxy_admin_workflows_form_rows' => [
'render element' => 'form',
],
];
return $items;
}
/**
* Implements hook_form_alter().
*
* Add a submit handler to the client webform form.
*/
function tripal_galaxy_form_webform_client_form_alter(&$form, &$form_state, $form_id) {
$node = $form['#node'];
$workflow = db_select('tripal_galaxy_workflow', 'tgw')->fields('tgw')
->condition('nid', $node->nid)
->execute()
->fetchObject();
if ($workflow and $workflow->status == 'Altered') {
drupal_set_message('Unfortunately, this wokflow is deprecated and no longer
available for submissions.', 'error');
tripal_set_message('This form is disabled because the workflow was
altered on the remote Galaxy Instance and may be out of sync
with this web form. If you want to continue to offer this workflow
to your site visitors, you will need to ' . l(t('add the workflow'), 'admin/tripal/extension/galaxy/workflows') . ' again as a new web form on this site.', TRIPAL_ERROR);
$form['actions']['next']['#disabled'] = TRUE;
}
else {
if ($workflow and $workflow->status == 'disabled') {
drupal_set_message('Unfortunately, this wokflow is disabled and no longer
avaliable for submissions.', 'error');
tripal_set_message('This form is disabled you may ' . l(t('enable the workflow here'), 'admin/tripal/extension/galaxy') . '.', TRIPAL_ERROR);
$form['actions']['next']['#disabled'] = TRUE;
}
}
// Check if this particular webform really is a galaxy webform
// At each 'turn of the page' of the particular webform this is being called.
if ($form['#node']->webform['components'][1]['form_key'] == 'galaxy_webform') {
$form['#submit'][] = 'tripal_galaxy_webform_client_form_submit';
}
}
/**
* Submit function for the webform_client_form.
*/
function tripal_galaxy_webform_client_form_submit($form, &$form_state) {
global $user;
// Don't do anything if the webform isn't finished being filled out.
if ($form_state['webform_completed'] != TRUE) {
return;
}
try {
// Get the submissinon ID.
$wf_sid = $form_state['storage']['details']['sid'];
// Get the Galaxy server workflow ID from the form then
// lookup our internal galaxy workflow ID.
$workflow_id = $form['#node']->webform['components'][1]['extra']['workflow_id'];
$workflow = db_select('tripal_galaxy_workflow', 'tgw')->fields('tgw')
->condition('workflow_id', $workflow_id)
->execute()
->fetchObject();
// Create the workflow submission.
$tg_workflow = tripal_galaxy_init_submission($workflow, $user);
// Linker table.
$data = [
'wf_sid' => $wf_sid,
'tg_sid' => $tg_workflow,
];
drupal_write_record('tripal_galaxy_webform', $data);
// For any files that were uploaded we want to set the usage for those
// so that they are mapped to the correct submission.
foreach ($form_state['values']['submitted'] as $cid => $component) {
if (is_array($component) and array_key_exists('existing', $component)) {
$fids = [];
if(array_key_exists('existing', $component) and $component['existing']) {
$fids = explode('|', $component['existing']);
}
if(array_key_exists('site_wide', $component) and $component['site_wide']) {
$fids = explode('|', $component['site_wide']);
}
if(array_key_exists('data_collection', $component) and $component['data_collection']) {
$fids = explode('|', $component['data_collection']);
}
$keys = preg_grep('/^submitted_.*$/', array_keys($component));
$uploaded_key = array_shift($keys);
if ($uploaded_key and $component[$uploaded_key]) {
$fids = explode('|', $component[$uploaded_key]);
}
if ($fids) {
foreach ($fids as $fid) {
$file = file_load($fid);
file_usage_add($file, 'tripal_galaxy', 'submission', $tg_workflow);
}
}
}
}
$args = [$tg_workflow];
$includes = [];
$job_id = tripal_add_job("Galaxy Workflow #" . $tg_workflow, 'tripal_galaxy',
'tripal_galaxy_invoke_webform_submission', $args, $user->uid, 10, $includes,
$ignore_duplicates = FALSE);
if ($job_id) {
drupal_set_message(t('Your job is successfully submitted to the job queue! You will be notified by email when your job begins and completes.'));
}
else {
drupal_set_message(t('Your analysis workflow was successfully submitted, but the job could not be added. Please contact the site administrator to report this issue.', 'warning'));
}
}
catch (Exception $e) {
drupal_set_message(t('Could not complete workflow submission. Please contact the web site administrator to report this issue.'), 'error');
watchdog_exception('tripal_galaxy', $e);
}
drupal_goto('user/' . $user->uid . '/galaxy-jobs');
}
/**
* Implements hook_webform_component_info().
*
* Describes new components for webforms that Galaxy workflows will use.
*/
function tripal_galaxy_webform_component_info() {
$components = [];
$components['BDSS_file'] = [
'label' => t('BDSS File'),
'description' => t('Allows multiple paths (URL, upload, Tripal web services) that a file may be attached.'),
'features' => [
'csv' => TRUE,
'email' => FALSE,
'email_address' => FALSE,
'email_name' => FALSE,
'required' => FALSE,
'title_display' => FALSE,
'title_inline' => FALSE,
'conditional' => TRUE,
'group' => FALSE,
'spam_analysis' => FALSE,
'attachment' => FALSE,
'conditional_action_set' => TRUE,
],
'file' => 'includes/components/tripal_galaxy.BDSS_file.inc',
];
$components['galaxy_sfile'] = [
'label' => t('File'),
'description' => t('Provides a single file for a Galaxy input.'),
'features' => [
'csv' => TRUE,
'email' => FALSE,
'email_address' => FALSE,
'email_name' => FALSE,
'required' => FALSE,
'title_display' => FALSE,
'title_inline' => FALSE,
'conditional' => TRUE,
'group' => FALSE,
'spam_analysis' => FALSE,
'attachment' => FALSE,
'conditional_action_set' => TRUE,
],
'file' => 'includes/components/tripal_galaxy.galaxy_sfile.inc',
];
$components['galaxy_sflist'] = [
'label' => t('List Files'),
'description' => t('Provides a list of files for a Galaxy input.'),
'features' => [
'csv' => TRUE,
'email' => FALSE,
'email_address' => FALSE,
'email_name' => FALSE,
'required' => FALSE,
'title_display' => FALSE,
'title_inline' => FALSE,
'conditional' => TRUE,
'group' => FALSE,
'spam_analysis' => FALSE,
'attachment' => FALSE,
'conditional_action_set' => TRUE,
],
'file' => 'includes/components/tripal_galaxy.galaxy_sflist.inc',
];
$components['galaxy_pflist'] = [
'label' => t('List of Paired Files'),
'description' => t('Provides a list of paired files for a Galaxy input.'),
'features' => [
'csv' => TRUE,
'email' => FALSE,
'email_address' => FALSE,
'email_name' => FALSE,
'required' => FALSE,
'title_display' => FALSE,
'title_inline' => FALSE,
'conditional' => TRUE,
'group' => FALSE,
'spam_analysis' => FALSE,
'attachment' => FALSE,
'conditional_action_set' => TRUE,
],
'file' => 'includes/components/tripal_galaxy.galaxy_pflist.inc',
];
$components['fixed_value'] = [
'label' => t('Fixed Value'),
'description' => t('A fixed value set by the workflow programmer.'),
'features' => [
'csv' => TRUE,
'email' => FALSE,
'email_address' => FALSE,
'email_name' => FALSE,
'required' => FALSE,
'title_display' => FALSE,
'title_inline' => FALSE,
'conditional' => TRUE,
'group' => FALSE,
'spam_analysis' => FALSE,
'attachment' => FALSE,
'conditional_action_set' => TRUE,
],
'file' => 'includes/components/tripal_galaxy.fixed_value.inc',
];
return $components;
}
/**
* Webform submission presave function.
*/
function tripal_galaxy_webform_submission_presave($node, &$submission) {
// Iterate through the components being submitted and remove non
// needed elements added by our galaxy form elements.
foreach ($submission->data as $cid => $data) {
$component = db_select('webform_component', 'wc')->fields('wc', [
'type',
'form_key',
])
->condition('cid', $cid)
->execute()
->fetchObject();
if ($component->type == 'galaxy_sfile') {
unset($submission->data[$cid]['uploader_fset']);
unset($submission->data[$cid]['existing_fset']);
unset($submission->data[$cid]['site_wide_fset']);
// If we have a data collection then we need to copy the files out
// of the data collection and into the Galaxy files.
if (array_key_exists('data_collection_fset', $submission->data[$cid]) and $submission->data[$cid]['data_collection_fset']['data_collection']) {
$fid = $submission->data[$cid]['data_collection_fset']['data_collection'];
$file = file_load($fid);
$site_dir = tripal_galaxy_get_files_dir();
if (!$site_dir) {
drupal_set_message('Could not access the data collection to the directory on the server for storing this file. This analysis may not execute correctly.', 'error');
}
else {
$file = file_copy($file, $site_dir . '/' . $file->filename);
if (!$file) {
drupal_set_message('Could not copy the data collection to the directory on the server for storing this file. This analysis may not execute correctly.', 'error');
}
else {
$submission->data[$cid]['data_collection'] = $file->fid;
}
}
}
}
unset($submission->data[$cid]['data_collection_fset']);
if ($component->type == 'galaxy_pfile') {
unset($submission->data[$cid]['uploader_fset']);
unset($submission->data[$cid]['existing_fset']);
unset($submission->data[$cid]['site_wide_fset']);
}
if ($component->type == 'galaxy_pflist') {
unset($submission->data[$cid]['uploader_fset']);
unset($submission->data[$cid]['existing_fset']);
}
if ($component->type == 'galaxy_sflist') {
unset($submission->data[$cid]['uploader_fset']);
unset($submission->data[$cid]['existing_fset']);
}
}
}
/**
* Retrieves the workflow submission report for an admin user.
*
* @param int $sid
* The webform submission ID.
*/
function tripal_galaxy_submission_admin_report($sid) {
// Set the breadcrumb.
$breadcrumb = [];
$breadcrumb[] = l(t('Home'), '<front>');
$breadcrumb[] = l(t('Administration'), 'admin');
$breadcrumb[] = l(t('Tripal'), 'admin/tripal');
$breadcrumb[] = l(t('Extensions'), 'admin/tripal/extension');
$breadcrumb[] = l(t('Galaxy'), 'admin/tripal/extension/galaxy');
$breadcrumb[] = l(t('Job Queue'), 'admin/tripal/extension/galaxy/job-queue');
drupal_set_breadcrumb($breadcrumb);
return tripal_galaxy_workflow_report($sid);
}
/**
* Implements hook_cron().
*/
function tripal_galaxy_cron() {
$args = [];
// We only want to execute the following tasks on a specific interval. The
// cron may be set to run every 30 minutes but some of our Galaxy
// housekeeping activities only need to run every day at least. If the
// tripal_galaxy_cron_last variable has never been used before we'll set
// a default to just 1 second over the $hours_between_runs.
$hours_between_runs = variable_get('tripal_galaxy_cron_hours', 24);
$last_execution = variable_get('tripal_galaxy_cron_last', time() - ($hours_between_runs * 60 * 60 + 1));
$next_execution = $last_execution + ($hours_between_runs * 60 * 60);
if (time() > $next_execution) {
try {
tripal_galaxy_check_servers();
}
catch (Exception $e) {
drupal_set_message($e->getMessage(), 'error');
drupal_set_message(t('Cannot disable the workflows'), 'error');
watchdog_exception('tripal_galaxy', $e);
return;
}
// Remove old histories from the remote tripal server.
try {
tripal_galaxy_delete_expired_histories();
}
catch (Exception $e) {
watchdog_exception('tripal_galaxy', $e);
return;
}
variable_set('tripal_galaxy_cron_last', time());
}
}
/**
* Checks the remote Galaxy servers to make sure they are up and running.
*/
function tripal_galaxy_check_servers() {
// Retrieve the list of galaxy servers
$sql = "SELECT * FROM {tripal_galaxy}";
$results = db_query($sql);
while ($result = $results->fetchObject()) {
$server_status = tripal_galaxy_test_connection(['galaxy_id' => $result->galaxy_id,]);
if ($server_status === FALSE) {
// Update status in the db.
db_update('tripal_galaxy')->fields(['serverstatus' => 'unavailable'])
->condition('galaxy_id', $result->galaxy_id, '=')
->execute();
// Put a message on the dashboard.
$title = t('Galaxy Server %servername was unreachable when tested at %time', [
'%server' => $result->servername,
'%time' => format_date(time()),
]);
$details = t('Galaxy Server %servername was unreachable when tested at %time. Please check the server status. If the server is down you may disable the workflows associated with that server by going to the Tripal Galaxy admin page.', [
'%server' => $result->servername,
'%time' => format_date(time()),
]);
$actions['Disable Server\'s Workflows'] = 'admin/tripal/extension/galaxy/disable/' . $result->galaxy_id;
$type = 'Tripal Galaxy';
$submitter_id = 'tripal_galaxy' . $result->servername . time();
tripal_add_notification($title, $details, $type, $actions, $submitter_id);
}
else {
if ($server_status === TRUE) {
db_update('tripal_galaxy')->fields(['serverstatus' => 'available',])
->condition('galaxy_id', $result->galaxy_id, '=')
->execute();
// Make sure no notification is up about the server being down.
$submitter_id = 'tripal_galaxy' . $result->servername;
$note = db_select('tripal_admin_notfications', 'tan')->fields('tan')
->condition('submitter_id', $submitter_id, '=')
->execute()
->fetchAll();
if ($note) {
db_delete('tripal_admin_notfications')
->condition('submitter_id', $submitter_id)
->execute();
}
}
}
}
}
/**
* Checks the status of non completed workflows.
*
* This function is meant to be added by cron and run by the TripalJob's system.
*
* @param TripalJob $job
* An instance of a TripalJob object.
*/
function tripal_galaxy_check_status(TripalJob $job = NULL) {
// Update the status of running workflows.
$query = db_select('tripal_galaxy_workflow_submission', 'tgws');
$query->fields('tgws', [
'sid',
]);
$query->condition('tgws.status', [
'Error',
'Completed',
], 'NOT IN');
$submissions = $query->execute();
foreach ($submissions as $submission) {
tripal_galaxy_check_submission_status($submission->sid);
}
// Update the status of the workflows to see if any of them were