-
Notifications
You must be signed in to change notification settings - Fork 3
/
synview.module
executable file
·503 lines (433 loc) · 18.8 KB
/
synview.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
<?php
/**
* @file
* The main file for the tripal go module.
*/
// Application Programmers Interface
require_once 'api/synview.api.inc';
// insert/delete synteny blocks according to synteny file
require_once 'includes/synview.syn_parser.inc';
// synteny Node functionality
require_once 'includes/synview.node.inc';
// Functions specific to themeing (ie: preprocess)
require_once 'theme/synview.theme.inc';
// Administration for furture
//require_once 'includes/synview.admin.inc';
/**
* Implements hook_menu().
*/
function synview_menu() {
// search syntenic block form
$items['synview/search'] = array(
'title' => 'Search Syntenic Blocks',
'page callback' => 'drupal_get_form',
'page arguments' => array('synview_search_form'),
'access arguments' => array('access content'),
'file' => 'includes/synview_search_form.inc',
'type' => MENU_NORMAL_ITEM,
);
// display search result
$items['synview/search/result/%'] = array(
'title' => 'Search Result for Syntenic Blocks',
'page callback' => 'synview_search_result',
'page arguments' => array(3),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
// synteny (MCScanX) dataset Node
$items['node__syn'] = array(
'template' => 'node--syn',
'render element' => 'node',
'base hook' => 'node',
'path' => 'themesyn',
);
// Display Syntenic Block
$items['synview/block/%'] = array(
'title' => 'Display Syntenic Blocks',
'page callback' => 'display_block',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
// help
$items['admin/tripal/extension/tripal_synview/help'] = array(
'title' => 'Help',
'page callback' => 'theme',
'page arguments' => array('synview_help'),
'access arguments' => array('administer tripal'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_theme().
*/
function synview_theme() {
$items = array();
$path = drupal_get_path('module', 'synview');
// explain the detail
// this hook create a array of theme through array item
// the key in array is like show_goenrich_report
// the value tell drupal where to find template page
// template: template name, and file must has suffix .tpl.php
// path: where to find template file
// use theme('key') to call the them
// search result
$items['synview_search_result'] = array(
'template' => 'synview_search_result',
'path' => "$path/theme",
);
// Displays the pwy enrich results for each job
$items['display_block'] = array(
'template' => 'display_block',
'path' => "$path/theme",
);
// Module Help
$items['synview_help'] = array(
'template' => 'synview_help',
'path' => "$path/theme",
);
return $items;
}
/**
* Implements hook_help().
*/
function synview_help($path, $arg) {
if ($path == 'admin/help#synview') {
return theme('synview_help');
}
}
/**
* Dispaly single block
*
* @param $block_id
*
* @return $result
* Return HTML output of the block to be displayed to the user
*
*/
function display_block($block_id) {
return theme('display_block', array('block_id' => $block_id));
}
/**
* retrieve blocks from drupal database according to location
*
* @param org_id chr start end array_of_ref_org_id
*
* @return $block
* array of searched blocks
*/
function synview_search_result($action) {
if (!array_key_exists('tripal_synview_search', $_SESSION)) {
drupal_goto('synview/search');
}
$org_id = $_SESSION['tripal_synview_search']['SELECT_GENOME'];
$chr = $_SESSION['tripal_synview_search']['SELECT_CHR'];
//$start = $_SESSION['tripal_synview_search']['START'];
//$end = $_SESSION['tripal_synview_search']['END'];
$ref_org = $_SESSION['tripal_synview_search']['REFORG'];
$end_pos = $_SESSION['tripal_synview_search']['RLEN'][$org_id][$chr];
# define search mode, 0 for query_org is different with hit_org; 1 for query and hit org are same
$search_mode = 0;
if (array_key_exists($org_id, $ref_org)) {
$search_mode = 1;
}
//dpm($_SESSION['tripal_synview_search']);
if ($action) {
if ($action == 'left') {
if ($start > 100000) {
$start = $start - 1000000;
} else {
$start = 1;
}
if ($end > 1000000) {
$end = $end - 1000000;
}
}
elseif ($action == 'right') {
if ($end < $end_pos - 1000000) {
$end = $end + 1000000;
} else {
$end = $end_pos;
}
if ($start < $end - 1000000) {
$start = $start + 1000000;
}
}
else {
}
}
//$_SESSION['tripal_synview_search']['START'] = $start;
//$_SESSION['tripal_synview_search']['END'] = $end;
$org_opts = get_organism_options();
// search block on one organism of select chrosome
// find block between start and end, also cover start and end,
$args = array(
':type_name' => 'syntenic_region',
':org_id' => $org_id,
':chr' => $chr
);
$where_location = '';//"AND
// ((FL.fmin < :start AND FL.fmax > :start) OR
// (FL.fmin < :end AND FL.fmax > :end) OR
// (FL.fmin > :start AND FL.fmax < :end)
// )";
//$args[':start'] = $start;
//$args[':end'] = $end;
$sql = "SELECT F.feature_id, F.name, FL.srcfeature_id, FL.fmin, FL.fmax, FL.strand FROM chado.feature F
LEFT JOIN chado.cvterm CVT ON F.type_id = CVT.cvterm_id
LEFT JOIN chado.featureloc FL ON F.feature_id = FL.feature_id
WHERE
CVT.name = :type_name AND F.organism_id = :org_id AND FL.srcfeature_id = :chr
$where_location
ORDER BY FL.fmin
";
$results = chado_query( $sql, $args);
// get all the feature_id of selected synteny_region
// then retrieve all corresponding synteny_regions in other organisms of these selected synteny_region
$search_feature_id = array();
$search_results = array();
foreach ($results as $r) {
$search_feature_id[] = $r->feature_id;
$search_results[$r->feature_id] = $r;
}
$sql = "SELECT S.blockid, S.b1, F.organism_id as org1, S.b2, T.organism_id as org2, S.score, S.evalue FROM synblock S
LEFT JOIN chado.feature F ON S.b1 = F.feature_id
LEFT JOIN chado.feature T ON S.b2 = T.feature_id
WHERE S.b1 IN (:bid) OR S.b2 IN (:bid)";
$res = chado_query($sql, array(':bid' => $search_feature_id));
// filter the result by $ref_org
// only the blocks in match ref_org will be exported
$blocks = array();
// the array syn_region used for store region information
$syn_region = array();
// jdata: array for json data
$jdata = array();
$sql_loc = "SELECT FL.srcfeature_id, FL.feature_id, FL.fmin, FL.fmax, FL.strand FROM chado.featureloc FL
WHERE FL.feature_id = :feature_id";
foreach ($res as $b) {
// filter the reference organism
// b1 and b2 are selected org
if ( $b->org1 == $org_id and $b->org2 == $org_id and $search_mode == 1 )
{
$loc1 = chado_query($sql_loc, array(':feature_id'=>$b->b1))->fetchObject();
$b->b1_org = $org_opts[$b->org1];
$b->b1_sid = $loc1->srcfeature_id;
$b->b1_chr = $_SESSION['tripal_synview_search']['REF'][$b->org1][$loc1->srcfeature_id];
$b->b1_len = $_SESSION['tripal_synview_search']['RLEN'][$b->org1][$loc1->srcfeature_id];
$b->b1_fmin = $loc1->fmin;
$b->b1_fmax = $loc1->fmax;
$b->b1_strand = $loc1->strand;
$loc2 = chado_query($sql_loc, array(':feature_id'=>$b->b2))->fetchObject();
$b->b2_org = $org_opts[$b->org2];
$b->b2_sid = $loc2->srcfeature_id;
$b->b2_chr = $_SESSION['tripal_synview_search']['REF'][$b->org2][$loc2->srcfeature_id];
$b->b2_len = $_SESSION['tripal_synview_search']['RLEN'][$b->org2][$loc2->srcfeature_id];
$b->b2_fmin = $loc2->fmin;
$b->b2_fmax = $loc2->fmax;
$b->b2_strand = $loc2->strand;
$blocks[$b->blockid] = $b;
// build array for json
if (!array_key_exists($b->org1, $jdata) ) {
$jdata[$b->org1] = array();
$jdata[$b->org1]['name'] = $org_opts[$b->org1];
$jdata[$b->org1]['canvas'] = 'canvas' . $b->org1;
$jdata[$b->org1]['ideoCollection'] = array();
$jdata[$b->org1]['bedCollection'] = array();
$jdata[$b->org1]['linkCollection'] = array();
$jdata[$b->org1]['labCollection'] = array();
}
if (!array_key_exists($b->b1_sid, $jdata[$b->org1]['ideoCollection']) ) {
$jdata[$b->org1]['ideoCollection'][$b->b1_sid] = array();
$jdata[$b->org1]['ideoCollection'][$b->b1_sid]['id'] = $b->b1_sid;
$jdata[$b->org1]['ideoCollection'][$b->b1_sid]['chr'] = $b->b1_chr;
$jdata[$b->org1]['ideoCollection'][$b->b1_sid]['length'] = $b->b1_len / 1000000;
$loc1->srcfeature_id == $_SESSION['tripal_synview_search']['SELECT_CHR'] ? $jdata[$b->org1]['ideoCollection'][$b->b1_sid]['color'] = 'blue' : $jdata[$b->org1]['ideoCollection'][$b->b1_sid]['color'] = 'red';
}
if (!array_key_exists($b->b2_sid, $jdata[$b->org1]['ideoCollection']) ) {
$jdata[$b->org1]['ideoCollection'][$b->b2_sid] = array();
$jdata[$b->org1]['ideoCollection'][$b->b2_sid]['id'] = $b->b2_sid;
$jdata[$b->org1]['ideoCollection'][$b->b2_sid]['chr'] = $b->b2_chr;
$jdata[$b->org1]['ideoCollection'][$b->b2_sid]['length'] = $b->b2_len / 1000000;
$loc2->srcfeature_id == $_SESSION['tripal_synview_search']['SELECT_CHR'] ? $jdata[$b->org1]['ideoCollection'][$b->b2_sid]['color'] = 'blue' : $jdata[$b->org1]['ideoCollection'][$b->b2_sid]['color'] = 'red';
}
if (!array_key_exists($b->blockid, $jdata[$b->org1]['bedCollection']) ) {
$jdata[$b->org1]['bedCollection'][] = array(
'chr' => $b->b1_sid,
'name' => $b->b1_chr,
'start' => $b->b1_fmin / 1000000,
'end' => $b->b1_fmax / 1000000,
'id' => $b->blockid, 'color' => 'green');
$jdata[$b->org1]['bedCollection'][] = array(
'chr' => $b->b2_sid,
'name' => $b->b2_chr,
'start' => $b->b2_fmin / 1000000,
'end' => $b->b2_fmax / 1000000,
'id' => $b->blockid, 'color' => 'green');
$jdata[$b->org1]['linkCollection'][] = array(
'bid' => $b->blockid,
'source' => $jdata[$b->org1]['bedCollection'][count($jdata[$b->org1]['bedCollection'])-2],
'target' => $jdata[$b->org1]['bedCollection'][count($jdata[$b->org1]['bedCollection'])-1]);
}
}
// b1 is ref (hit) orgs, b2 is select (query) org
elseif ( array_key_exists($b->org1, $ref_org) and $b->org2 == $org_id )
{
$loc1 = chado_query($sql_loc, array(':feature_id'=>$b->b1))->fetchObject();
$b->b1_org = $org_opts[$b->org1];
$b->b1_sid = $loc1->srcfeature_id;
$b->b1_chr = $_SESSION['tripal_synview_search']['REF'][$b->org1][$loc1->srcfeature_id];
$b->b1_len = $_SESSION['tripal_synview_search']['RLEN'][$b->org1][$loc1->srcfeature_id];
$b->b1_fmin = $loc1->fmin;
$b->b1_fmax = $loc1->fmax;
$b->b1_strand = $loc1->strand;
$r = $search_results[$b->b2];
$b->b2_org = $org_opts[$b->org2];
$b->b2_sid = $r->srcfeature_id;
$b->b2_chr = $_SESSION['tripal_synview_search']['REF'][$b->org2][$r->srcfeature_id];
$b->b2_len = $_SESSION['tripal_synview_search']['RLEN'][$b->org2][$r->srcfeature_id];
$b->b2_fmin = $r->fmin;
$b->b2_fmax = $r->fmax;
$b->b2_strand = $r->strand;
$blocks[$b->blockid] = $b;
// build array for json
if (!array_key_exists($b->org1, $jdata) ) {
$jdata[$b->org1] = array();
$jdata[$b->org1]['name'] = $org_opts[$b->org1];
$jdata[$b->org1]['canvas'] = 'canvas' . $b->org1;
$jdata[$b->org1]['ideoCollection'] = array();
$jdata[$b->org1]['bedCollection'] = array();
$jdata[$b->org1]['linkCollection'] = array();
$jdata[$b->org1]['labCollection'] = array();
}
if (!array_key_exists($b->b1_sid, $jdata[$b->org1]['ideoCollection']) ) {
$jdata[$b->org1]['ideoCollection'][$b->b1_sid] = array();
$jdata[$b->org1]['ideoCollection'][$b->b1_sid]['id'] = $b->b1_sid;
$jdata[$b->org1]['ideoCollection'][$b->b1_sid]['chr'] = $b->b1_chr;
$jdata[$b->org1]['ideoCollection'][$b->b1_sid]['length'] = $b->b1_len / 1000000;
$loc1->srcfeature_id == $_SESSION['tripal_synview_search']['SELECT_CHR'] ? $jdata[$b->org1]['ideoCollection'][$b->b1_sid]['color'] = 'blue' : $jdata[$b->org1]['ideoCollection'][$b->b1_sid]['color'] = 'red';
}
if (!array_key_exists($b->b2_sid, $jdata[$b->org1]['ideoCollection']) ) {
$jdata[$b->org1]['ideoCollection'][$b->b2_sid] = array();
$jdata[$b->org1]['ideoCollection'][$b->b2_sid]['id'] = $b->b2_sid;
$jdata[$b->org1]['ideoCollection'][$b->b2_sid]['chr'] = $b->b2_chr;
$jdata[$b->org1]['ideoCollection'][$b->b2_sid]['length'] = $b->b2_len / 1000000;
$r->srcfeature_id == $_SESSION['tripal_synview_search']['SELECT_CHR'] ? $jdata[$b->org1]['ideoCollection'][$b->b2_sid]['color'] = 'blue' : $jdata[$b->org1]['ideoCollection'][$b->b2_sid]['color'] = 'red';
}
if (!array_key_exists($b->blockid, $jdata[$b->org1]['bedCollection']) ) {
$jdata[$b->org1]['bedCollection'][] = array(
'chr' => $b->b1_sid,
'name' => $b->b1_chr,
'start' => $b->b1_fmin / 1000000,
'end' => $b->b1_fmax / 1000000,
'id' => $b->blockid, 'color' => 'green');
$jdata[$b->org1]['bedCollection'][] = array(
'chr' => $b->b2_sid,
'name' => $b->b2_chr,
'start' => $b->b2_fmin / 1000000,
'end' => $b->b2_fmax / 1000000,
'id' => $b->blockid, 'color' => 'green');
$jdata[$b->org1]['linkCollection'][] = array(
'bid' => $b->blockid,
'source' => $jdata[$b->org1]['bedCollection'][count($jdata[$b->org1]['bedCollection'])-2],
'target' => $jdata[$b->org1]['bedCollection'][count($jdata[$b->org1]['bedCollection'])-1]);
}
}
// b2 is ref orgs, b1 is select org
elseif ( array_key_exists($b->org2, $ref_org) and $b->org1 == $org_id )
{
$r = $search_results[$b->b1];
$b->b1_org = $org_opts[$b->org1];
$b->b1_sid = $r->srcfeature_id;
$b->b1_chr = $_SESSION['tripal_synview_search']['REF'][$b->org1][$r->srcfeature_id];
$b->b1_len = $_SESSION['tripal_synview_search']['RLEN'][$b->org1][$r->srcfeature_id];
$b->b1_fmin = $r->fmin;
$b->b1_fmax = $r->fmax;
$b->b1_strand = $r->strand;
$loc2 = chado_query($sql_loc, array(':feature_id'=>$b->b2))->fetchObject();
$b->b2_org = $org_opts[$b->org2];
$b->b2_sid = $loc2->srcfeature_id;
$b->b2_chr = $_SESSION['tripal_synview_search']['REF'][$b->org2][$loc2->srcfeature_id];
$b->b2_len = $_SESSION['tripal_synview_search']['RLEN'][$b->org2][$loc2->srcfeature_id];
$b->b2_fmin = $loc2->fmin;
$b->b2_fmax = $loc2->fmax;
$b->b2_strand = $loc2->strand;
$blocks[$b->blockid] = $b;
// build array for json
if (!array_key_exists($b->org2, $jdata) ) {
$jdata[$b->org2] = array();
$jdata[$b->org2]['name'] = $org_opts[$b->org2];
$jdata[$b->org2]['canvas'] = 'canvas' . $b->org2;
$jdata[$b->org2]['ideoCollection'] = array();
$jdata[$b->org2]['bedCollection'] = array();
$jdata[$b->org2]['linkCollection'] = array();
$jdata[$b->org2]['labCollection'] = array();
}
if (!array_key_exists($b->b1_sid, $jdata[$b->org2]['ideoCollection']) ) {
$jdata[$b->org2]['ideoCollection'][$b->b1_sid] = array();
$jdata[$b->org2]['ideoCollection'][$b->b1_sid]['id'] = $b->b1_sid;
$jdata[$b->org2]['ideoCollection'][$b->b1_sid]['chr'] = $b->b1_chr;
$jdata[$b->org2]['ideoCollection'][$b->b1_sid]['length'] = $b->b1_len / 1000000;
$r->srcfeature_id == $_SESSION['tripal_synview_search']['SELECT_CHR'] ? $jdata[$b->org2]['ideoCollection'][$b->b1_sid]['color'] = 'blue' : $jdata[$b->org2]['ideoCollection'][$b->b1_sid]['color'] = 'red';
}
if (!array_key_exists($b->b2_sid, $jdata[$b->org2]['ideoCollection']) ) {
$jdata[$b->org2]['ideoCollection'][$b->b2_sid] = array();
$jdata[$b->org2]['ideoCollection'][$b->b2_sid]['id'] = $b->b2_sid;
$jdata[$b->org2]['ideoCollection'][$b->b2_sid]['chr'] = $b->b2_chr;
$jdata[$b->org2]['ideoCollection'][$b->b2_sid]['length'] = $b->b2_len / 1000000;
$loc2->srcfeature_id == $_SESSION['tripal_synview_search']['SELECT_CHR'] ? $jdata[$b->org2]['ideoCollection'][$b->b2_sid]['color'] = 'blue' : $jdata[$b->org2]['ideoCollection'][$b->b2_sid]['color'] = 'red';
}
if (!array_key_exists($b->blockid, $jdata[$b->org2]['bedCollection']) ) {
$jdata[$b->org2]['bedCollection'][] = array(
'chr' => $b->b1_sid,
'name' => $b->b1_chr,
'start' => $b->b1_fmin / 1000000,
'end' => $b->b1_fmax / 1000000,
'id' => $b->blockid, 'color' => 'green');
$jdata[$b->org2]['bedCollection'][] = array(
'chr' => $b->b2_sid,
'name' => $b->b2_chr,
'start' => $b->b2_fmin / 1000000,
'end' => $b->b2_fmax / 1000000,
'id' => $b->blockid, 'color' => 'green');
$jdata[$b->org2]['linkCollection'][] = array(
'bid' => $b->blockid,
'source' => $jdata[$b->org2]['bedCollection'][count($jdata[$b->org2]['bedCollection'])-2],
'target' => $jdata[$b->org2]['bedCollection'][count($jdata[$b->org2]['bedCollection'])-1]);
}
}
else
{
}
}
$jdata_tmp = array();
foreach ($jdata as $id => $data) {
$data['ideoCollection'] = array_values($data['ideoCollection']);
$jdata_tmp[] = $data;
}
$json_data = json_encode($jdata_tmp);
$json_data = "var data = $json_data";
drupal_add_js($json_data, array('type'=>'inline', 'scope'=>'footer','weight' => 50));
$cluster = array();
return theme('synview_search_result', array('cluster'=> $cluster, 'blocks' => $blocks, 'jdata' => $jdata_tmp));
}
/**
* load synteny blocks of synteny file into chado database
*
* @param $node
*
* generate job args, then trigger a tripal job for parsing synteny file and loading blocks
*/
function synteny_file_load_job($node)
{
global $user;
if ($node->syn_name) {
$job_args = array(
$node->syn_org1,
$node->syn_org2,
$node->syn_path,
);
tripal_add_job("Parse synteny file: $node->syn_path", 'tripal_synview',
'synview_parse_synfile', $job_args, $user->uid);
}
}