-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_fastq_se_sj.pl
executable file
·268 lines (227 loc) · 6.24 KB
/
sort_fastq_se_sj.pl
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/perl
use strict;
use warnings;
#use Cwd;
##### ##### ##### ##### #####
use Getopt::Std;
use vars qw( $opt_a $opt_b $opt_c $opt_v $opt_w);
# Usage
my $usage = "
sort_fasstq.pl - uses blat to sort fastq sequences into hits and misses.
by
Brian J. Knaus
May 2010
Copyright (c) 2010 Brian J. Knaus.
License is hereby granted for personal, academic, and non-profit use.
Commercial users should contact the author (http://brianknaus.com).
Great effort has been taken to make this software perform its said
task however, this software comes with ABSOLUTELY NO WARRANTY,
not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Usage: perl script.pl options
required:
-a fastq input file.
-b reference fasta file.
optional:
-c inset in nucleotides, used to remove barcodes [default = 0].
-v verbose mode [T/F, default is F].
-w delete intermediate (blat) files [T/F, default is T]
Verion 0.2.
";
# command line processing.
getopts('a:b:c:v:w:');
die $usage unless ($opt_a);
die $usage unless ($opt_b);
my ($infa, $ref, $inset, $nfile, $verb, $clean);
$infa = $opt_a if $opt_a;
$ref = $opt_b if $opt_b;
$inset = $opt_c ? $opt_c : 0;
$verb = $opt_v ? $opt_v : "F";
$clean = $opt_w ? $opt_w : "T";
##### ##### ##### ##### #####
# Subfunctions.
# Check fastq file.
sub fastqchk{
my $inf = shift;
my @temp;
my $in;
# Check that file exists.
if(-e $inf){} else {
die "Error: $inf does not exist!\n";
}
open($in, "<", $inf) or die "Can't open $inf: $!";
chomp($temp[0] = <$in>); # First line is id.
chomp($temp[1] = <$in>); # Second line is sequence.
chomp($temp[2] = <$in>); # Third line is id.
chomp($temp[3] = <$in>); # Fourth line is quality.
# Check identifier line.
if($temp[0] !~ /^@/){
print "Unexpected file format!\n";
print "Line1: $temp[0]\n";
print "Line2: $temp[1]\n";
print "Line3: $temp[2]\n";
print "Line4: $temp[3]\n";
die "Error: Fastq file expected.\n";
}
# Check sequence.
$_ = $temp[1];
s/A//gi;
s/C//gi;
s/T//gi;
s/G//gi;
s/N//gi;
if(length($_)>0){
print "Unexpected file format!\n";
print "Line1: $temp[0]\n";
print "Line2: $temp[1]\n";
print "Line3: $temp[2]\n";
print "Line4: $temp[3]\n";
die "Invalid character in sequence: $_.\n";
}
# Check identifier line.
if($temp[2] !~ /^\+/){
print "Unexpected file format!\n";
print "Line1: $temp[0]\n";
print "Line2: $temp[1]\n";
print "Line3: $temp[2]\n";
print "Line4: $temp[3]\n";
die "Error: Fastq file expected.\n";
}
close $in or die "$in: $!";
}
sub outname {
my $inf = shift;
my @temp = split("/", $inf);
$inf = $temp[$#temp];
@temp = split(/\./, $inf);
my $outf = $temp[0];
return($outf);
}
sub fastq2fasta {
my $infa = shift;
#
my $outfa = outname($infa);
#
my ($ina, $out, $outna);
my @temp;
my ($counta);
open($ina, "<", $infa) or die "Can't open $infa: $!";
open($out, ">", $outfa.".fa") or die "Can't open $outfa.fa: $!";
while (<$ina>){
chomp($temp[0] = $_); # First line is an id.
chomp($temp[1] = <$ina>); # Second line is a sequence.
chomp($temp[2] = <$ina>); # Third line is an id.
chomp($temp[3] = <$ina>); # Fourth line is quality.
#
# Prune first char.
$temp[0] = substr($temp[0], 1);
# Count Ns.
# $counta = ($temp[1] =~ tr/N//);
# Process Ns.
# if ($counta >= 1){
# $ns++;
# if ($nfile eq "T"){
# print $outna "@".$temp[0]."\n";
# print $outna "$temp[1]\n";
# print $outna "$temp[2]\n";
# print $outna "$temp[3]\n";
# #
# }
# next;
# }
# Print to fasta file.
print $out ">$temp[0]\n";
print $out "$temp[1]\n";
}
close $ina or die "$ina: $!";
close $out or die "$out: $!";
}
sub blat {
my $outf = shift;
$outf = outname($outf);
my $ref = shift;
my $temp;
# $temp = join("", "blat $ref $outf.fa ", $outf, "_blatout.psl -tileSize=6 -stepSize=1 -minScore=12 -noHead -fine -maxIntron=0");
$temp = join("", "/local/cluster/bin/blat $ref $outf.fa ", $outf, "_blatout.psl -tileSize=6 -stepSize=1 -minScore=12 -noHead -fine -maxIntron=0");
#
# $temp = join("", "blat $ref $outf.fa ", $outf, "_blatout.psl -tileSize=6 -stepSize=1 -minIdentity=90 -noHead -maxIntron=750000");
system $temp;
}
sub blathash {
my $outf = shift;
$outf = outname($outf);
my $in;
my @temp;
my %blats;
open( $in, "<", $outf.'_blatout.psl') or die "Can't open ", $outf, "_blatout.psl: $!";
while (<$in>){
@temp = split(/\t/);
$blats{$temp[9]} = 0;
}
close $in or die "$in: $!";
return(keys(%blats));
}
sub sortfile {
my $infa = shift;
my $outfa = outname($infa);
my %blats;
my @temp;
my ($ina, $outa, $outma);
# Generate hash of blat hits.
@temp = blathash($infa);
foreach(@temp){
$blats{"$_"} = 0;
}
undef @temp;
# Process fastq files.
open( $ina, "<", $infa) or die "Can't open $infa: $!";
open( $outa, ">", $outfa."_hit.fq") or die "Can't open ", $outfa, "_hit.fq: $!";
open( $outma, ">", $outfa."_miss.fq") or die "Can't open ", $outfa, "_miss.fq: $!";
while (<$ina>){
chomp($temp[0] = $_); # First line is an id.
chomp($temp[1] = <$ina>); # Second line is a sequence.
chomp($temp[2] = <$ina>); # Third line is an id.
chomp($temp[3] = <$ina>); # Fourth line is quality.
if (exists $blats{substr($temp[0], 1)}){
# Print to fastq file.
# Hits.
print $outa "$temp[0]\n";
print $outa "$temp[1]\n";
print $outa "$temp[2]\n";
print $outa "$temp[3]\n";
} else {
# Print to fastq file.
# Misses.
print $outma "$temp[0]\n";
print $outma "$temp[1]\n";
print $outma "$temp[2]\n";
print $outma "$temp[3]\n";
}
}
close $ina or die "$ina: $!";
close $outa or die "$outa: $!";
close $outma or die "$outma: $!";
}
sub clean {
my $infa = shift;
my $outf = outname($infa);
unlink $outf.".fa";
unlink $outf."_blatout.psl";
}
##### ##### ##### ##### #####
# Globals.
##### ##### ##### ##### #####
# Main.
# Check file format.
fastqchk($infa);
# Create a fasta file for input to blat.
fastq2fasta($infa);
# Call blat.
blat($infa, $ref);
# Create sorted files.
sortfile($infa);
# Clean up temp files.
if ($clean eq "T"){
clean($infa);
}
##### ##### ##### ##### #####
# EOF.