line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CracTools::SAMReader; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$CracTools::SAMReader::DIST = 'CracTools'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: An easy to use tool to read files in SAM format. |
6
|
|
|
|
|
|
|
$CracTools::SAMReader::VERSION = '1.25'; |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
13358
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
46
|
|
9
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
37
|
|
10
|
2
|
|
|
2
|
|
5
|
use Carp; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
102
|
|
11
|
2
|
|
|
2
|
|
797
|
use CracTools::SAMReader::SAMline; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1608
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
2
|
|
|
2
|
1
|
1960
|
my $class = shift; |
16
|
2
|
|
|
|
|
4
|
my ($sam_file,$sam_type) = @_; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
9
|
my $self = bless{ |
19
|
|
|
|
|
|
|
sam_file => $sam_file, |
20
|
|
|
|
|
|
|
sam_type => $sam_type, |
21
|
|
|
|
|
|
|
}, $class; |
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
|
|
9
|
$self->init(); |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
|
|
9
|
return $self; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub iterator { |
30
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
31
|
1
|
|
|
|
|
3
|
my $f_it = $self->iteratorFile("IGNORE_HEADERS"); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
return sub { |
34
|
7
|
|
|
7
|
|
33
|
my ($line) = $f_it->(); |
35
|
7
|
|
|
|
|
6
|
my $sam_line; |
36
|
7
|
100
|
|
|
|
12
|
if(defined $line) { |
37
|
6
|
|
|
|
|
22
|
$sam_line = CracTools::SAMReader::SAMline->new($line); |
38
|
|
|
|
|
|
|
} |
39
|
7
|
|
|
|
|
9
|
return $sam_line; |
40
|
1
|
|
|
|
|
5
|
}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub iteratorFile { |
45
|
3
|
|
|
3
|
1
|
20
|
my $self = shift; |
46
|
3
|
|
|
|
|
4
|
my $option = shift; |
47
|
3
|
|
|
|
|
8
|
my $sam_file = $self->{sam_file}; |
48
|
|
|
|
|
|
|
|
49
|
3
|
50
|
|
|
|
8
|
if($sam_file =~ /\.sam$/) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
50
|
3
|
50
|
|
|
|
36
|
open(SAM,"< $sam_file") or die ("Cannot open $sam_file"); |
51
|
|
|
|
|
|
|
} elsif($self->{sam_file} =~ /\.sam.gz$/) { |
52
|
0
|
0
|
|
|
|
0
|
open(SAM,"gunzip -c $sam_file |") or die ("Cannot open $sam_file"); |
53
|
|
|
|
|
|
|
} elsif($self->{sam_file} =~ /\.bam$/) { |
54
|
0
|
0
|
|
|
|
0
|
open(SAM, "-|", "samtools view -h $sam_file" )or die "Cannot open $sam_file, check if samtools are installed."; |
55
|
|
|
|
|
|
|
} else { |
56
|
0
|
0
|
|
|
|
0
|
open(SAM,"< $sam_file") or die ("Cannot open $sam_file"); |
57
|
0
|
|
|
|
|
0
|
warn "Unknown file format. We assume this is SAM (uncompressed)."; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
3
|
|
|
|
|
78
|
my $next_line; |
61
|
3
|
|
|
|
|
4
|
my $line_number = 0; |
62
|
|
|
|
|
|
|
|
63
|
3
|
100
|
66
|
|
|
13
|
if(defined $option && $option eq "IGNORE_HEADERS") { |
64
|
1
|
|
|
|
|
12
|
while(my $line = ) { |
65
|
4
|
100
|
|
|
|
13
|
if(!($line =~ /^@/)) { |
66
|
1
|
|
|
|
|
4
|
$next_line = $line; |
67
|
1
|
|
|
|
|
2
|
$line_number++; |
68
|
1
|
|
|
|
|
1
|
last; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} else { |
72
|
2
|
|
|
|
|
27
|
$next_line = ; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
return sub { |
76
|
39
|
|
|
39
|
|
32
|
my $sam_line = $next_line; |
77
|
39
|
|
|
|
|
50
|
$next_line = ; |
78
|
39
|
|
|
|
|
27
|
$line_number++; |
79
|
39
|
100
|
|
|
|
35
|
if($sam_line) { |
80
|
37
|
|
|
|
|
64
|
return $sam_line, $line_number; |
81
|
|
|
|
|
|
|
} else { |
82
|
2
|
0
|
|
|
|
16
|
close(SAM) or warn $! ? "Error closing samtools pipe: $!" : "Exit status $? from samtools"; |
|
|
50
|
|
|
|
|
|
83
|
2
|
|
|
|
|
5
|
return (); |
84
|
|
|
|
|
|
|
} |
85
|
3
|
|
|
|
|
16
|
}; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub header { |
91
|
8
|
|
|
8
|
1
|
6
|
my $self = shift; |
92
|
8
|
|
|
|
|
30
|
return $self->{header}; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub refSeqLength { |
97
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
98
|
1
|
|
|
|
|
1
|
my $ref_seq = shift; |
99
|
1
|
50
|
|
|
|
3
|
croak("Missing reference sequence name in arguement") unless defined $ref_seq; |
100
|
1
|
|
|
|
|
3
|
my $refseq_lengths = $self->allRefSeqLengths(); |
101
|
1
|
|
|
|
|
5
|
return $refseq_lengths->{$ref_seq}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub allRefSeqLengths { |
106
|
2
|
|
|
2
|
1
|
2
|
my $self = shift; |
107
|
2
|
|
|
|
|
9
|
my @header_lines = split('\n',$self->header); |
108
|
2
|
|
|
|
|
3
|
my %refseq_lengths; |
109
|
2
|
|
|
|
|
5
|
foreach (@header_lines) { |
110
|
30
|
100
|
|
|
|
63
|
if ($_ =~/\@SQ.*SN:/) { |
111
|
25
|
|
|
|
|
66
|
my ($name,$length) = $_ =~/\@SQ.*SN:(\S+)\s+LN:(\d+)+/; |
112
|
25
|
|
|
|
|
41
|
$refseq_lengths{$name} = $length; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
2
|
|
|
|
|
7
|
return \%refseq_lengths; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub commandLine { |
120
|
2
|
|
|
2
|
1
|
2
|
my $self = shift; |
121
|
2
|
50
|
|
|
|
5
|
if(defined $self->header) { |
122
|
2
|
|
|
|
|
5
|
my @header_lines = split('\n',$self->header); |
123
|
2
|
|
|
|
|
3
|
my $command_line; |
124
|
2
|
|
|
|
|
4
|
foreach (@header_lines) { |
125
|
6
|
100
|
|
|
|
22
|
if ($_ =~/\@PG.*PN:crac/) { |
126
|
2
|
|
|
|
|
13
|
($command_line) = $_ =~ /CL:([^\t]+)/; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
2
|
|
|
|
|
5
|
return $command_line; |
130
|
|
|
|
|
|
|
} else { |
131
|
0
|
|
|
|
|
0
|
return undef; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub getCracArgumentValue { |
137
|
2
|
|
|
2
|
1
|
184
|
my $self = shift; |
138
|
2
|
|
|
|
|
3
|
my $argument = shift; |
139
|
2
|
|
|
|
|
4
|
my $command_line = $self->commandLine; |
140
|
2
|
50
|
|
|
|
3
|
if(defined $command_line) { |
141
|
2
|
|
|
|
|
31
|
my ($value) = $command_line =~ /--$argument\s+(\S+)/; |
142
|
2
|
|
|
|
|
7
|
return $value; |
143
|
|
|
|
|
|
|
} else { |
144
|
0
|
|
|
|
|
0
|
return undef; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub hasCracOption { |
150
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
151
|
0
|
|
|
|
|
0
|
my $option = shift; |
152
|
0
|
0
|
|
|
|
0
|
croak("Missing argument") unless defined $option; |
153
|
0
|
0
|
|
|
|
0
|
if(defined $self->commandLine) { |
154
|
0
|
|
|
|
|
0
|
return $self->commandLine =~ /--$option/; |
155
|
|
|
|
|
|
|
} else { |
156
|
0
|
|
|
|
|
0
|
return 0; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub getCracVersionNumber { |
162
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
163
|
1
|
50
|
|
|
|
2
|
if(defined $self->header) { |
164
|
1
|
|
|
|
|
2
|
my @header_lines = split('\n',$self->header); |
165
|
1
|
|
|
|
|
2
|
my $version_number; |
166
|
1
|
|
|
|
|
3
|
foreach (@header_lines) { |
167
|
3
|
100
|
|
|
|
12
|
if ($_ =~/\@PG.*PN:crac/) { |
168
|
1
|
|
|
|
|
4
|
($version_number) = $_ =~ /VN:([^\t]+)/; |
169
|
1
|
|
|
|
|
1
|
last; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
} |
172
|
1
|
|
|
|
|
3
|
return $version_number; |
173
|
|
|
|
|
|
|
} else { |
174
|
0
|
|
|
|
|
0
|
return undef; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub init { |
180
|
2
|
|
|
2
|
1
|
4
|
my $self = shift; |
181
|
2
|
|
|
|
|
8
|
my $f_it = $self->iteratorFile; |
182
|
2
|
|
|
|
|
2
|
my $header; |
183
|
2
|
|
|
|
|
5
|
while(my ($line) = $f_it->()) { |
184
|
31
|
100
|
|
|
|
85
|
if($line =~ /^@/) { |
185
|
30
|
|
|
|
|
46
|
$header .= $line; |
186
|
|
|
|
|
|
|
} else { |
187
|
1
|
|
|
|
|
2
|
last; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
} |
190
|
2
|
|
|
|
|
13
|
$self->{header} = $header; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
1; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
__END__ |