-
Notifications
You must be signed in to change notification settings - Fork 0
/
repeatAtlas
executable file
·464 lines (429 loc) · 11.3 KB
/
repeatAtlas
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
#!/usr/bin/perl -w
# Authors: Thomas Dybdal Pedersen
# For license see /usr/biotools/CMG-biotools.license
use strict;
use Getopt::Long;
use File::Copy;
my $location = "/media/sf_Dropbox/GitHub/circosScripts/";
#### INPUT CHECK
my $organism;
my $name = "chr1";
my $output;
my $bin = 2500;
my $cutlength = 100;
my $cutevalue = 1e-10;
my $pRule = "50/50";
my $repeats = 1;
my $paralogues = 1;
my $abs;
if ($#ARGV < 0){
print "\n";
print "USAGE: repeatAtlas -f <organismName> -b <binSize> -n <plotName> -o <outputFile> -abs\n";
print "Creates a repeat atlas of the organism given in <organismName> based on a genbank file.\n";
print "\n";
print "-f\tThe name of the gbk file to use; with or without the .gbk extension <REQUIRED>\n";
print "-b\tThe binsize to use for the lanes (default 2500)\n";
print "-n\tThe name to appear below the plot (default chr1)\n";
print "-o\tThe name of the plot file to create. Two files will be created: <outputFile>.png and <outputFile>.svg\n";
print "-abs\tFLAG. If set the binsize is read as bp, if not it is read as the number of bins\n";
print "-pRule\tThe rule that defines paralogues if these are plotted. The format is x/y where x is the 50% conserved\n\t\tand y is the length compared to the largest sequence (default 50/50)\n";
print "\n";
exit;
}
elsif ($#ARGV == 0){
$organism = $ARGV[0];
$organism =~ s/\.gbk//;
}
else {
GetOptions ("f=s" => \$organism,
"b=i" => \$bin,
"abs" => \$abs,
"n=s" => \$name,
"pRule=s" => \$pRule,
"rep=i" => \$repeats,
"para=i" => \$paralogues,
"o=s" => \$output);
if (defined $organism){
$organism =~ s/\.gbk//;
}
}
if(! $organism){
die "No organism defined: use -f <organism>\n";
}
if(! -e "${organism}.gbk"){
if($organism eq "1"){
die "No organism defined: use -f <organism>\n";
}
die "Missing genbank file: ${organism}.gbk\n";
}
if($output){
if($output eq "1"){
$output = $organism;
}
}
$name =~ s/\s/_/g;
#### END OF INPUT CHECK
#### PARSING DATA
my @tab = `saco_convert -I genbank -O tab ${organism}.gbk`;
chomp(my $length = `/usr/biotools/saco_convert -I genbank -O length $organism.gbk`);
if($bin){
$bin = $length/$bin unless $abs;
}
#### END OF DATA PARSING
#### CREATE DIRECTORIES
mkdir 'data';
mkdir 'etc';
#### END OF DIRECTORY CREATION
#### CREATE KARYOTYPE FILE
open KARYO, ">data/karyotype.txt";
print KARYO "chr - chr1 ".$name." 0 ".$length." purple";
close KARYO;
#### END OF KARYOTYPE CREATION
#### CREATING DATA
print STDOUT "Calculating baseskew...";
&baseskews;
print STDOUT "DONE\nCalculating stacking energy...";
&ornstein;
if($repeats){
print STDOUT "DONE\nBlasting for repeats...";
&repeats;
}
else{
open DIR, ">data/direct.txt";
close DIR;
open INV, ">data/inverted.txt";
close INV;
}
if($paralogues){
print STDOUT "DONE\nFinding paralogues...";
¶logue;
}
else{
open PARA, ">data/paralogue.txt";
close PARA;
}
print STDOUT "DONE\n";
&ann;
my $bigbin = int($bin*5);
`binLinks -bin_size ${bigbin} -links data/direct.txt -link_end 2 > data/hist.direct.txt`;
`binLinks -bin_size ${bigbin} -links data/inverted.txt -link_end 2 > data/hist.inverted.txt`;
#### END OF DATA CREATION
#### COPY OF CONFIG FILES
copy("${location}etc/repeatAtlas/circos.conf", 'etc/');
copy("${location}etc/repeatAtlas/customColors.conf", 'etc/');
copy("${location}etc/repeatAtlas/ideogram.conf", 'etc/');
copy("${location}etc/repeatAtlas/links.conf", 'etc/');
copy("${location}etc/repeatAtlas/plots.conf", 'etc/');
copy("${location}etc/repeatAtlas/ticks.conf", 'etc/');
#### END OF CONFIG COPY
#### SETUP RANGES
my @gcRange;
my @dataholder;
open GCI, '<data/gcSkew.txt';
while(<GCI>){
my @line = split;
chomp($line[3]);
push(@dataholder, $line[3]);
}
close GCI;
@gcRange = (&average(@dataholder) - 3*&stdev(@dataholder), &average(@dataholder) + 3*&stdev(@dataholder));
undef(@dataholder);
my @conf;
open PLOTCONF, "<etc/plots.conf";
while(<PLOTCONF>){
if(/_gcRANGE_/){
my $line = sprintf("min = %.2f\nmax = %.2f\n", $gcRange[0], $gcRange[1]);
push(@conf, $line);
}
else {
push(@conf, $_);
}
}
close PLOTCONF;
open NEWCONF, ">etc/plots.conf";
foreach(@conf){
print NEWCONF;
}
close NEWCONF;
#### END OF RANGE SETUP
#### CREATE PLOT
if($output){
`circos`;
move("circos.png", "${output}.png");
move("circos.svg", "${output}.svg");
}
#### END OF PLOTTING
#### SUBROUTINE DEFINITIONS
sub ann {
my @data = `/usr/biotools/saco_convert -I genbank -O annotation ${organism}.gbk`;
open ANN, ">data/rrna.txt";
foreach(@data){
my @line = split;
next unless $line[0] ne "source";
next unless $line[0] ne "gene";
if($line[4] eq "rRNA"){
print ANN "chr1 ".$line[1]." ".$line[2]." ".$line[4]."\n";
}
}
close ANN;
}
sub baseskews {
my @gcSkew;
my @atPercent;
my @gcBinned;
my @atBinned;
`/usr/biotools/saco_convert -A dna2 -I genbank -O skews ${organism}.gbk > temp`;
open TEMP, "<temp";
while(<TEMP>){
chomp;
my @line = split;
push(@gcSkew, $line[2]);
push(@atPercent, $line[3]);
}
close TEMP;
unlink("temp");
my $start;
my $end;
my $i;
my @gcSub;
my @atSub;
my $arrayLength = @gcSkew;
for($i=0; $i < $arrayLength; $i += $bin){
$start = int($i);
$end = int($i + $bin - 1);
$end = $arrayLength-1 unless $end < $arrayLength-1;
@gcSub = @gcSkew[$start .. $end];
@atSub = @atPercent[$start .. $end];
my $gcMean = &average(@gcSub);
my $atMean = &average(@atSub);
push(@gcBinned, "$start $end $gcMean");
push(@atBinned, "$start $end $atMean");
undef(@gcSub);
undef(@atSub);
undef($start);
undef($end);
}
open GC, ">data/gcSkew.txt";
open AT, ">data/atPercent.txt";
foreach(@gcBinned){
print GC "chr1 ".$_."\n";
}
foreach(@atBinned){
print AT "chr1 ".$_."\n";
}
close GC;
close AT;
}
sub ornstein {
my @temptab = @tab;
my $i;
my @ornstein;
my %val = (
"AA" => -5.37,
"AC" => -10.51,
"AG" => -6.78,
"AT" => -6.57,
"CA" => -6.57,
"CC" => -8.26,
"CG" => -9.61,
"CT" => -6.78,
"GA" => -9.81,
"GC" => -14.59,
"GG" => -8.26,
"GT" => -10.51,
"TA" => -3.82,
"TC" => -9.81,
"TG" => -6.57,
"TT" => -5.37
);
foreach (@temptab) {
$_ = uc;
my @seq = split(/\t/, $_);
for ($i = 0; $i+2 <= length($seq[1]); $i++) {
if (defined($val{substr($seq[1], $i, 2)})) {
push(@ornstein, $val{substr($seq[1], $i, 2)});
} else {
push(@ornstein, int(0));
}
}
}
undef(@temptab);
my $start;
my $end;
my @ornBinned;
my @ornSub;
my @mean;
my $arrayLength = @ornstein;
for($i=0; $i < $arrayLength; $i += $bin){
$start = int($i);
$end = int($i + $bin - 1);
$end = $arrayLength-1 unless $end < $arrayLength-1;
@ornSub = @ornstein[$start .. $end];
my $ornMean = &average(@ornSub);
push(@ornBinned, "$start $end $ornMean");
push(@mean, $ornMean);
undef(@ornSub);
undef($start);
undef($end);
}
my $transFac = 100;
my $mean = &average(@mean);
undef(@mean);
undef(@ornstein);
open ORN, ">data/ornstein.txt";
foreach(@ornBinned){
my @line = split;
my $trans = ($line[2]-$mean)*($transFac**(abs($line[2]-$mean)));
print ORN "chr1 ".$line[0]." ".$line[1]." ".$trans."\n";
}
close ORN;
}
sub repeats {
open FSA, ">", "${organism}.db";
select FSA;
foreach (@tab) {
my @tmp = split(/\t/, $_);
$tmp[0] = "XXXX";
my $w = 10000;
my $o = 5000;
my $l = length($tmp[1]);
for (my $i = 0; $i <= $l; $i += $o){
my $s2 = substr($tmp[1], $i, $w);
my $ls2 = length($s2);
if ($ls2 < $w) {
my $pad = $w-$ls2;
$pad = "X"x$pad;
$s2 = $s2.$pad;
}
$s2 =~ s/(.{60})/$1\n/g;
print ">".$tmp[0]."_".eval($i+1)."-".eval($i+$w+1)."\n";
print $s2."\n";
}
}
close FSA;
`formatdb -i ${organism}.db -p F -t ${organism}.db`;
`blastall -p blastn -F F -g F -d ${organism}.db -e 1 -i ${organism}.db -o ${organism}.blast`;
my @blasttable = `/usr/biotools/indirect/blastntable.pl ${organism}.blast`;
open DIRECT, ">data/direct.txt";
open INVERTED, ">data/inverted.txt";
my %hash;
my $n1;
my $n2;
foreach(@blasttable){
sub solveE {
my $ans;
my @input = @_;
my @char = split(//, $input[0]);
if ($char[0] eq "e"){
$ans = "1".$input[0];
} else {
$ans = $input[0];
}
$ans;
}
my @F = split;
$F[16] =~ /_([0-9]+)-[0-9]+$/;
my $c = $1+$F[10]-1;
my $d = $1+$F[11]-1;
$F[17] =~ /_([0-9]+)-[0-9]+$/;
my $e = $1+$F[13]-1;
my $f = $1+$F[14]-1;
my $g = solveE($F[7]);
if ($F[12] eq $F[15] && $d-$c >= $cutlength && $c != $e && $g < $cutevalue){
my $n1 = "chr1_${c}_${d}_chr1_${e}_${f}";
my $n2 = "chr1_${e}_${f}_chr1_${c}_${d}";
my $n3 = "chr1_${d}_${c}_chr1_${f}_${e}";
my $n4 = "chr1_${f}_${e}_chr1_${d}_${c}";
unless($hash{$n1}){
print DIRECT $n1." chr1 ".$c." ".$d." evalue=".$g."\n";
print DIRECT $n1." chr1 ".$e." ".$f." evalue=".$g."\n";
$hash{$n1} = 1;
$hash{$n2} = 1;
$hash{$n3} = 1;
$hash{$n4} = 1;
}
} elsif ($F[12] ne $F[15] && $d-$c >= $cutlength && $c != $e && $g < $cutevalue){
my $n1 = "chr1_${c}_${d}_chr1_${e}_${f}";
my $n2 = "chr1_${e}_${f}_chr1_${c}_${d}";
my $n3 = "chr1_${d}_${c}_chr1_${f}_${e}";
my $n4 = "chr1_${f}_${e}_chr1_${d}_${c}";
unless($hash{$n1}){
print INVERTED $n1." chr1 ".$c." ".$d." evalue=".$g."\n";
print INVERTED $n1." chr1 ".$e." ".$f." evalue=".$g."\n";
$hash{$n1} = 1;
$hash{$n2} = 1;
$hash{$n3} = 1;
$hash{$n4} = 1;
}
}
}
my @trash = ("formatdb.log", "${organism}.blast", "${organism}.db", "${organism}.db.nhr", "${organism}.db.nin", "${organism}.db.nsq");
foreach(@trash){
unlink;
}
}
sub paralogue{
`saco_convert -I genbank -O fasta ${organism}.gbk > temp.fna`;
`prodigalrunner temp.fna`;
my @trash = ("temp.fna", "temp.gff", "temp_prodigal.gbk", "temp_prodigal.orf.fna");
foreach(@trash){
unlink;
}
copy("temp_prodigal.orf.fsa", "database");
`formatdb -i database`;
`/usr/biotools/blast/bin/blastall -p blastp -F 0 -d database -e 1e-5 -m 8 -i temp_prodigal.orf.fsa -o temp.blast`;
open BLAST, "<temp.blast";
open OUT, ">data/paralogue.txt";
my %hash;
my @rules = split("/", $pRule);
$rules[1] = $rules[1]/100;
while(<BLAST>){
my @line = split;
next if ($line[0] eq $line[1]); #Test if two different CDS' are compared
next if ($line[2] < $rules[0]); #Test if conservation is above 50 %
next if ($line[3]/($line[7] < $line[9] ? $line[9] : $line[7]) < $rules[1]); #Test whether allignment constitutes over 50% of max length
my $comb1 = $line[0]."_".$line[1];
my $comb2 = $line[1]."_".$line[0];
next if $hash{$comb1};
$hash{$comb1} = 1;
$hash{$comb2} = 1;
$line[0] =~ /^.*_CDS_(\d+)-(\d+)$/ or die;
print OUT $comb1." chr1 ".$1." ".$2."\n";
$line[1] =~ /^.*_CDS_(\d+)-(\d+)$/;
print OUT $comb1." chr1 ".$1." ".$2."\n";
}
close BLAST;
close OUT;
undef(@trash);
@trash = ("database", "database.phr", "database.pin", "database.psq", "formatdb.log", "temp.blast", "temp_prodigal.orf.fsa");
foreach(@trash){
unlink;
}
}
sub average{
my @data = @_;
my $length = @data;
if ($length == 0) {
die("Empty array\n");
}
my $total = 0;
foreach (@data) {
$total += $_;
}
my $average = $total / $length;
return $average;
}
sub stdev{
my @data = @_;
my $length = @data;
if($length == 1){
return 0;
}
my $average = &average(@data);
my $sqtotal = 0;
foreach(@data) {
$sqtotal += ($average-$_) ** 2;
}
my $std = ($sqtotal / (@data-1)) ** 0.5;
return $std;
}