-
Notifications
You must be signed in to change notification settings - Fork 0
/
findBestSequence.pm
235 lines (169 loc) · 4.63 KB
/
findBestSequence.pm
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
package findBestSequence;
use strict;
use warnings;
use Exporter;
our @ISA= qw(Exporter);
our @EXPORT= qw(&poolData &mapToCountsBatch &uniq &readBED &extractCoordinates &getFai &round &extractSequences &fastaTofastqBatch &mapDataBatch);
use config;
use vars qw(%config );
*config=\%config::config;
sub extractCoordinates{
my ($seq, $min, $max, $start, $end, $prefix, $log) = @_;
for(my $i=$min; $i<=$max; $i++){
open(OUT, ">"."$prefix.$i.bed") or die "Cannot create file $prefix.$i.bed: $!";
for(my $j=$start; $j+$i <= $end; $j++){
print OUT $seq."\t".$j."\t";
print OUT $j+$i."\n";
}
close OUT;
}
return 1;
}
sub extractSequence{
my ($bed, $fasta, $out, $log) = @_;
## Build up the command line
my $cmdline = "$config{'bedtools'} getfasta -fi $fasta -bed $bed -fo $out";
my $title = "Extracting fasta file.";
&runCmdLine($cmdline, $log, $title);
return 1;
}
sub extractSequences{
my ($fasta, $prefix, $min, $max, $log) = @_;
for(my $i=$min; $i<=$max; $i++){
extractSequence("$prefix.$i.bed", $fasta, "$prefix.$i.fasta", $log);
}
return 1;
}
sub fastaTofastq{
my ($in, $out, $log)= @_;
open FILE, "<".$in or die "Cannot open file $in: $!";
open OUT, ">".$out or die "Cannot create file $out: $!";
my $header="";
my ($sequence, $sequence_length, $sequence_quality);
while(<FILE>) {
chomp;
if (/^>(.+)/) {
if($header ne "") {
print OUT "\@".$header."\n";
print OUT $sequence."\n";
print OUT "+"."\n";
print OUT $sequence_quality."\n";
}
$header = $1;
$sequence = "";
$sequence_length = "";
$sequence_quality = "";
}
else {
$sequence .= $_;
$sequence_length = length($_);
for(my $i=0; $i<$sequence_length; $i++) {$sequence_quality .= "I"}
}
}
close FILE;
print OUT "\@".$header."\n";
print OUT $sequence."\n";
print OUT "+"."\n";
print OUT $sequence_quality."\n";
close OUT;
return 1;
}
sub fastaTofastqBatch{
my ($prefix, $min, $max, $log) = @_;
for(my $i=$min; $i<=$max; $i++){
fastaTofastq("$prefix.$i.fasta", "$prefix.$i.fastq", $log);
}
return 1;
}
sub getFai{
my ($fasta, $log) = @_;
## Build up the command line
my $cmdline = "$config{'samtools'} faidx $fasta";
my $title = "Generating *.fai file.";
&runCmdLine($cmdline, $log, $title);
return 1;
}
sub mapData{
my ($fastq, $genome, $sam, $log) = @_;
## Build up the command line
my $cmdline = "$config{'bowtie'} -a -v 0 -p 4 $genome $fastq -S $sam";
my $title = "Generating *.fai file.";
&runCmdLine($cmdline, $log, $title);
return 1;
}
sub mapDataBatch{
my ($prefix, $genome, $min, $max, $log) = @_;
for(my $i=$min; $i<=$max; $i++){
mapData("$prefix.$i.fastq", $genome, "$prefix.$i.sam", $log);
}
return 1;
}
sub mapToCounts{
my ($sam, $out, $log) = @_;
my %count;
open(SAM, "<".$sam) or die "Cannot open the file $sam: $!";
while(<SAM>){
next if(/^@/);
chomp;
my @tab = split "\t";
$count{$tab[0]}{'count'}++;
$count{$tab[0]}{'sequence'} = $tab[9];
$count{$tab[0]}{'length'} = length($tab[9]);
}
close SAM;
open(OUT, ">".$out) or die "Cannot create output file $out: $!";
print OUT "Sequence name\tSequence\tCount\n";
foreach my $key (keys %count){
print OUT $key."\t".$count{$key}{'sequence'}."\t".$count{$key}{'count'}."\n";
}
close OUT;
return 1;
}
sub mapToCountsBatch{
my ($prefix, $min, $max, $log) = @_;
for(my $i=$min; $i<=$max; $i++){
mapToCounts("$prefix.$i.sam", "$prefix.$i.count", $log);
}
return 1;
}
sub poolData{
my ($prefix, $tmp, $path, $log) = @_;
## Build up the command line
my $cmdline = "Rscript $path/findBestSequence.R \"$tmp\" \"$prefix\"";
my $title = "Compiling data with R.";
&runCmdLine($cmdline, $log, $title);
return 1;
}
sub readBED{
my ($bed, $log) = @_;
open(BED, "<".$bed) or die "Cannot open BED file $bed: $!";
my @bed;
while(<BED>){
chomp;
push @{$bed[$#bed+1]}, split "\t";
}
return @bed;
}
sub round{
$_[0] > 0 ? int($_[0] + .5) : -int(-$_[0] + .5)
}
sub runCmdLine {
my ($cmdline, $log, $title) = @_;
## Run the command line and output into the log file
print $log "\n##############\n" ;
print $log "## Start analysis: ".`date`."\n";
print $log "## $title. \n\n";
print $log "$cmdline\n";
print $log "#<--- Output: --------------------------------------------------\n";
my $result = `$cmdline 2>&1`;
print $log $result."\n";
print $log "\n";
print $log "#--------------------------------------------------------------->\n";
print $log "## End of analysis: ".`date`."\n";
return 1;
}
sub uniq {
my %seen;
grep !$seen{$_}++, @_
}
1;