-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfastq_pileup
executable file
·173 lines (137 loc) · 3.79 KB
/
fastq_pileup
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
#!/usr/bin/perl -w
# Only works on Illumina v1.8 or greater fastq data ...
# Copyright © 2015, Owen Marshall
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
use strict;
use File::Basename;
$|++;
unless (@ARGV) {
help();
}
my %vars = (
'sample' => 2000000,
'output_cutoff' => 1,
);
my %vars_details = (
'sample' => 'Analyse no more than this number of reads (use "all" to process the entire file)',
'output_cutoff' => 'Do not output sequences with fewer than this number of reads',
);
my @in_files;
process_cli();
analyse();
exit 0;
sub analyse {
foreach my $fn (@in_files) {
my ($fhead) = fileparse($fn, qr/\.[^.]*/);
my $fout = "$fhead.pileup.csv";
print STDERR "Reading file $fn ...\n";
open(IN, "gunzip -c $fn |") || die "ERROR: Cannot open input for reading: $!";
my %pile;
my $lines=0;
my ($id, $seq, $desc, $qual, $total);
while (<IN>) {
chomp;
my $lnum = $lines%4 +1;
# Switch is too slow ...
$lines++;
if ($lnum == 1) {$id = $_; $seq =""; $desc=""; $qual=""; next};
if ($lnum == 2) {$seq = $_; next};
if ($lnum == 3) {$desc = $_; next};
$qual = $_;
if ($qual) {
# full set; process
$total++;
unless ($vars{'sample'} eq "all") {
last if $total > $vars{'sample'};
}
if ($total%100000 == 0) {
print STDERR "$total reads processed ...\r";
}
$pile{$seq}++;
}
}
close IN;
open(my $fh, '>', $fout) || die "Could not open output file for writing: $!\n";
foreach my $k (sort {$pile{$b} <=> $pile{$a}} keys %pile) {
last if $pile{$k} < $vars{'output_cutoff'};
print $fh "$k\t$pile{$k}\n";
}
close $fh;
}
}
sub process_cli {
foreach (@ARGV) {
if (/--(.*)=(.*)/) {
unless (defined($vars{$1})) {
print STDERR "Did not understand $_ ...\n";
help();
}
my ($v, $opt) = ($1,$2);
$vars{$v} = $opt;
next;
} elsif (/--h[elp]*/) {
help();
} elsif (/--(.*)/) {
# if no parameter is specified we assume it's a switch ...
if (defined($vars{$1})) {
$vars{$1} = 1;
} else {
print STDERR "Did not understand $_ ...\n";
help();
}
next;
}
push @in_files, $_;
}
}
sub help {
print STDOUT <<EOT;
fastq-collapse.pl
Performs a read pile-up on a Illumina v1.8 or greater FASTQ file.
Options:
EOT
my $opt_len = 0;
foreach (keys %vars) {
my $l = length($_);
$opt_len = $l if $l > $opt_len;
}
$opt_len+=2;
my $cols= `tput cols` || 80;
my ($v, $val, $def, $def_format);
my $help_format = "format STDOUT =\n"
.' '.'^'.'<'x$opt_len . ' '. '^' . '<'x($cols-$opt_len-4) . "\n"
.'$v, $def_format'."\n"
.' '.'^'.'<'x$opt_len . ' '. '^' . '<'x($cols-$opt_len-6) . "~~\n"
.'$v, $def_format'."\n"
.".\n";
eval $help_format;
die $@ if $@;
foreach my $k (sort (keys %vars)) {
($v, $val, $def) = ($k, $vars{$k}, $vars_details{$k});
$def||="";
$def_format = $val ? "$def\n\r[Current value: $val]" : $def;
$v = "--$v";
# format =
# ^<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#$v, $def_format
# ^<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
#$v, $def_format
#.
write();
}
print STDOUT "\n";
exit 1;
}