script/fqlen.pl untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 154 CC 3 SCAR 12.8
CC
3
Cov
0%
format_dna
6.0
verbose
6.0
usage
2.0
CRAP 3.6
1 #!/usr/bin/env perl
2 # ABSTRACT: A demo implementation to filter fastx files by length
3 # PODNAME: fqlen.pl
4  
5 use 5.014;
6 use FASTX::Reader;
7 use Getopt::Long;
8 use File::Basename;
9 use Pod::Usage;
10 my $PROGRAM = 'fqlen';
11 my $VERSION = '0.1';
12 my $DESCR = 'Select sequences by size';
13 my $AUTHOR = 'Andrea Telatin (proch)';
14  
15  
16  
17 my %schemes = (
18 'raw' => 'Do not change sequence name (default)',
19 'num' => 'Numbered sequence (see also -p)',
20 'file'=> 'Use file basename as prefix',
21 );
22 my $opt_minlen = 1;
23 my $opt_maxlen = 0;
24 my (
25 $opt_fasta_format,
26 $opt_fasta_width,
27 $opt_verbose,
28 $opt_prefix,
29 $opt_strip_comment,
30 $opt_addlength,
31 $opt_help,
32 );
33 my $opt_name = 'raw';
34 my $opt_separator = '.';
35 my $_opt = GetOptions(
36 'm|min=i' => \$opt_minlen,
37 'x|max=i' => \$opt_maxlen,
38 'f|fasta' => \$opt_fasta_format,
39 'w|fasta-width=i' => \$opt_fasta_width,
40 'c|strip-comment' => \$opt_strip_comment,
41 'n|namescheme=s' => \$opt_name,
42 'v|verbose' => \$opt_verbose,
43 'l|len' => \$opt_addlength,
44 's|separator=s' => \$opt_separator,
45 'p|prefix=s' => \$opt_prefix,
46 'h|help' => \$opt_help,
47 );
48 pod2usage({-exitval => 0, -verbose => 2}) if $opt_help;
49 usage() unless defined $ARGV[0];
50  
51 # Check schemes
52 if (not defined $schemes{$opt_name}) {
53 usage();
54 die "Name scheme -n '$opt_name' is not valid [choose: ", join(",", keys %schemes), "]\n";
55 }
56  
57 # Separator for read name is '' if no prefix is given
58 my $sep = '';
59 $sep = $opt_separator if ($opt_prefix);
60  
61 my $global_counter = 0;
62 my $global_printed = 0;
63 my %check_reads;
64 foreach my $input_file (@ARGV) {
65 if (! -e "$input_file") {
66 verbose(qq(Skipping "$input_file": file not found));
67 next;
68 } elsif (-d "$input_file") {
69 verbose(qq(Skipping "$input_file": is a directory));
70 next;
71 }
72 my $reader = FASTX::Reader->new({ filename => "$input_file" });
73 my $local_counter = 0;
74 my $local_printed = 0;
75 while (my $s = $reader->getRead() ) {
76
77 my $len = length($s->{seq});
78 $global_counter++;
79 $local_counter++;
80 # Length Check
81 next if ($len < $opt_minlen);
82 next if ($opt_maxlen > 0 and $len > $opt_maxlen);
83
84  
85 # Read name
86 my $name;
87 if ($opt_name eq 'raw') {
88 $name = $s->{name};
89 } elsif ($opt_name eq 'file') {
90 $name = basename($input_file) . $opt_separator . $local_counter;
91 } elsif ($opt_name eq 'num') {
92 $name = $opt_prefix . $sep . $global_counter;
93 }
94
95  
96 # Comment
97 $name .= " " .$s->{comment} unless ($opt_strip_comment);
98 # Add length
99 $name .= " length=" . $len if ($opt_addlength);
100 my $sequence = $s->{seq};
101 my $quality = $s->{qual};
102  
103 if ($check_reads{$name}) {
104 die "FATAL ERROR: Duplicate read name <$name> using scheme $opt_name.\nReading <$input_file>, sequence number $local_counter (total sequences $global_counter)\n";
105 }
106  
107 $check_reads{$name}++;
108 $global_printed++;
109 $local_printed++;
110 # Print
111 if ($opt_fasta_format or not defined $quality) {
112 # In fasta format
113 print '>', $name, "\n", format_dna($sequence, $opt_fasta_width);
114 } else {
115 # In fastq format
116 print '@', $name, "\n", $sequence, "\n+\n", $quality, "\n";
117 }
118 }
119 if ($opt_verbose and $global_counter > 0) {
120 say STDERR " [$input_file]. ", sprintf("%.2f", 100*$global_printed/$global_counter), "% sequences selected.";
121 }
122  
123 }
124  
125 if ($opt_verbose and $global_counter > 0) {
126 say STDERR " [ALL_DONE]. ", sprintf("%.2f", 100*$global_printed/$global_counter), "% sequences selected.";
127 }
128 sub format_dna {
129 my ($sequence, $width) = @_;
130 if (not defined $width) {
131 return "$sequence\n";
132 }
133  
134 my $formatted;
135 my $line = $width;
136 for (my $i=0; $i<length($sequence); $i+=$line) {
137 my $frag = substr($sequence, $i, $line);
138 $formatted.=$frag."\n";
139 }
140 return $formatted;
141 }
142  
143 sub verbose {
144 return unless $opt_verbose;
145 say STDERR " - $_[0]";
146 }
147  
148 sub usage {
149 print STDERR<<END;
150 -------------------------------------------------------------------------------
151 $PROGRAM - $DESCR (ver. $VERSION)
152 -------------------------------------------------------------------------------
153 Synopsis:
154 fqlen [options] FILE1 FILE2 ... FILEn
155  
156 -m, --min INT Minimum length to print a sequence [default: 1]
157 -x, --max INT Maximum length to print a sequence [default: 0 for unlimited]
158 -l, --len Add read length as comment
159 -f, --fasta Force FASTA output (default: as INPUT)
160 -w, --fasta-width INT Paginate FASTA sequences (default: no)
161  
162  
163 -n, --namescheme Sequence name scheme:
164 END
165 foreach my $scheme (sort keys %schemes) {
166 say "\t\t- \"$scheme\" ($schemes{$scheme})";
167 }
168 say STDERR<<END;
169 -p, --prefix STR Use as sequence name prefix this string
170 -c, --strip-comment Remove sequence comment (default: no)
171
172 FILEn A FASTA or FASTQ file, gzipped is ok
173  
174 Note that usage with multiple files can raise errors (eg. duplicate sequence
175 name). Also, wrong formatting if mixing fasta and fastq files without
176 specifying --fasta.
177 -------------------------------------------------------------------------------
178 END
179 }
180  
181 __END__