-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfastq_combine.pl
executable file
·136 lines (128 loc) · 3.58 KB
/
fastq_combine.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
#!/usr/bin/perl
use strict;
use Getopt::Long;
# defines --fasta option (output as fasta instead of fastq)
# defines --no-check option (don't check for equal lengths of input files)
my $fasta;
my $check;
GetOptions('fasta' => \$fasta,
'no-check' => \$check);
# input fastq-files
my $inputfastq1 = $ARGV[0];
my $inputfastq2 = $ARGV[1];
my $prefix = $inputfastq1;
$prefix=~s/_(read|r|)(1|2).(fq|fastq)//;
open (FASTQ1 , '<' , $inputfastq1) or die "\nCreate a single file out of paired end read files\nUSAGE:\tperl fastq_combine.pl [--fasta --no-check] readfile_1.fastq readfile_2.fastq\n\n";
open (FASTQ2 , '<' , $inputfastq2) or die "\n";
# Check if read files are of same length
unless ($check==1){
my$length1=`wc -l $inputfastq1`;
my$length2=`wc -l $inputfastq2`;
$length1=~s/\s.+//g;
$length2=~s/\s.+//g;
if($length1 != $length2){die "\nInput files have unequal length!\n\n";}
}
my $checkline1;
my $checkline2;
# when --fasta option is invoked
if($fasta==1){
# create output file and check if it alreads exists
my $outputfile=$prefix."_comb.reads.fas";
if(-e $outputfile){
print "\n$outputfile already exists! Overwrite? (Y|n) ";
chomp(my $input = <STDIN>);
if ($input eq "Y"){
`rm -rvf $outputfile`;
print "\noverwriting\n";
}
else{
die"\nexiting!\n"}
}
open (OUTFILE , '>>', $outputfile);
my $linecounter=0;
my $counter=-1;
my @fastq1;
my @fastq2;
my $fastq1;
my $fastq2;
my$fastq1line;
my$fastq2line;
while(not eof FASTQ1 and not eof FASTQ2){
$fastq1 = <FASTQ1>;
$fastq2 = <FASTQ2>;
push(@fastq1,$fastq1);
push(@fastq2,$fastq2);
$linecounter++;
$counter++;
if($counter == 3){
# replace '@' from fastq-id with '>'
$fastq1line=$fastq1[0];
$fastq1line=~s/@/>/g;
$fastq2line=$fastq2[0];
$fastq2line=~s/@/>/g;
# perform name check only when option is set
unless($check==1){
$checkline1=$fastq1line;
$checkline1=~s/\/1//g;
$checkline2=$fastq2line;
$checkline2=~s/\/2//g;
if($checkline1 ne $checkline2){
my $wrongline=$linecounter-3;
die "\nERROR: Read names do not match!\n Check input files line number $wrongline\n\n"}
}
# Print the first 2 lines of file (id + sequence)
print OUTFILE "$fastq1line$fastq1[$counter-2]";
print OUTFILE "$fastq2line$fastq2[$counter-2]";
$counter = -1;
undef @fastq1;
undef @fastq2;
}
}
}
else{
# create output file and check if it alreads exists
my $outputfile=$prefix."_comb.reads.fq";
if(-e $outputfile){
print "\n$outputfile already exists! Overwrite? (Y|n) ";
chomp(my $input = <STDIN>);
if ($input =~/Y/){
`rm -rf $outputfile`;
print "\noverwriting\n";
}
else{
die"\nexiting!\n\n"}
}
open (OUTFILE , '>>', $outputfile);
my $linecounter=0;
my $counter=-1;
my @fastq1;
my @fastq2;
my $fastq1;
my $fastq2;
while(not eof FASTQ1 and not eof FASTQ2){
$fastq1 = <FASTQ1>;
$fastq2 = <FASTQ2>;
push(@fastq1,$fastq1);
push(@fastq2,$fastq2);
$linecounter++;
$counter++;
if($counter == 3){
# perform name check only when option is set
unless($check==1){
$checkline1=$fastq1[0];
$checkline1=~s/\/1//g;
$checkline2=$fastq2[0];
$checkline2=~s/\/2//g;
if($checkline1 ne $checkline2){
my $wrongline=$linecounter-3;
die "\nERROR: Read names do not match!\n Check input files line number $wrongline\n\n"}
}
print OUTFILE "$fastq1[$counter-3]$fastq1[$counter-2]$fastq1[$counter-1]$fastq1[$counter]";
print OUTFILE "$fastq2[$counter-3]$fastq2[$counter-2]$fastq2[$counter-1]$fastq2[$counter]";
$counter = -1;
undef @fastq1;
undef @fastq2;
}
}
}
close(OUTFILE);