File Coverage

blib/lib/Bio/Lite.pm
Criterion Covered Total %
statement 78 94 82.9
branch 18 38 47.3
condition 5 12 41.6
subroutine 13 15 86.6
pod 7 7 100.0
total 121 166 72.8


line stmt bran cond sub pod time code
1             package Bio::Lite;
2             {
3             $Bio::Lite::DIST = 'Bio-Lite';
4             }
5             # ABSTRACT: Lightweight and fast module with a simplified API to ease scripting in bioinformatics
6             $Bio::Lite::VERSION = '0.003';
7 1     1   24622 use Carp;
  1         3  
  1         130  
8              
9 1     1   6 use Exporter;
  1         2  
  1         1474  
10             our @ISA = qw(Exporter);
11             our @EXPORT = qw(seqFileIterator reverseComplemente convertStrand pairedEndSeqFileIterator gffFileIterator getReadingFileHandle getWritingFileHandle);
12             our @EXPORT_OK = qw();
13              
14              
15             sub reverseComplemente {
16 1     1 1 9 my $seq = reverse shift;
17 1         3 $seq =~ tr/ACGTacgt/TGCAtgca/;
18 1         8 return $seq;
19             }
20              
21              
22             my %conversion_hash = ( '+' => 1, '-' => '-1', '1' => '+', '-1' => '-');
23             sub convertStrand($) {
24 4     4 1 6 my $strand = shift;
25 4         19 return $conversion_hash{$strand};
26             }
27              
28              
29             sub seqFileIterator {
30 5     5 1 7582 my ($file,$format) = @_;
31              
32 5 50       17 croak "Missing file in argument of seqFileIterator" if !defined $file;
33              
34             # Get file handle for $file
35 5         14 my $fh = getReadingFileHandle($file);
36              
37             # Automatic file extension detection
38 5 50       12 if(!defined $format) {
39 5 100       14 if($file =~ /\.(fasta|fa)(\.|$)/) {
    50          
40 2         22 $format = 'fasta';
41             } elsif($file =~ /\.(fastq|fq)(\.|$)/) {
42 3         69 $format = 'fastq';
43             } else {
44 0         0 croak "Undefined file extension";
45             }
46             } else {
47 0         0 $format = lc $format;
48             }
49              
50             # FASTA ITERATOR
51 5 100       18 if ($format eq 'fasta') {
    50          
52             # Read prev line for FASTA because we dont know the number
53             # of line used for the sequence
54 2         23 my $prev_line = <$fh>;
55 2         4 chomp $prev_line;
56             return sub {
57 7     7   4260 my ($name,$seq,$qual);
58 7 100       16 if(defined $prev_line) {
59 5         22 ($name) = $prev_line =~ />(.*)$/;
60 5         12 $prev_line = <$fh>;
61             # Until we find a new sequence identifier ">", we
62             # concatenate the lines corresponding to the sequence
63 5   100     32 while(defined $prev_line && $prev_line !~ /^>/) {
64 10         12 chomp $prev_line;
65 10         15 $seq .= $prev_line;
66 10         55 $prev_line = <$fh>;
67             }
68 5         25 return {name => $name, seq => $seq, qual => $qual};
69             } else {
70 2         3 return undef;
71             }
72 2         14 };
73             # FASTQ ITERATOR
74             } elsif ($format eq 'fastq') {
75             return sub {
76 7     7   3584 my ($name,$seq,$qual);
77 7         162 ($name) = <$fh> =~ /@(.*)$/;
78 7 100       15 if(defined $name) {
79 6         21 $seq = <$fh>;
80 6         9 chomp $seq;
81 6         8 <$fh>; # skip second seq name (useless line)
82 6         11 $qual = <$fh>;
83 6         8 chomp $qual;
84 6         27 return {name => $name, seq => $seq, qual => $qual};
85             } else {
86 1         4 return undef;
87             }
88 3         23 };
89             } else {
90 0         0 croak "Undefined file format";
91             }
92             }
93              
94              
95             sub pairedEndSeqFileIterator {
96 1     1 1 2521 my($file1,$file2,$format) = @_;
97              
98 1         4 my $it1 = seqFileIterator($file1,$format);
99 1         4 my $it2 = seqFileIterator($file2,$format);
100              
101             return sub {
102 2     2   2053 my $entry1 = $it1->();
103 2         6 my $entry2 = $it2->();
104 2 50 33     14 if(defined $entry1 && defined $entry2) {
105 2         8 return { read1 => $entry1, read2 => $entry2 };
106             } else {
107 0         0 return undef;
108             }
109 1         6 };
110             }
111              
112              
113             sub gffFileIterator {
114 1     1 1 3807 my ($file,$format) = @_;
115              
116 1 50 33     10 croak "Missing arguments in gffFileIterator" if !defined $file || !defined $format;
117              
118 1         4 $format = lc $format;
119 1         3 my $fh = getReadingFileHandle($file);
120              
121 1         3 my $attribute_split;
122              
123 1 50 0     4 if ($format eq 'gff3') {
    0          
124 1     4   6 $attribute_split = sub {my $attr = shift; return $attr =~ /(\S+)=(.*)/;};
  4         6  
  4         22  
125             } elsif ($format eq 'gtf' || $format eq 'gff2') {
126 0     0   0 $attribute_split = sub {my $attr = shift; return $attr =~ /(\S+)\s+"(.*)"/;};
  0         0  
  0         0  
127             } else {
128 0         0 croak "Undefined gff format";
129             }
130              
131 1         38 my $line = <$fh>;
132 1         8 while($line =~ /^#/) {
133 3         13 $line = <$fh>;
134             }
135              
136             return sub {
137 1 50   1   5 if (defined $line) {
138 1         10 my($chr,$source,$feature,$start,$end,$score,$strand,$frame,$attributes) = split("\t",$line);
139 1         6 my @attributes_tab = split(";",$attributes);
140 1         1 my %attributes_hash;
141 1         4 foreach my $attr (@attributes_tab) {
142 4         9 my ($k,$v) = $attribute_split->($attr);
143 4         12 $attributes_hash{$k} = $v;
144             }
145 1         3 $line = <$fh>; # Get next line
146 1         15 return { chr => $chr,
147             source => $source,
148             feature => $feature,
149             start => $start,
150             end => $end,
151             score => $score,
152             strand => $strand,
153             frame => $frame,
154             attributes => \%attributes_hash,
155             };
156             } else {
157 0         0 return undef;
158             }
159 1         10 };
160             }
161              
162              
163             sub getReadingFileHandle {
164 6     6 1 11 my $file = shift;
165 6         8 my $fh;
166 6 50       24 if($file =~ /\.gz$/) {
167 0 0       0 open($fh,"gunzip -c $file |") or die ("Cannot open $file");
168             } else {
169 6 50       78 open($fh,"< $file") or die ("Cannot open $file");
170             }
171 6         274 return $fh;
172             }
173              
174              
175             sub getWritingFileHandle {
176 0     0 1   my $file = shift;
177 0           my $fh;
178 0 0         if($file =~ /\.gz$/) {
179 0 0         open($fh,"| gzip > $file") or die ("Cannot open $file");
180             } else {
181 0 0         open($fh,"> $file") or die ("Cannot open $file");
182             }
183 0           return $fh;
184             }
185              
186              
187             1;
188              
189             __END__