-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbagel4_promoter_term_2_json.pl
executable file
·153 lines (122 loc) · 4.8 KB
/
bagel4_promoter_term_2_json.pl
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
#!/usr/bin/env perl
# Anne de Jong, October 2017
# Add promoters and terminiators to json
use strict ;
use warnings ;
use lib "/usr/bagel4/lib" ;
use bagel4_functions ;
use lib "/usr/molgentools/lib";
use anne_files ;
use anne_genomics ;
use File::Basename;
# ---------------------------------------------------------- parameters -------------------------------------------------------------------------
my $sessiondir = '/usr/bagel4/test';
my $program_dir = dirname($0) ;
my $queryname ;
my $usage = "option:
-s sessiondir [default=$sessiondir]
-queryname queryname of the table names. e.g. AOI_1
reads the promoters and terminators table and convert it to a JSON file
promoters tab delimited table [pos strand description] queryname.promoters
terminators tab delimited table [pos strand description] queryname.transterm.table
e.g. /usr/bagel4/bagel4_promoter_term_2_json.pl -s /usr/bagel4/test -queryname NC_0085331
" ;
my $yline_hight = 10 ;
my $prom_hight = 2 ;
my $prom_point = 0.4 ;
my $term_hight = 2 ;
my $term_loop = 0.4 ;
&parseparam() ;
# ----------------------------------------------------------- main ------------------------------------------------------------------------------
print "Adding Promoter and Terminator data to JSON\n";
my %fasta = anne_genomics::fasta2hash("$sessiondir/$queryname.AOI.fna") ; # read the MULTI fasta file AND the AOI names
my $yline = 10;
foreach my $queryname (sort keys %fasta) {
print "\tAdding $queryname\n";
my $region_size = length($fasta{$queryname}) ; # get the region size for scaling
my @promoter_elements ;
my @terminator_elements ;
# Add the promoters elements
my @lines = anne_files::read_lines("$sessiondir/$queryname.promoters");
foreach my $line (@lines) {
my @items = split "\t", $line ;
if (scalar @items >2) {
print "\t\tpromoter\t$line\n";
my $position = 100 * $items[0] / $region_size ; # rescale to 100
my @element = '{' ;
push @element, '"name": "promoter",' ;
push @element, '"color": "#2d65bf",' ;
push @element, '"yline": '.$yline.',';
push @element, "\"xtext\": $position,";
push @element, "\"annotation\":\"$items[2]\",";
push @element, "\"points\": [".add_promoter_polygon($position, $items[1])."]}" ;
push @promoter_elements, (join "\n\t", @element)."\n" ; # add each element to elements
}
}
# Add the terminators elements
@lines = anne_files::read_lines("$sessiondir/$queryname.transterm");
foreach my $line (@lines) {
if ($line =~ m/^(\d+)\t(.)\t(.*)/) {
print "\t\tterminator\t$line\n";
my $position = 100 * $1 / $region_size ; # rescale to 100
my $ori = 1; $ori = -1 if ($2 eq '+') ;
my @element = '{' ;
push @element, '"name": "terminator",' ;
push @element, '"color": "#ba4a28",' ;
push @element, '"yline": '.$yline.',';
push @element, "\"xtext\": $position,";
push @element, "\"annotation\":\"$3\",";
push @element, "\"stem\": [".json_coord($position,0).",".json_coord($position,$term_hight*$ori)."]," ;
push @element, "\"loop\": ".json_circle($position,$term_hight*$ori,$term_loop)."}" ;
push @terminator_elements, (join "\n\t", @element)."\n" ; # add each element to elements
}
}
# make the final JSON
my @json = '{ '; # json start and end with { }
push @json, '"promoters": [';
push @json, join ',', @promoter_elements ;
push @json, "]," ;
push @json, '"terminators": [';
push @json, join ',', @terminator_elements ;
push @json, "]";
push @json, "\n}" ; # end JSON with }
anne_files::write_lines("$sessiondir/$queryname.prom_term.json", @json) ;
}
#print (join /\t/,@json) ;
#print "\n";
# ----------------------------------------------------------------------------- functions ----------------------------------------------------------
sub json_circle {
my ($cx,$cy, $r) = @_ ;
return "{\"cx\":$cx,\"cy\":$cy,\"r\":$r}";
}
sub json_coord {
my ($x,$y) = @_ ;
return "{\"x\":$x,\"y\":$y}";
}
sub add_promoter_polygon {
my ($position, $strand) = @_ ;
my @result ;
if ($strand eq '+') {
push @result, json_coord($position,0) ;
push @result, json_coord($position,-$prom_hight) ;
push @result, json_coord($position+$prom_point, -$prom_hight+($prom_point/2)) ;
push @result, json_coord($position, -$prom_hight+$prom_point) ;
} else {
push @result, json_coord($position,0) ;
push @result, json_coord($position,$prom_hight) ;
push @result, json_coord($position-$prom_point, $prom_hight-($prom_point/2)) ;
push @result, json_coord($position, $prom_hight-$prom_point) ;
}
return join ',', @result ;
}
sub parseparam {
my $var ;
my @arg = @ARGV ;
while(@arg) {
$var = shift(@arg) ;
die $usage if ($var eq '-h' or $var eq '--help') ;
$sessiondir = shift(@arg) if($var eq '-s') ;
$queryname = shift(@arg) if($var eq '-queryname') ;
}
die $usage if (!$queryname) ;
}