-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequenceIO.pm
262 lines (209 loc) · 5.94 KB
/
SequenceIO.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
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
package SequenceIO;
###################################################################
# SequenceIO
#
# Package for common IO routines
#
# Author: Giuseppe Narzisi
# Date: December 11, 2013
#
###################################################################
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(loadCoordinates loadExonsBed loadGenomeFasta parseHeader saveReadGroup);
use strict;
use warnings;
use FindBin qw($Bin);
use lib $Bin; # add $Bin directory to @INC
use Utils;
my $bamtools = "$Bin/bamtools-2.3.0/bin/bamtools";
## load selected location (coordinates)
#####################################################
sub loadCoordinates {
my $file = $_[0];
my $exons = $_[1];
my $VERBOSE = $_[2];
print STDERR "Loading coordinates...";
if ($file ne "null") {
open SELECTED, "< $file" or die "Can't open $file ($!)\n";
my $cntall_locs = 0;
while (<SELECTED>) {
chomp;
next if ($_ =~ /^#/); # skip comments
my ($chr, $pos) = split /\t/, $_, 2;
#if ($chr =~ /^chr/) { $chr = substr($chr,3); }
my $exon;
$exon->{chr} = $chr;
$exon->{start} = $pos;
$exon->{end} = $pos;
push @{$exons->{$chr}}, $exon;
$cntall_locs++;
}
close SELECTED;
#if($VERBOSE) {
print STDERR "$cntall_locs locations.\n";
#}
}
}
## load exons list
#####################################################
sub loadExonsBed {
print STDERR "Loading targets from BED file...";
my $file = $_[0];
my $exons = $_[1];
my $radius = $_[2];
my $VERBOSE = $_[3];
open EXONSLIST, "< $file" or die "Can't open $file ($!)\n";
my $cntall_exons = 0;
my $cntovl_exons = 0;
my $prev_exon;
$prev_exon->{chr} = 0;
$prev_exon->{start} = 0;
$prev_exon->{end} = 0;
while (<EXONSLIST>)
{
chomp;
next if ($_ =~ /^#/ || $_ =~ /^@/); # skip comments
#my ($chr, $start, $end) = split /\t/, $_, 3;
my @array = split /\t/, $_;
my $chr = $array[0];
my $start = $array[1];
my $end = $array[2];
#if ($chr =~ /^chr/) { $chr = substr($chr,3); }
# exons coordinate are sorted in the bed file
if ( ($prev_exon->{chr} eq $chr) && ($start <= $prev_exon->{end}) ) {
#print STDERR "exons regions overlap: [$prev_exon->{start},$prev_exon->{end}] [$start,$end]\n";
$prev_exon->{end} = $end;
$cntovl_exons++;
}
else {
my $exon;
$exon->{chr} = $chr;
$exon->{start} = $start;
$exon->{end} = $end;
## extend region left and right by radius
#my $l = $start-$radius;
#my $u = $end+$radius;
push @{$exons->{$exon->{chr}}}, $exon;
# update prev exon
$prev_exon = $exon;
$cntall_exons++;
}
#last if($cntall_exons > 10);
}
if($VERBOSE) {
# print exons
foreach my $k (keys %$exons) { # for each chromosome
foreach my $exon (@{$exons->{$k}}) { # for each exon
print STDERR "$exon->{chr}\t$exon->{start}\t$exon->{end}\n";
}
}
}
close EXONSLIST;
#if($VERBOSE) {
print STDERR "$cntall_exons targets (filtered $cntovl_exons overlapping).\n";
#}
}
# load the genome in fasta format
##########################################
sub loadGenomeFasta {
print STDERR "Loading genome from FASTA file...";
my $file = $_[0];
my $genome = $_[1];
open FASTAFILE, "< $file" or die "Can't open $file ($!)\n";
my $header = "";
my $SDNA = "";
my $chr = "";
my $cnt = 0;
while ( <FASTAFILE> ) {
chomp;
#print "$_\n";
if($_ =~ m/>/) {
$cnt++;
$header = $_;
#print "$header\n";
if($chr ne "") {
#print "$chr\n";
$genome->{$chr}->{seq} = $SDNA;
}
$header =~ s/^>//; # remove ">"
$header =~ s/\s+$//; # remove trailing whitespace
my ($label, $tmp) = split / /, $header, 2;
$chr = $label;
#if ($label =~ /^chr/) { $chr = substr($label,3); } # update chromosome label
$SDNA = "";# clear out old sequence
}
else {
s/\s+//g; # remove whitespace
$SDNA .= $_;
}
}
close FASTAFILE;
if($chr ne "") { # handle last sequence
#print "$chr\n";
$genome->{$chr}->{seq} = $SDNA;
}
#if($VERBOSE) {
print STDERR "$cnt sequences.\n";
#}
}
## process SAM header to extract read groups info
#####################################################
sub parseHeader {
my $PREFIX = $_[0];
my $readgroups = $_[1];
my $headerfile = "header.txt";
# erease readgroup information from previous family
for (keys %$readgroups) { delete $readgroups->{$_}; }
## extract header from SAM file
runCmd("extract SAM header", "$bamtools header -in $PREFIX/bamfile.bam > $PREFIX/$headerfile");
## read the list of read groups per sample from header
open HEADER, "< $PREFIX/$headerfile" or die "Can't open $PREFIX/$headerfile ($!)\n";
while (<HEADER>) {
chomp;
next if($_ eq ""); # skip empty string
next if($_ !~ /^\@/); # skip non-header line
my @records = split /\t/, $_;
if ($records[0] eq "\@RG") {
#@RG ID:READGROUP SM:SAMPLE
#print join(' ', @records), "\n";
my $sm;
my $id;
foreach my $tag (@records) {
my @data = split /:/, $tag;
#print join(':', @data), "\n";
if($data[0] eq "SM") { $sm = $data[1]; }
if($data[0] eq "ID") { $id = $data[1]; }
}
push @{$readgroups->{$sm}->{rg}}, $id;
}
}
close HEADER;
}
## save read groups to file
#####################################################
sub saveReadGroup {
my $sample = $_[0];
my $PREFIX = $_[1];
my $readgroups = $_[2];
my $rgfile = $_[3];
open SAMPLE, "> $PREFIX/$rgfile" or die "Can't open $PREFIX/$rgfile ($!)\n";
for my $sm ( keys %$readgroups ) {
if($sample eq "ALL") {
#print $sample.": ";
foreach my $rg ( @{$readgroups->{$sm}->{rg}}) {
# print $rg.", ";
print SAMPLE "$rg\n";
}
}
elsif($sm eq $sample) {
#print $sample.": ";
foreach my $rg ( @{$readgroups->{$sm}->{rg}}) {
# print $rg.", ";
print SAMPLE "$rg\n";
}
}
}
close SAMPLE;
}
1;