forked from hemberg-lab/scRNA.seq.course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_Flexible_UMI_Demultiplexing.pl
211 lines (189 loc) · 6.25 KB
/
1_Flexible_UMI_Demultiplexing.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
use strict;
use warnings;
# Matches upto two mismatches between observed cell barcodes and the expected cell barcodes
# Excludes reads with problematic UMIs: >= 80% A, >= 80% T, contained in adaptor sequence. - Note do not provide adaptors for short UMI datasets (fewer than 7 bases) since there is a high probability of real UMIs being contained in the adaptor for such cases.
# Allows barcodes to contain ambiguous bases
# Allows trailing bases at the end of the barcode sequence but requires barcodes to begin from the first base in the barcode sequence.
if (@ARGV != 6) {
print STDERR "perl 1_Flexible_UMI_Demultiplexing.pl read1.fq read2.fq b_structure index mismatch prefix\n";
print STDERR "
read1.fq : barcode/umi containing read
read2.fq : non-barcode containing read
b_structure : a single string of the format C##U# or U#C##
where C## is the cell-barcode and U# is the UMI.
e.g. C10U4 = a 10bp cell barcode followed by a 4bp UMI
index : file containg a single column of expected cell-barcodes.
if equal to \"UNKNOWN\" script will output read counts for each unique barcode.
mismatch : maximum number of permitted mismatches (recommend 2)
prefix : prefix for output fastq files.\n";
exit(1);}
my $infile1 = $ARGV[0];
my $infile2 = $ARGV[1];
my $barcodestructure = $ARGV[2];
my $MAXmismatch = $ARGV[4];
# Parse Barcode Structure #
my $order = -1;
my $C_len = -1;
my $U_len = -1;
if ($barcodestructure =~ /^C(\d+)U(\d+)$/) {
$order=1;
$C_len = $1;
$U_len = $2;
print "Barcode Structure: $C_len bp CellID followed by $U_len bp UMI\n";
} elsif ($barcodestructure =~ /^U(\d+)C(\d+)$/) {
$order = 0;
$C_len = $2;
$U_len = $1;
print "Barcode Structure: $U_len bp UMI followed by $C_len bp CellID\n";
} else {
die "$barcodestructure not recognized.\n";
}
# ----------------------- #
my $OUTprefix = $ARGV[5]; #prefix for output
#Ensure output directory exists
if ($OUTprefix =~ /^(.+)\/[^\/]$/) {
if ($1 ne ".") {
system("mkdir -p $1");
}
}
# Read Expected Cell Barcodes #
my %CellBarcodes = ();
my %ofhs = ();
if ($ARGV[3] ne "UNKNOWN") {
open (my $ifh, $ARGV[3]) or die "Cannot open $ARGV[3]\n";
my $index=1;
while (<$ifh>) {
chomp;
if ($_ =~/^#/) {next;}
my $barcode = $_;
$CellBarcodes{$barcode} = $index;
open(my $fh,'>',"$OUTprefix\_$barcode.fq") or die $!;
$ofhs{$index} = $fh;
$index++;
} close($ifh);
}
# --------------------------- #
### Process Reads ###
# Summary Statistics
my $NotProperBarcodes = 0;
my $NotPossibleCell = 0;
my $AmbiguousCell = 0;
my $ExactMatch = 0;
my $Mismatch = 0;
my $BadUMI = 0;
my $total_reads = 0;
my $OutputReads=0;
open (my $ifh1, $infile1) or die $!;
open (my $ifh2, $infile2) or die $!;
while(<$ifh1>) {
my $file1line = $_;
my $file2line = <$ifh2>;
if ($file1line =~ /^@/) { #Skip any file headers
# Ensure matching pair of reads
my @thing1 = split(/\s+/,$file1line);
my @thing2 = split(/\s+/,$file2line);
#my $readname1 = chop($thing1[0]);
#my $readname2 = chop($thing2[0]);
#if ($readname1 ne $readname2) {die "file1 & file2 readnames don't match! $thing1[0] $thing2[0]\n";}
my $readname = $thing1[0];
my $barcodes = <$ifh1>;
my $read = <$ifh2>;
$total_reads++;
# Parse barcodes
my $CellID = ""; my $UMI = "";
if ($order) {
if ($barcodes =~ /^([ATCGNUKMRYSWBVHDX]{$C_len})([ATCGNUKMRYSWBVHDX]{$U_len})/) {
$CellID = $1; $UMI = $2;
} else {$NotProperBarcodes++; next;}
} else {
if ($barcodes =~ /^([ATCGNUKMRYSWBVHDX]{$U_len})([ATCGNUKMRYSWBVHDX]{$C_len})/) {
$CellID = $2; $UMI = $1;
} else {$NotProperBarcodes++; next;}
}
# if ($barcodes =~ /^([ATCGNUKMRYSWBVHDX]{11})([ATCGNUKMRYSWBVHDX]{10})/) {
# Correct for upto two mismatches between observed and expected cell barcodes
if ($ARGV[3] ne "UNKNOWN") {
my $mismatches = 0;
if (!exists($CellBarcodes{$CellID})) { # Not an expected barcode
# Barcode contains uncertain bases -> convert to wildcards and pattern match on expected barcodes. (given priority over barcodes with higher confidence mismatches)
if ($CellID !~ /^[ATCG]+$/) {
$mismatches = () = $CellID =~ /[^ATCG]/g; # count uncertain bases as mismatches
$CellID =~ s/[^ATCG]/./g; #Turn non-ATCG bases into wildcards
}
my @matches = ();
my %close = ();
foreach my $barcode (keys(%CellBarcodes)) {
if ($barcode =~/$CellID/) { # Match but with uncertainty
push(@matches, $barcode);
} else {
if (scalar(@matches == 0)) { # Count mismatches
my $count = ( $barcode ^ $CellID ) =~ tr/\0//;
if ($count >= length($barcode)-$MAXmismatch) { # Allow upto 2 mismatches
$close{$barcode} = $count;
}
}
}
}
# If exact matches with uncertainty then give those priority, otherwise keep the most similar expected barcodes
if (scalar(@matches) == 0 && scalar(keys(%close)) > 0) { # Has 1 or 2 mismatches
my $max = my_max(values(%close)); # Closest match
$mismatches = length($CellID)-$max;
foreach my $code (keys(%close)) {
if ($close{$code} == $max) {
push(@matches,$code);
}
}
}
if (scalar(@matches) == 1) { # single best match
$CellID = $matches[0];
$Mismatch++;
} elsif (scalar(@matches) > 1) { #More than one equally good match
$AmbiguousCell++;
next;
} else { # No match
$NotPossibleCell++;
next;
}
} else { # Exact match
$ExactMatch++;
}
} #If known barcodes
if ($ARGV[3] ne "UNKNOWN") {
# Has Acceptable Barcode
<$ifh1>;<$ifh2>; #+'s
my $file1qual = <$ifh1>;
my $file2qual = <$ifh2>;
my $handle = $ofhs{$CellBarcodes{$CellID}};
print $handle "$readname:$UMI\n$read+\n$file2qual";
$OutputReads++;
} else {
$CellBarcodes{$CellID}++;
}
} else {next;}
}
if ($ARGV[3] ne "UNKNOWN") {
print STDERR "
Doesn't match any cell: $NotPossibleCell
Ambiguous: $AmbiguousCell
Exact Matches: $ExactMatch
Contain mismatches: $Mismatch
Input Reads: $total_reads
Output Reads: $OutputReads\n";
close($ifh1);
close($ifh2);
foreach my $ofh (keys(%ofhs)) {close($ofhs{$ofh});}
} else {
print STDERR "Bad UMI: $BadUMI\n";
my @Codes = sort { $CellBarcodes{$b} <=> $CellBarcodes{$a} } keys(%CellBarcodes);
foreach my $code (@Codes) {
print "$code ".$CellBarcodes{$code}."\n";
}
}
sub my_max {
if (scalar(@_) == 1) {return($_[0])};
my $max = shift;
foreach my $ele (@_) {
if ($ele > $max) {$max = $ele;}
}
return($max);
}