lib/Bio/ToolBox/Parser/ucsc.pm

filter: Statementstmt 71.7% 112 / 156 Branchbran 54.5% 48 / 88 Conditioncond 48.0% 12 / 25 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 63.8% 194 / 304 CC 55 SCAR 35.2
CC
55
Cov
64%
load_extra_data
60.3
open_file
54.1
typelist
10.1
CRAP 33.7
« lib/Bio/ToolBox/utility.pm ↑ Summary lib/Bio/ToolBox/SeqFeature.pm »
1 package Bio::ToolBox::Parser::ucsc;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw(carp cluck croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use base 'Bio::ToolBox::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Bio::ToolBox::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Bio::ToolBox::Parser::ucsc::builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '2.02';
11  
12 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
13 3 my $class = shift;
14 3 return $class->SUPER::new(@_);
15 }
16  
17 7 ▶sub open_file {
Subroutine open_file: called CC=20 SCAR=39.9
Pod documented
18 7 my $self = shift;
19 7 ▶ my $filename = shift || $self->file || undef;
Condition60%3/5
shift() || $self->file || undef
l!l
70
shift() or $self->file
l!l&&r!l&&!r
250
MC/DC0%0/2
shift() || $self->file || undef
shift() $self->file
Truth table66%2/3
A or B
A= shift()B= $self->file
ABresult
1X1
011
000
20  
21 # check file
22 # Unlike the gff and bed parsers, the ucsc parser can handle opening
23 # multiple files, primarily to recycle various UCSC support files.
24 # This is historical precedent from earlier versions, unfortunately.
25 # This may unnecessarily complicate and/or inflate memory....
26 7 ▶ unless ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 7 7
27 0 carp 'ERROR: no file name passed!';
28 0 return;
29 }
30  
31 # Check file format type
32 7 ▶ my $filetype = $self->filetype || undef;
Condition50%1/2
$self->filetype || undef
l!l
70
MC/DC0%0/1
$self->filetype || undef
$self->filetype
Truth table50%1/2
A
A= $self->filetype
Aresult
11
00
33 7 ▶ unless ($filetype) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filetype) 0 7 7
34 0 ( my $flavor, $filetype ) = Bio::ToolBox::Data->taste_file($filename);
35 0 ▶ unless ( $flavor eq 'ucsc' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flavor eq "ucsc") 0 0 0
36 0 confess 'FATAL: File is not a UCSC-format file!!! How did we get here?';
37 }
38 0 $self->{filetype} = $filetype;
39 }
40 7 ▶ if ( $filetype eq 'genePred' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filetype eq "genePred") 0 7 7
41  
42 # turn off gene processing for simple genePred which has no gene names
43 0 $self->do_gene(0);
44 }
45  
46 # The ucsc parser does not have convertor subroutines, unlike the gff and bed
47 # parsers. Rather, parsing is done by the number of elements in each line
48 # and parsed accordingly in the builder object. Slightly inefficient but
49 # functional.
50  
51 # Open filehandle object
52 7 ▶ my $fh = Bio::ToolBox::Data->open_to_read_fh($filename)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "Bio::ToolBox::Data"->open_to_read_fh($filename) or croak("FATAL: cannot open file '${filename}'!") 7 0 7
53 or croak "FATAL: cannot open file '$filename'!";
54  
55 # reset source as necessary
56 7 ▶ if ( $filename =~ /ensgene/i and $self->source eq 'UCSC' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($filename =~ /ensgene/i and $self->source eq 'UCSC') { } 5 2 7
elsif ($filename =~ /xenorefgene/i and $self->source eq 'UCSC') { } 0 2 2
elsif ($filename =~ /refgene/i and $self->source eq 'UCSC') { } 0 2 2
elsif ($filename =~ /refseq/i and $self->source eq 'UCSC') { } 0 2 2
elsif ($filename =~ /knowngene/i and $self->source eq 'UCSC') { } 0 4 4
Condition66%2/3
$filename =~ /ensgene/i and $self->source eq "UCSC"
!ll&&!rl&&r
025
MC/DC50%1/2
$filename =~ /ensgene/i and $self->source eq "UCSC"
$filename =~ /ensgene/i $self->source eq "UCSC"
Truth table66%2/3
A and B
A= $filename =~ /ensgene/iB= $self->source eq "UCSC"
ABresult
0X0
100
111
57 5 $self->source('EnsGene');
58 }
59 ▶ elsif ( $filename =~ /xenorefgene/i and $self->source eq 'UCSC' ) {
Condition33%1/3
$filename =~ /xenorefgene/i and $self->source eq "UCSC"
!ll&&!rl&&r
200
MC/DC0%0/2
$filename =~ /xenorefgene/i and $self->source eq "UCSC"
$filename =~ /xenorefgene/i $self->source eq "UCSC"
Truth table33%1/3
A and B
A= $filename =~ /xenorefgene/iB= $self->source eq "UCSC"
ABresult
0X0
100
111
60 0 $self->source('xenoRefGene');
61 }
62 ▶ elsif ( $filename =~ /refgene/i and $self->source eq 'UCSC' ) {
Condition33%1/3
$filename =~ /refgene/i and $self->source eq "UCSC"
!ll&&!rl&&r
200
MC/DC0%0/2
$filename =~ /refgene/i and $self->source eq "UCSC"
$filename =~ /refgene/i $self->source eq "UCSC"
Truth table33%1/3
A and B
A= $filename =~ /refgene/iB= $self->source eq "UCSC"
ABresult
0X0
100
111
63 0 $self->source('refGene');
64 }
65 ▶ elsif ( $filename =~ /refseq/i and $self->source eq 'UCSC' ) {
Condition33%1/3
$filename =~ /refseq/i and $self->source eq "UCSC"
!ll&&!rl&&r
200
MC/DC0%0/2
$filename =~ /refseq/i and $self->source eq "UCSC"
$filename =~ /refseq/i $self->source eq "UCSC"
Truth table33%1/3
A and B
A= $filename =~ /refseq/iB= $self->source eq "UCSC"
ABresult
0X0
100
111
66 0 $self->source('refSeq');
67 }
68 ▶ elsif ( $filename =~ /knowngene/i and $self->source eq 'UCSC' ) {
Condition33%1/3
$filename =~ /knowngene/i and $self->source eq "UCSC"
!ll&&!rl&&r
200
MC/DC0%0/2
$filename =~ /knowngene/i and $self->source eq "UCSC"
$filename =~ /knowngene/i $self->source eq "UCSC"
Truth table33%1/3
A and B
A= $filename =~ /knowngene/iB= $self->source eq "UCSC"
ABresult
0X0
100
111
69 0 $self->source('knownGene');
70 }
71  
72 # Check existing data
73 # the reason this parser can handle reading a second file without having to make a
74 # new parser object (like gff and bed) is so that we can potentially recycle the
75 # extra UCSC information
76 7 ▶ if ( $self->fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fh) 2 5 7
77  
78 # close existing
79 2 $self->{fh}->close;
80  
81 # go ahead and clear out existing data
82 2 $self->{gene2seqf} = {};
83 2 $self->{top_features} = [];
84 2 $self->{loaded} = {};
85 2 $self->{id2count} = {};
86 2 $self->{counts} = {};
87 2 $self->{'eof'} = 0;
88 2 $self->{line_count} = 0;
89 }
90  
91 # Finish
92 7 $self->{fh} = $fh;
93 7 return 1;
94 }
95  
96 5 ▶sub load_extra_data {
Subroutine load_extra_data: called CC=16 SCAR=41.0
Pod documented
97 5 my ( $self, $file, $type ) = @_;
98 5 ▶ unless ($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 5 5
99 0 carp 'ERROR: no file name passed!';
100 0 return;
101 }
102  
103 # check the type
104 5 ▶ if ( $type =~ m/ensembltogene | ensname/xi ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($type =~ /ensembltogene | ensname/xi) { } 2 3 5
elsif ($type =~ /ensemblsource | enssrc/xi) { } 3 0 3
elsif ($type =~ /refseqstat | status/xi) { } 0 0 0
elsif ($type =~ /refseqsum | summary/xi) { } 0 0 0
elsif ($type =~ /kgxref/i) { } 0 0 0
105 2 $type = 'ensembltogene';
106 }
107 elsif ( $type =~ m/ensemblsource | enssrc/xi ) {
108 3 $type = 'ensemblsource';
109 }
110 elsif ( $type =~ m/refseqstat | status/xi ) {
111 0 $type = 'refseqstat';
112 }
113 elsif ( $type =~ m/refseqsum | summary/xi ) {
114 0 $type = 'refseqsum';
115 }
116 elsif ( $type =~ /kgxref/i ) {
117 0 $type = 'kgxref';
118 }
119 else {
120 0 carp "ERROR: unknown type '$type' to load extra data";
121 0 return;
122 }
123  
124 5 my $fh = Bio::ToolBox::Data->open_to_read_fh($file);
125 5 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 5 5
126 0 carp "ERROR: unable to open file '$file'!";
127 0 return;
128 }
129  
130 # load ensembl data
131 5 my $count = 0;
132 5 ▶ if ( $type =~ /ensembl/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /ensembl/) { } 5 0 5
133  
134 # we will store gene name in position 0, and source in position 1
135 5 ▶ my $index = $type eq 'ensembltogene' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'ensembltogene' ? : 2 3 5
136 5 ▶ while ( my $line = $fh->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $fh->getline) 85 5 90
137  
138 # process line
139 85 chomp $line;
140 85 ▶ next if ( $line =~ /^#/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 85 85
141 85 my @line_data = split /\t/, $line;
142 85 ▶ if ( scalar @line_data != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @line_data != 2) 0 85 85
143 0 carp sprintf
144 "ERROR: file $file doesn't seem right!? Line has %d elements!",
145 scalar @line_data;
146 0 return;
147 }
148  
149 # store data into hash
150 85 $self->{'ensembldata'}{ $line_data[0] }->[$index] = $line_data[1];
151 85 $count++;
152 }
153 }
154  
155 # load various refSeq data
156 else {
157 # we just store the line data based on the gene ID
158 # each table has different elements
159 # here they are for reference
160  
161 ### refSeqStatus table
162 # 0 mrnaAcc RefSeq gene accession name
163 # 1 status Status ('Unknown', 'Reviewed', 'Validated', 'Provisional', 'Predicted', 'Inferred')
164 # 2 molecule type ('DNA', 'RNA', 'ds-RNA', 'ds-mRNA', 'ds-rRNA', 'mRNA', 'ms-DNA', 'ms-RNA', 'rRNA', 'scRNA', 'snRNA', 'snoRNA', 'ss-DNA', 'ss-RNA', 'ss-snoRNA', 'tRNA', 'cRNA', 'ss-cRNA', 'ds-cRNA', 'ms-rRNA') values molecule type
165  
166 ### refSeqSummary table
167 # 0 RefSeq mRNA accession
168 # 1 completeness FullLength ('Unknown', 'Complete5End', 'Complete3End', 'FullLength', 'IncompleteBothEnds', 'Incomplete5End', 'Incomplete3End', 'Partial')
169 # 1 summary text values Summary comments
170  
171 ### kgXref table
172 # 0 kgID Known Gene ID
173 # 1 mRNA mRNA ID
174 # 2 spID SWISS-PROT protein Accession number
175 # 3 spDisplayID SWISS-PROT display ID
176 # 4 geneSymbol Gene Symbol
177 # 5 refseq RefSeq ID
178 # 6 protAcc NCBI protein Accession number
179 # 7 description Description
180  
181 ### ensemblToGeneName table
182 # 0 Ensembl transcript ID
183 # 1 gene name
184  
185 # load the table
186 0 ▶ while ( my $line = $fh->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $fh->getline) 0 0 0
187 0 chomp $line;
188 0 ▶ next if ( $line =~ /^#/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
189 0 my @line_data = split /\t/, $line;
190  
191 # the unique id should be the first element in the array
192 # take it off the array, since it doesn't need to be stored there too
193 0 my $id = shift @line_data;
194  
195 # check for duplicate lines
196 0 ▶ if ( exists $self->{$type}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$type}{$id}) 0 0 0
197 0 printf STDERR " %s line for identifier %s exists twice!\n", $type, $id;
198 0 next;
199 }
200  
201 # store data into hash
202 0 $self->{$type}{$id} = [@line_data];
203 0 $count++;
204 }
205 }
206  
207 5 $fh->close;
208 5 return $count;
209 }
210  
211 3 ▶sub typelist {
Subroutine typelist: called CC=5 SCAR=23.1
Pod documented
212 3 my $self = shift;
213  
214 3 ▶ if ( keys %{ $self->{counts} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$self{'counts'};}) { } 0 3 3
215  
216 # actual list of counted types
217 0 my @items;
218 0 foreach my $k ( keys %{ $self->{counts} } ) {
219 0 ▶ push @items, $k if $self->{counts}{$k} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"counts"}{$k} > 0 0 0 0
220 }
221 0 return join( ',', @items );
222 }
223 else {
224 # generic list based on expectations
225 3 ▶ if ( $self->filetype eq 'genePred' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filetype eq 'genePred') { } 0 3 3
226 0 return 'mRNA,ncRNA,exon,CDS';
227 }
228 else {
229 3 return 'gene,mRNA,ncRNA,exon,CDS';
230 }
231 }
232 }
233  
234 92 ▶sub next_feature {
Subroutine next_feature: called CC=8 SCAR=21.7
Pod documented
235 92 my $self = shift;
236  
237 # check that we have an open filehandle
238 92 ▶ unless ( $self->fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->fh) 0 92 92
239 0 croak 'FATAL: no UCSC file loaded to parse!';
240 }
241 92 ▶ return if $self->{'eof'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 0 92 92
242  
243 92 ▶ while ( my $line = $self->fh->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->fh->getline) 114 5 119
244 114 ▶ if ( $line !~ /\w+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /\w+/) 0 114 114
245 0 $self->{line_count}++;
246 0 next;
247 }
248 114 ▶ if ( $line =~ /^#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#/) 27 87 114
249 27 push @{ $self->{comments} }, $line;
250 27 $self->{line_count}++;
251 27 next;
252 }
253 87 chomp $line;
254 87 my @linedata = split /\t/, $line;
255 87 my $builder = Bio::ToolBox::Parser::ucsc::builder->new( \@linedata, $self );
256 87 $self->{line_count}++;
257 87 ▶ unless ($builder) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($builder) 0 87 87
258  
259 # builder will print its own error message if fails
260 0 printf STDERR " unable to parse line number %d\n", $self->{line_count};
261 0 next;
262 }
263  
264 # generate and return the feature from the line
265 87 ▶ if ( $self->do_gene ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->do_gene) { } 70 17 87
266 70 return $builder->build_gene;
267 }
268 else {
269 17 return $builder->build_transcript;
270 }
271 }
272  
273 # presumed end of file
274 5 $self->{'eof'} = 1;
275 5 return;
276 }
277  
278 2 *parse_file = \&parse_table;
279 2 *parse_file if 0; # avoid once warning
280  
281 5 ▶sub parse_table {
Subroutine parse_table: called CC=9 SCAR=22.2
Pod documented
282 5 my $self = shift;
283 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 3 5
284 2 ▶ $self->open_file(shift) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->open_file(shift()) or return 2 0 2
285 }
286 5 ▶ unless ( $self->fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->fh) 0 5 5
287 0 confess 'FATAL: must open a file first!';
288 }
289 5 ▶ return if ( $self->{'eof'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 0 5 5
290  
291 #### Main Loop
292 5 printf " Parsing %s format file....\n", $self->filetype;
293 5 ▶ while ( my $feature = $self->next_feature ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $feature = $self->next_feature) 85 5 90
294  
295 # record this top feature
296 85 my $id = $feature->primary_id;
297  
298 # the builder should have checked and made this ID unique
299 85 $self->{loaded}{$id} = $feature;
300  
301 # check chromosome
302 85 my $s = $feature->seq_id;
303 85 ▶ unless ( exists $self->{seq_ids}{$s} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"seq_ids"}{$s}) 5 80 85
304 5 $self->{seq_ids}{$s} = $feature->end;
305 }
306 85 ▶ $self->{seq_ids}{$s} = $feature->end if $feature->end > $self->{seq_ids}{$s};
Branch: 1 branch
BranchTrueFalseTotal
if $feature->end > $self->{"seq_ids"}{$s} 25 60 85
307 }
308  
309 # after parsing the entire file, add all the loaded features to a sorted
310 # array of the top features
311 # we pseudo-genomic sort by Schwartzian transform because UCSC tables
312 # can't be trusted to be sorted
313 5 push @{ $self->{top_features} }, map { $_->[2] }
314 81 ▶ sort { $a->[0] cmp $b->[0] or $a->[1] <=> $b->[1] }
Condition66%2/3
$Bio::ToolBox::Parser::ucsc::a->[0] cmp $Bio::ToolBox::Parser::ucsc::b->[0] || $Bio::ToolBox::Parser::ucsc::a->[1] <=> $Bio::ToolBox::Parser::ucsc::b->[1]
l!l&&r!l&&!r
0801
MC/DC50%1/2
$Bio::ToolBox::Parser::ucsc::a->[0] cmp $Bio::ToolBox::Parser::ucsc::b->[0] || $Bio::ToolBox::Parser::ucsc::a->[1] <=> $Bio::ToolBox::Parser::ucsc::b->[1]
$Bio::ToolBox::Parser::ucsc::a->[0] cmp $Bio::ToolBox::Parser::ucsc::b->[0] $Bio::ToolBox::Parser::ucsc::a->[1] <=> $Bio::ToolBox::Parser::ucsc::b->[1]
Truth table66%2/3
A || B
A= $Bio::ToolBox::Parser::ucsc::a->[0] cmp $Bio::ToolBox::Parser::ucsc::b->[0]B= $Bio::ToolBox::Parser::ucsc::a->[1] <=> $Bio::ToolBox::Parser::ucsc::b->[1]
ABresult
1X1
011
000
315 5 map { [ $_->seq_id, $_->start, $_ ] } values %{ $self->{loaded} };
316  
317 # finished parsing
318 5 return 1;
319 }
320  
321 2 ▶sub counts {
Subroutine counts: called CC=2 SCAR=7.0
Pod documented
322 2 my $self = shift;
323 2 my %counts = %{ $self->{counts} };
324 2 ▶ return wantarray ? %counts : \%counts;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
325 }
326  
327 2 1;
328  
329 __END__
« lib/Bio/ToolBox/utility.pm ↑ Summary lib/Bio/ToolBox/SeqFeature.pm »