forked from molgenis/ngs-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demultiplex_miseq_illumina_barcoded.pl
executable file
·234 lines (204 loc) · 6.95 KB
/
demultiplex_miseq_illumina_barcoded.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
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use File::Glob ':glob';
use File::stat;
use Getopt::Long;
use File::Basename;
use Switch;
use Text::CSV;
use Scalar::Util;
#perl ../demultiplex.pl -end1 test_1.fq -end2 test_2.fq -index index.fq -csvfile SampleSheetConverted.csv -output ~/miseq/testoutput
my ($help, $end1, $end2, $indexfile, $csvfile, $outputfolder);
#### get options
GetOptions(
"h" => \$help,
"end1=s" => \$end1,
"end2=s" => \$end2,
"index=s" => \$indexfile,
"csvfile=s" => \$csvfile,
"output=s" => \$outputfolder
);
usage() and exit(1) if $help;
# mandatory args
usage() and exit(1) unless $end1;
usage() and exit(1) unless $end2;
usage() and exit(1) unless $indexfile;
usage() and exit(1) unless $csvfile;
usage() and exit(1) unless $outputfolder;
#Check if output dir is empty
print "Checking if output directory is empty..\n";
opendir(DIR, $outputfolder) or die "$!";
readdir DIR; # reads .
readdir DIR; # reads ..
if (readdir DIR) { #now there should be a file or folder
print "Output directory is not empty!\n";
print "Please manually clean the directory or choose a different output folder!\n";
exit (1);
}
else {
print "Output directory is empty!\n";
print "Starting analysis now..\n";
}
close DIR;
print "Started!\n";
#Open files
open (END1, "<", $end1) or die("Unable to open file $end1");
open (END2, "<", $end2) or die("Unable to open file $end2");
open (INDEX, "<", $indexfile) or die("Unable to open file $indexfile");
#Read files into array
my @end1file = <END1>;
my @end2file = <END2>;
my @indexfile = <INDEX>;
#Close files
close(END1);
close(END2);
close(INDEX);
my $end1lastindex = $#end1file;
#read index file and retrieve indx from barcode column
#Read CSV file
my $csv = Text::CSV->new();
open (CSV, "<", $csvfile) or die $!;
my @headers;
while (<CSV>) {
next if ($. != 1);
if ($csv->parse($_)) {
@headers = $csv->fields();
} else {
my $err = $csv->error_input;
print "Failed to parse line: $err";
}
}
close(CSV);
####Retrieve column indices
my %headers_to_indexes = (map { $headers[$_] => $_ } (0 .. $#headers));
my $sampleID;
my $samplebarcode;
my %sample_barcode_map;
open (CSV, "<", $csvfile) or die $!;
while (<CSV>) {
next if ($. == 1);
if ($csv->parse($_)) {
my @columns = $csv->fields();
$sampleID = $columns[$headers_to_indexes{"Sample_ID"}];
$samplebarcode = $columns[$headers_to_indexes{"index"}];
if (exists $sample_barcode_map{$sampleID}) {
#Do nothing
}else{
#Add to hash
$sample_barcode_map{ $sampleID } = $samplebarcode;
}
}
}
close(CSV);
#Print sample barcode combinations
print "Found the following sample barcode combinations:\n";
foreach my $key (sort keys %sample_barcode_map){
print "$key\t$sample_barcode_map{$key}\n";
}
my $tot = ($end1lastindex+1)/4;
print "\n\nTotal number of reads found: $tot\n\n";
#Cut file extensions
my $prefix_out_end1 = $end1;
my $prefix_out_end2 = $end2;
$prefix_out_end1 =~ s/.fq//gs;
$prefix_out_end2 =~ s/.fq//gs;
#Actual code
for (my $i=0; $i <= $end1lastindex; $i = $i + 4){
#print "$i\n";
#print $end1file[$i] . $end1file[$i+1] . $end1file[$i+3] . "\n"
#Retrieve information from read IDs
my $end1idline = $end1file[$i];
my $end1seq = $end1file[$i+1];
my $end1qual = $end1file[$i+3];
my $end1id;
my $end1idx;
my $end2idline = $end2file[$i];
my $end2seq = $end2file[$i+1];
my $end2qual = $end2file[$i+3];
my $end2id;
my $end2idx;
my $indexidline = $indexfile[$i];
my $indexid;
my $indexidx;
if ($end1idline =~ m/(.+) .+:.+:.+:(.+)/gs){
$end1id = $1;
$end1idx = $2;
chomp $end1idx;
#print "$end1id\t$end1idx\n";
}
if ($end2idline =~ m/(.+) .+:.+:.+:(.+)/gs){
$end2id = $1;
$end2idx = $2;
chomp $end2idx;
}
if ($indexidline =~ m/(.+) .+:.+:.+:(.+)/gs){
$indexid = $1;
$indexidx = $2;
chomp $indexidx;
}
#Check if all info matches between end1, end2 and index file
if ($end1id eq $end2id && $end1id eq $indexid && $end2id eq $indexid && $end1idx eq $end2idx && $end1idx eq $indexidx && $end2idx eq $indexidx){
#Retrieve barcode corresponding to id
my $brcd = $sample_barcode_map{ "Sample$end1idx" };
#print "$brcd\n";
#Write output away
open (OUT1, ">>", "$outputfolder/$prefix_out_end1\_$brcd.fq") or die("Unable to open file $outputfolder/$prefix_out_end1\_$brcd.fq");
open (OUT2, ">>", "$outputfolder/$prefix_out_end2\_$brcd.fq") or die("Unable to open file $outputfolder/$prefix_out_end2\_$brcd.fq");
print OUT1 $end1idline . $end1seq . "+\n" . $end1qual;
print OUT2 $end2idline . $end2seq . "+\n" . $end2qual;
close(OUT1);
close(OUT2);
}else{
print "Reads with ID $end1id don't match sequence ID or barcode ID\nExiting now!\n";
exit(1);
}
}
print "\nGathering statistics..\n";
#Count statistics
my @files1 = glob "$outputfolder/$prefix_out_end1*";
my @files2 = glob "$outputfolder/$prefix_out_end2*";
my $files1index = $#files1;
my $files2index = $#files2;
if ($files1index != $files2index){
print "Number of 1st en 2nd end output files is not equal!\n";
print "Please check if files are missing!\nExiting now!\n";
exit(1);
}
open (LOG, ">", "$outputfolder/demultiplex_log.log") or die("Unable to open file $outputfolder/demultiplex_log.log");
#Print statistics per barcode
print LOG "\n\nFILE\tTOTAL_NUMBER_OF_READ_PAIRS\tPERCENTAGE_OF_TOTAL\n";
for (my $k=0; $k <= $files1index; $k++){
my $first = `wc -l $files1[$k] | awk '{print \$1}'`;
my $second = `wc -l $files2[$k] | awk '{print \$1}'`;
chomp $first;
chomp $second;
#print "$first\t$second\n";
if ($first == $second){
#calculate statistics
my $reads = ($first/4);
my $perc = ($reads/$tot)*100;
print LOG $files1[$k] . "\t$reads\t$perc\n";
}else{
print "Detected different number of lines in " . $files1[$k] . " and " . $files2[$k] . "\n";
print "Exiting now!";
exit(1);
}
}
close(LOG);
print "\n\nFinished!\n";
sub usage {
print <<EOF;
#########################################################################################
This script demultiplexes paired-end illumina barcoded ngs data generated by the MiSeq.
#########################################################################################
Usage: ./demultiplex_miseq_illumina_barcoded.pl
\t-end1 First end of pair to demultiplex
\t-end2 Second end of pair to demutliplex
\t-index Third file containing the barcodes (I file)
\t-csvfile CSV file containing the sample IDs and used barcodes
\t-output Folder to write demultiplexed output to
#########################################################################################
EOF
}