-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSignatures.pl
324 lines (303 loc) · 9.45 KB
/
getSignatures.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
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
#!/usr/bin/perl
use strict;
use File::Temp qw( tempfile tempdir ); # to make temporary files/directories
use sigtrap qw(handler signalHandler normal-signals);
use Getopt::Long;
use List::Util qw(sum);
#######################################################
###################### Arguments ######################
#######################################################
my $minSig = 2;
my $maxSig = 4;
my $defSig = 3;
my $defDNA = 1000;
my $defResDir = "DNASignature";
my $roundup = "%.4f";
##### controlable
my $fnaFile = '';
my $signatureLn = $defSig;
my $minDNAln = $defDNA;
my $resultsDir = '';
my $circular = "F";
my $options = GetOptions(
"i=s" => \$fnaFile,
"l=s" => \$signatureLn,
"m=s" => \$minDNAln,
"o=s" => \$resultsDir,
"c=s" => \$circular,
);
my $ownName = $0;
$ownName =~ s{.*/}{};
if( !$fnaFile ) {
print qq(about:\n);
print qq( This program produces files with DNA oligonucleotide\n)
. qq( counts and signatures\n\n);
print "usage: " . $ownName . " [options]\n";
print "\noptions:\n";
print " -i genome file [GCF_000005845.fna.gz], required\n";
print " -l signature length, default $defSig\n";
print " -m minimum DNA length worth calculating signatures,\n"
. " default $defDNA (as per Karlin)\n";
print " -c DNA is circular [T/F], default F\n";
print " -o output directory, default $defResDir-[signature length]\n";
print "\n";
exit;
}
my $fnaRoot = $fnaFile;
$fnaRoot =~ s{^\S*/}{};
$fnaRoot =~ s{\.fna\S*}{};
$fnaRoot =~ s{\.(gz|bz2)}{}g;
my $signatureLn
= $signatureLn >= $minSig && $signatureLn <= $maxSig ? $signatureLn
: $defSig;
my $circular = $circular =~ m{^(T|F)$}i ? uc($1) : "F";
my $minDNAln = $minDNAln > 50 ? $minDNAln : $defDNA;
print "\tworking with $fnaRoot signature size -> $signatureLn\n";
print "\t $fnaRoot is circular -> $circular\n";
my $resultsDir
= length($resultsDir) > 2 ? $resultsDir
: join("-",$defResDir,$signatureLn);
print "\t saving results at $resultsDir\n";
mkdir($resultsDir) unless(-d $resultsDir);
my $tempFolder = tempdir("/tmp/signatures.XXXXXXXXXXXX");
### pre establish the different oligonucleotides
### (not convenient for oligo sizes above 5)
my @bases = qw( A C G T );
my @nucl = @bases;
my $current_ln = 0;
while( $current_ln < $signatureLn ) {
my @grower = ();
for my $current ( @nucl ) {
for my $base ( @bases ) {
my $grown = $current . $base;
push(@grower,$grown);
}
}
$current_ln = length($grower[0]);
@nucl = @grower;
}
######### counting both sides:
my %rev_comp = reverse_complement(@nucl);
######### prepared above to count both sides
### learn genome DNA
#### working only with bzipped files
my $calculated = 0;
my $outFile = "$resultsDir/$fnaRoot.signature.bz2";
my $tmpFile = "$tempFolder/$fnaRoot.signature.bz2";
my $refSeq = slurpSeq("$fnaFile");
my $nuclHead = join("\t","Oligonucleotide",@nucl,"Total");
my %gnm_count = (); ### genomic counts
my $gnmGC = 0; ### GC content
my $gnmAT = 0; ### AT content
my $gnm_length = 0;
my @seqIDs
= sort { length($refSeq->{"$b"}) <=> length($refSeq->{"$a"}) }
keys %{ $refSeq };
open( my $SIG,"|-","bzip2 -9 >$tmpFile" );
SEQREAD:
for my $seqID ( @seqIDs ) {
my $seq = $refSeq->{"$seqID"};
if( length($seq) < $minDNAln ) {
last SEQREAD;
}
print STDERR "\tworking with $seqID\n";
my($cGC,$cAT,$pGC,$clean) = getGC($seq);
my($rCounts,$dsCounts) = countOligos($seq);
my @calculations
= calculate_signatures($pGC,$dsCounts);
$calculated++;
print {$SIG}
join("\t","Piece",$seqID,"GC",$cGC,"AT",$cAT,"fGC",$pGC
,"Length",length($seq)),"\n";
print {$SIG} join("\n",$nuclHead,@calculations),"\n";
### genomic additions:
for my $oligo ( keys %{ $dsCounts } ) {
$gnm_count{"$oligo"} += $dsCounts->{"$oligo"};
}
$gnm_length += length($seq);
$gnmGC += $cGC;
$gnmAT += $cAT;
}
### now the genomic calculations
if( $calculated > 0 ) {
print STDERR "\tworking the whole thing\n";
my $pgc = sprintf("$roundup",($gnmGC/($gnmGC + $gnmAT)));
print {$SIG}
join("\t","Whole",$fnaRoot,"GC",$gnmGC,"AT",$gnmAT,"fGC",$pgc
,"Length",$gnm_length),"\n";
my @calculations
= calculate_signatures($pgc,\%gnm_count);
print {$SIG} join("\n",$nuclHead,@calculations),"\n";
close($SIG);
system("mv $tmpFile $outFile 2>/dev/null");
}
else {
close($SIG);
unlink("$resultsDir/$fnaRoot.signature.bz2");
}
if( -d "$tempFolder" ) {
print "\tcleaning up ...\n";
system "rm -r $tempFolder";
print "\tdone!\n\n";
}
else {
print "\ttemp files cleared out\n";
print "\tdone!\n\n";
}
sub reverse_complement {
my @nucl = @_;
my %rev_comp = ();
for my $nucl ( @nucl ) {
my $rev_comp = reverse($nucl);
$rev_comp =~ tr{ATGC}{TACG};
$rev_comp{"$nucl"} = $rev_comp;
}
return(%rev_comp);
}
sub calculate_signatures {
my($pGC,$nucl_count) = @_;
my $pAT = 1 - $pGC;
my $item_total = sum( values %{ $nucl_count } );
my $total_prop = 0;
my $total_prob = 0;
my $total_expt = 0;
my @line_prop = ("Proportion");
my @line_prob = ("Probability");
my @line_count = ("Count");
my @line_expt = ("Expected");
my @line_sign = ("Signature");
for my $nucl ( @nucl ) {
my $count = $nucl_count->{"$nucl"};
push(@line_count,$count);
##### now proportions and signatures
my $prop = $count/$item_total;
$total_prop += $prop;
my $print_prop = sprintf("%.5f",$prop);
push(@line_prop,$print_prop);
my $gc = $nucl =~ tr{GC}{GC};
my $at = $nucl =~ tr{AT}{AT};
#print "$nucl\t( $pGC**$gc ) * ( $pAT**$at )\n";
my $probability = ( ($pGC/2)**$gc ) * ( ($pAT/2)**$at );
$total_prob += $probability;
my $print_prob = sprintf("%.5f",$probability);
push(@line_prob,$print_prob);
my $expt_calc = $item_total * $probability;
my $expt = sprintf("%.1f",$expt_calc);
push(@line_expt,$expt);
$total_expt += $expt;
my $sign = $expt > 0 ? sprintf("%.5f",($count/$expt)) : 0;
#my $sign = sprintf("%.5f",($prop/$probability));
push(@line_sign,$sign);
}
my @lines_to_print = ();
my $line_prop = join("\t",@line_prop,$total_prop);
my $line_prob = join("\t",@line_prob,$total_prob);
my $line_count = join("\t",@line_count,$item_total);
my $line_expt = join("\t",@line_expt,$total_expt);
my $line_sign = join("\t",@line_sign);
my @lines_to_print
= ($line_prop,$line_prob,$line_count,$line_expt,$line_sign);
return(@lines_to_print);
}
sub signalHandler {
if( -d "$tempFolder" ) {
print "\n\tcleaning up ...\n";
system "rm -r $tempFolder";
die " done!\n\n";
}
else {
print "\n\ttemp files cleared out\n";
die " done!\n\n";
}
}
sub figureCompression {
my $rootName = $_[0];
$rootName =~ s{\.(gz|bz2|Z)$}{};
my $fullName
= ( -f "$rootName.gz" ) ? "$rootName.gz"
: ( -f "$rootName.Z" ) ? "$rootName.Z"
: ( -f "$rootName.bz2" ) ? "$rootName.bz2"
: ( -f "$rootName" ) ? "$rootName"
: "none";
my $catProg
= $fullName =~ m{\.(gz|Z)$} ? "gzip -qdc"
: $fullName =~ m{\.bz2$} ? "bzip2 -qdc"
: "cat";
if( $fullName eq "none" ) {
return();
}
else {
return("$catProg","$fullName");
}
}
sub slurpSeq {
my $fnaFl = $_[0];
my($cat,$truefile) = figureCompression("$fnaFl");
my %seq = ();
my $id = '';
open( my $FNA,"-|","$cat $truefile" );
while(<$FNA>) {
if( m{^>\s*(\S+)} ) {
$id = $1;
}
else {
chomp;
$seq{"$id"} .= uc($_);
}
}
close($FNA);
my $cSeqs = keys %seq;
if( $cSeqs > 0 ) {
return(\%seq);
}
else {
return();
}
}
sub getGC {
my $seq = $_[0];
my $countGC = $seq =~ tr{GC}{GC}; ### GC content
my $countAT = $seq =~ tr{AT}{AT}; ### AT content
my $fGC = sprintf("$roundup",($countGC/($countGC + $countAT)));
my $clean = length($seq) - ($countGC + $countAT);
return($countGC,$countAT,$fGC,$clean);
}
sub countOligos {
my $seq = $_[0];
my %count_oligo = ();
my @bases = split(//,$seq);
##### procedure to work with circular DNA pieces
if( $circular eq "T" ) {
my $add2circle = $signatureLn - 2;
my @add2circle = @bases[0 .. $add2circle];
push(@bases,@add2circle);
print "\tappended: ",join(", ",@add2circle),"\n";
}
my $addRange = $signatureLn - 1;
BASE:
for my $n ( 0 .. $#bases ) {
my $complement = $n + $addRange;
last if( $complement > $#bases );
my $nucl = join "",@bases[$n .. $complement];
next BASE unless( $nucl =~ m{^[ATGC]+$} );
#print $nucl,"<-nucleotide\n";exit if($n > 5);
$count_oligo{"$nucl"}++;
}
####### double strand:
my %ds_count = ();
for my $nucl ( @nucl ) {
my $count_o
= $count_oligo{"$nucl"} > 0 ? $count_oligo{"$nucl"} : 0;
my $rev_comp = $rev_comp{"$nucl"};
my $count = $rev_comp eq $nucl ? $count_o * 2
: $count_o + $count_oligo{"$rev_comp"};
$ds_count{"$nucl"} = $count;
}
my $counts = keys %count_oligo;
if( $counts > 0 ) {
return(\%count_oligo,\%ds_count);
}
else {
return();
}
}