-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtripal_seq.module
604 lines (543 loc) · 22.5 KB
/
tripal_seq.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
<?php
/**
* Let's include some files
*/
// Admin
require_once 'includes/admin/tseq.dblist.inc';
require_once 'includes/admin/tseq.settings.inc';
require_once 'includes/admin/tseq.categories.inc';
// Classes
require_once 'includes/tseq_databases.inc';
// API
require_once 'api/tseq.api.inc';
// Forms (allow for more than just basic form)
require_once 'includes/tseq_submit.form.inc';
// Theme
require_once 'theme/tseq.theme.inc';
// Output
require_once 'includes/tseq.review.inc';
require_once 'includes/tseq.download.inc';
/**
* Implements hook_permission
*/
function tripal_seq_permission() {
return array (
'administer diamond' => array (
'title' => t ( 'Administer Diamond Module' ),
'description' => t ( 'Allows an admin to modify Tripal Sequence Similarity search configuration' )
),
);
}
function tripal_seq_init()
{
}
/*
* Implements hook_menu
*/
function tripal_seq_menu()
{
$items = array();
// TSeq Job submission form (for URL)
$items['TSeq/submit'] = array(
'title' => 'Sequence Similarity Search: Diamond/BLAST',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_submit_form'),
'access callback' => TRUE,
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
// TSeq Results page
$items['TSeq/results/%'] = array(
'title' => 'Diamond Job Results',
'page callback' => 'tseq_review',
'page arguments' => array(2),
'access callback' => TRUE,
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
// TSeq Download page
$items['TSeq/results/download/%/%'] = array(
'title' => 'TSeq Job Results Download',
'page callback' => 'tseq_download',
'page arguments' => array(3,4),
'access callback' => TRUE,
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
// Configuration pages
// Configuration: Main page
$items['admin/tripal/extension/tseq/config'] = array(
'title' => 'TSeq Configuration',
'description' => 'Configuration for the Tripal Sequence Similarity Search module',
'page callback' => 'tseq_config_dblist_page_build',
'page arguments'=> array('tseq_config_dblist_page'),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/admin/tseq.dblist.inc',
'file path' => drupal_get_path('module','tripal_seq'),
'access arguments' => array('administer diamond'),
);
// Configuration: Database list, first page
$items['admin/tripal/extension/tseq/config/dblist'] = array(
'title' => 'Available Databases',
'description' => 'Tripal Sequence Similarity Search Admin Configuration page',
'page callback' => 'tseq_config_dblist_page_build',
'page arguments' => array('tseq_config_dblist_page'),
'access arguments' => array('administer diamond'),
'type' => MENU_DEFAULT_LOCAL_TASK ,
'file' => 'includes/admin/tseq.dblist.inc',
'file path' => drupal_get_path ('module', 'tripal_seq')
);
// Configuration: Categories Tab
$items['admin/tripal/extension/tseq/config/categories'] = array(
'title' => 'Categories',
'type' => MENU_LOCAL_TASK,
'weight' => 1,
'priority' => 1,
'page callback' => 'tseq_config_categories_page_build',
'page arguments'=> array('tseq_config_categories_page'),
'access arguments' => array('administer diamond'),
'file' => 'includes/admin/tseq.categories.inc',
'file path' => drupal_get_path ('module', 'tripal_seq')
);
// Configuration: Settings Tab
$items['admin/tripal/extension/tseq/config/settings'] = array(
'title' => 'Job Settings',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
'priority' => 2,
'page callback' => 'drupal_get_form',
'page arguments'=> array('tseq_configuration_settings_form'),
'access arguments' => array('administer diamond'),
'file' => 'includes/admin/tseq.settings.inc',
'file path' => drupal_get_path ('module', 'tripal_seq')
);
// Configuration: Defaults Tab
$items['admin/tripal/extension/tseq/config/defaults'] = array(
'title' => 'Submission Defaults',
'type' => MENU_LOCAL_TASK,
'weight' => 3,
'priority' => 3,
'page callback' => 'drupal_get_form',
'page arguments'=> array('tseq_configuration_defaults_form'),
'access arguments' => array('administer diamond'),
'file' => 'includes/admin/tseq.defaults.inc',
'file path' => drupal_get_path ('module', 'tripal_seq')
);
//Admin: Genome Browser Link Scheme Tab
/*$items['admin/tripal/extension/tseq/config/browseLink'] = array(
'title' => 'Genome Browser Link',
'type' => MENU_LOCAL_TASK,
'weight' => 4,
'priority' => 4,
'page callback' => 'tseq_config_browseLink_page_build',
'page arguments'=> array('1', 'tseq_config_browseLink_page'),
'access callback' => TRUE,
'access arguments' => array('administer diamond'),
'file' => 'includes/admin/tseq.browseLink.inc',
'file path' => drupal_get_path ('module', 'tripal_seq')
);
*
*/
// Admin: add a new target database
$items['admin/tripal/extension/tseq/config/add_db'] = array(
'title' => 'Add an Existing Database',
'description' => 'Tripal Diamond Admin Configuration page',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_database_add_form',5),
'access arguments' => array('administer diamond'),
'type' => MENU_LOCAL_ACTION,
);
// Admin: edit an existing database
$items['admin/tripal/extension/tseq/edit_db/%'] = array(
'title' => 'Tripal Diamond Database Edit',
'description' => 'Tripal Diamond Admin Configuration page',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_database_edit_form',5),
'access arguments' => array('administer diamond'),
'type' => MENU_CALLBACK
);
// Admin: delete an existing database
$items['admin/tripal/extension/tseq/delete_db/%'] = array(
'title' => 'Tripal Diamond Database Delete',
'description' => 'Tripal Diamond Admin Configuration page',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_database_delete_form', 5),
'access arguments' => array('administer diamond'),
'type' => MENU_CALLBACK
);
// Add/Edit/Delete Categories
// Admin: add a new category
$items['admin/tripal/extension/tseq/add_category'] = array(
'title' => 'Add Category',
'description' => 'Tripal Diamond Admin Configuration page',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_category_add_form',5),
'access arguments' => array('administer diamond'),
'type' => MENU_CALLBACK,
);
// Admin: edit an existing category
$items['admin/tripal/extension/tseq/edit_category/%'] = array(
'title' => 'Tripal Diamond Category Edit',
'description' => 'Tripal Diamond Admin Configuration page',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_category_edit_form',5),
'access arguments' => array('administer diamond'),
'type' => MENU_CALLBACK
);
// Admin: delete an existing category
$items['admin/tripal/extension/tseq/delete_category/%'] = array(
'title' => 'Tripal Diamond Database Delete',
'description' => 'Tripal Diamond Admin Configuration page',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_category_delete_form', 5),
'access arguments' => array('administer diamond'),
'type' => MENU_CALLBACK
);
/*
// Admin: add a new Genome Browser Link Scheme
$items['admin/tripal/extension/diamond/add_browseLink'] = array(
'title' => 'TSeq Genome Browser Link Scheme Add',
'description' => 'Tripal Diamond Admin Configuration page',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_browseLink_add_form',5),
'access callback' => TRUE,
'access arguments' => array('administer diamond'),
'type' => MENU_CALLBACK,
);
// Admin: edit an existing Genome Browser Link Scheme
$items['admin/tripal/extension/diamond/edit_browseLink/%'] = array(
'title' => 'TSeq Genome Browser Link Scheme Edit',
'description' => 'Tripal Diamond Admin Configuration page',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_browseLink_edit_form',5),
'access callback' => TRUE,
'access arguments' => array('administer diamond'),
'type' => MENU_CALLBACK
);
// Admin: delete an existing Genome Browser Link Scheme
$items['admin/tripal/extension/diamond/delete_browseLink/%'] = array(
'title' => 'TSeq Genome Browser Link Scheme Delete',
'description' => 'Tripal Diamond Admin Configuration page',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_browseLink_delete_form', 5),
'access callback' => TRUE,
'access arguments' => array('administer diamond'),
'type' => MENU_CALLBACK
);
*
*/
//Admin: Export the database list
$items['admin/tripal/extension/tseq/config/export'] = array(
'title' => 'TSeq Database Export (CSV)',
'description' => 'Export the database list to a CSV file for backup/transfer',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_database_export_form'),
'access callback' => TRUE,
'access arguments' => array('administer diamond'),
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
);
//Admin: Import a saved database list
$items['admin/tripal/extension/tseq/config/import'] = array(
'title' => 'TSeq Database Import (CSV)',
'description' => 'Import to the database list from a CSV file',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_database_import_form'),
'access callback' => TRUE,
'access arguments' => array('administer diamond'),
'type' => MENU_LOCAL_ACTION,
'weight' => 2,
);
//Admin: Refresh the database list
$items['admin/tripal/extension/tseq/config/refresh'] = array(
'title' => 'Refresh this list',
'description' => 'Check every database file to update the status.',
'page callback' => 'drupal_get_form',
'page arguments' => array('tseq_database_list_refresh_form'),
'access callback' => TRUE,
'access arguments' => array('administer diamond'),
'type' => MENU_LOCAL_ACTION,
'weight' => 3,
);
// Analysis page
$items['user/%/tseq_analysis'] = array (
'title' => 'TSeq Analysis',
'description' => 'Show results of Diamond or BLAST jobs belonging to the user',
'page callback' => 'tseq_user_analysis_page',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
'file' => 'includes/tseq.user.inc',
'file path' => drupal_get_path('module', 'tripal_seq'),
);
return $items;
}
function tripal_seq_theme()
{
$items = array();
$path = drupal_get_path('module', 'tripal_seq');
// Displays the BLAST results for each job
$items['tseq_results'] = array(
'template' => 'tseq_review',
'path' => "$path/theme",
);
return $items;
}
// Email to users
/**
* Implements hook_mail
*/
function tripal_seq_mail($key, &$message, $params) {
$site_name = variable_get('site_name', "");
/*
* Get information from the job (tseq_job_information)
*/
$job_id = $params['job']->getJobID();
$job_information = tseq_get_job_information($job_id); // from table: tseq_job_information
$job_results = tseq_get_job_results($job_id); // from table: tseq_results
$job_human_readable = tseq_get_program($job_information['sequence_query_type'],$job_information['database_search_type']);
global $base_url;
switch ($key) {
case 'job-started':
$user = $params['user'];
$message['subject'] = t("Your Sequence Similary Search Job has started");
$message['body'][] = "The ".$job_human_readable[0]." job you submitted to ".$site_name." has started.";
$message['body'][] = "You can view the progress by <a href=\"".$base_url."/TSeq/results/".$job_information['tripal_job_id']."\">clicking here</a>.";
$message['body'][] = "or by visiting this URL: ".$base_url."/TSeq/results/".$job_information['tripal_job_id'];
$message['body'][] = "Job details:";
$message['body'][] = "Job ID: ".$job_id;
$message['body'][] = "Submission date: ".format_date($job_information['submit_time'], 'short');
//$message['body'][] = "Number of sequences submitted: ".$job_results['sequence_count'];
break;
case 'job-complete':
$user = $params['user'];
$message['subject'] = t("Your Sequence Similary Search Job has finished");
$message['body'][] = "The ".$job_human_readable[0]." job you submitted to ".$site_name." has finished.";
$message['body'][] = "You can view the progress by <a href=\"".$base_url."/TSeq/results/".$job_information['tripal_job_id']."\">clicking here</a>.";
$message['body'][] = "or by visiting this URL: ".$base_url."/TSeq/results/".$job_information['tripal_job_id'];
$message['body'][] = "Job details:";
$message['body'][] = "Job ID: ".$job_id;
$message['body'][] = "Submission date: ".format_date($job_information['submit_time'], 'short');
$message['body'][] = "Number of sequences submitted: ".$job_results['sequence_count'];
$message['body'][] = "Number of matches found: ".$job_results['matches'];
break;
case 'job-error':
$message['subject'] = t("TSeq Job: Error");
$message['body'][] = "There was an issue with the TSeq job";
break;
}
}
/**
* @param integer $tripal_job_id Job ID that TSeq is being notified about
* @param string $key one-word status of job
*
* Allow Tripal Remote Job to notify this module that something has happened
*/
function tripal_seq_trj_notify($tripal_job_id,$key)
{
switch ($key)
{
case "started":
break;
case "completed":
publish_results($tripal_job_id);
break;
default:
// handle errors PUMPKIN
break;
}
}
/**
* @param int $job_id Tripal Job ID of the completed job
*
* Publish the results to the database so they can be viewed on the results page and in email notifications immediately
*/
function publish_results($job_id)
{
// Get the Job information to be used throughout this function
$job_information = tseq_get_job_information($job_id);
$outputPath = DRUPAL_ROOT.'/sites/default/files/tripal/jobs/';
// Do the results of this job exist in the database?
$resultsFound = FALSE;
if (!tseq_results_exist($job_id)) // No
{
//Prepare the data to be inserted into table `tseq_results`
/*
* tripal_job_id $job_id
* user_id $job_information['user_id'];
* tseq_db_id tseq_get_db_id_by_location($job_information['database_file'])
* summary ?
* result_type assume BLAST XML
* data ...
* matches ...
* database_used
*/
$user_id = $job_information['user_id'];
$tseq_db_id = tseq_get_db_id_by_location($job_information['database_file']);
$result_type = "BLAST XML";
// Get status and data
$matches_found = 0;
//Database used
if ($job_information['database_file_type'] == 'database')
{
// Make some spaghetti getting this information because the sequence database name isn't stored in the tseq db
$tseq_db_id = tseq_get_db_id_by_location($job_information['database_file']);
$db_info = tseq_get_db_info($tseq_db_id);
//drupal_set_message("Database used: ".$db_info['name'].", version ".$db_info['version']." (".$db_info['category'].")");
$database_used = $db_info['name'].", version ".$db_info['version']." (".$db_info['type']." - ".$db_info['category'].")";
}
else
{
$database_used = '';
}
//Sequences queried
$query_file_no_path = explode('/',$job_information['sequence_file']);
$query_file = $outputPath.$job_id.'/'.$query_file_no_path[count($query_file_no_path)-1];
if (file_exists($query_file))
{
$sequence_count = tseq_get_query_sequence_count($query_file);
}
else
{
$sequence_count = -1;
}
// Look in the results files to see what has been returned.
$blastXML_filename = '/results_blastxml.txt';
$pairwise_filename = '/results_pairwise.txt';
if (file_exists($outputPath.$job_id.$blastXML_filename) && file_exists($outputPath.$job_id.'/STDERR.txt'))
{
//Prepare output if there is any data to show in $blastXML_filename
if (filesize($outputPath.$job_id.'/STDERR.txt') > 0)
{
$jobResults = file($outputPath.$job_id.'/STDERR.txt');
$resultsFound = TRUE;
foreach($jobResults as $resultLine)
{
// Check to see if there were any matches.
if (strcmp($resultLine, "0 queries aligned.") == 0)
{
$resultsFound = FALSE;
$summary = "no results found";
$data = "no results found";
}
}
if ($resultsFound)
{
$summary = "results found";
$data = 'Look for data in tseq_results_data table';
}
// Gather other details
//Matches found
$matches_found = tseq_get_matches_count($outputPath.$job_id.$blastXML_filename);
}
else if (filesize($outputPath.$job_id.'/STDERR.txt') > 0)
{
$data = file_get_contents($outputPath.$job_id.'/STDERR.txt');
$summary = "error";
}
else
{
$data = "There were no matches found.";
$summary = "error";
}
}
else
{
$data = "Results not found. Please contact an adminstrator and reference Tripal Job Number: ".$job_id; // This means there was no STDERR or STDOUT files, and possibly even the job dir (most likely due to system)
$summary = "error";
if (file_exists($query_file))
{
$sequence_count = 0;
}
$database_used ='';
}
// Pairwise/alignment view
/*
if (file_exists($outputPath.$job_id.$pairwise_filename)) // File exists
{
if (filesize($outputPath.$job_id.$pairwise_filename) > 0) // File has contents
{
// TODO Determine contents of pairwise results with 0 matches
$pairwise_results = file($outputPath.$job_id.$pairwise_filename);
foreach ($pairwise_results as $pairwise_result)
}
}*/
// and finally, insert (if there is anything to insert)
// Results details (always insert these)
$newDB = array(
'tripal_job_id' => $job_id,
'user_id' => $user_id,
'tseq_db_id' => $tseq_db_id,
'data' => $data,
'summary' => $summary,
'result_type' => $result_type,
'sequence_count'=> $sequence_count,
'matches' => $matches_found,
'database_used' => $database_used,
);
drupal_write_record('tseq_results', $newDB); // Insert the job details
// Get the actual results file, not the STDOUT file
$jobResults = file($outputPath.$job_id.$blastXML_filename);
// Results data (Insert these only if they exist)
if ($resultsFound)
{
foreach($jobResults as $key => $jobResult)
{
$columns = explode("\t", $jobResult);
$job_results_data[$key] = array(
'tripal_job_id' => $job_id,
'match_id' => $key,
'query_label' => $columns[0],
'target' => $columns[1],
'percent_identity' => $columns[2],
'alignment_length' => $columns[3],
'mismatches' => $columns[4],
'gap_opens' => $columns[5],
'start_position_query' => $columns[6],
'end_position_query' => $columns[7],
'start_position_database' => $columns[8],
'end_position_database' => $columns[9],
'e_value' => $columns[10],
'bit_score' => $columns[11],
);
drupal_write_record('tseq_results_data',$job_results_data[$key]);
}
}
}
}
/**
* Update existing db location status
*/
function tripal_seq_update_db_status() {
// Get the list of databases stored in the database
$databases = tseq_get_all_dbs();
$status = array();
foreach($databases as $database) {
$status[$database->db_id] = [$database->db_id, TSeqDatabases::db_check($database->db_id, $database->location)];
}
foreach($status as $update) {
db_update('tseq_db_existing_locations')
->fields(array(
'status' => $update[1],
))
->condition('db_id', $update[0], '=')
->execute();
}
// Save the current time in the settings table (tseq_settings.db_last_refresh)
$now = time();
db_update('tseq_settings')
->fields(array(
'db_last_refresh' => $now,
))
->condition('settings_id', 0, '=')
->execute();
}
/**
* Implements hook_cron()
*/
function tripal_seq_cron() {
$args = [];
$includes = [];
tripal_add_job('Cron: Checking TSeq database file status',
'tripal_seq', 'tripal_seq_update_db_status', $args, 0, 1, $includes, TRUE);
}