-
Notifications
You must be signed in to change notification settings - Fork 15
/
nanopore_barcode.pl
executable file
·227 lines (188 loc) · 6.07 KB
/
nanopore_barcode.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
#!/usr/bin/perl
use warnings;
use strict;
use Pod::Usage; ## uses pod documentation in usage code
use Getopt::Long qw(:config auto_version auto_help);
our $VERSION = "0.3";
=head1 NAME
nanopore_barcode.pl -- generates a random barcode suitable for
the nanopore sequencer
=head1 SYNOPSIS
./nanopore_barcode.pl [options]
=head2 Options
=over 2
=item B<-length>
Length of barcode to generate, excluding prefix/suffix (default 38)
=item B<-count>
Number of barcodes to generate (default 1)
=item B<-threshold>
Difference threshold for barcode inclusion (default 0.2)
=item B<-prefix>
Prefix sequence for barcode (default I<GGTGCTG>)
=item B<-suffix>
Suffix sequence for barcode (default I<TTAACCT>)
=item B<-order>
Order of base selection (default I<ATCG>)
=item B<-baseprob>
Base probability, excluding previous base (commma-separated, default
I<0.45,0.30,0.25>)
=item B<-showtm> I<type>
Show melting temperature of (p)refix, (s)uffix, or (e)ntire sequence,
or a certain distance from the start of the sequence (+bp) or the end
of the sequence (-bp).
=back
=head1 DESCRIPTION
Note: when more than one barcode is generated, each new barcode is
checked to make sure it is sufficiently different from previously
added sequences.
=cut
my $length = 38;
my $count = 1;
my $threshold = 0.2;
my $prefix = "GGTGCTG";
my $suffix = "TTAACCT";
my $baseOrder = "ATCG";
my $baseProb = "0.25,0.50,0.75";
my $showTm = "e"; # entire sequence only
GetOptions('length=i' => \$length,
'count=i'=> \$count,
'threshold=f'=> \$threshold,
'prefix=s' => \$prefix,
'suffix=s' => \$suffix,
'order=s' => \$baseOrder,
'baseprob=s' => \$baseProb,
'showtm=s' => \$showTm,
) or pod2usage(1);
my @baseProbs = split(/,/,$baseProb);
my @baseOrders = split(//, $baseOrder);
my $lastBase = "";
# Derived from code from
# http://www.simgene.com/Oligo_Calc/OligoCalcObj.js
sub getTm {
my ($seq) = @_;
if(length($seq) == 0){
return (undef,undef);
}
$seq =~ tr/ //d;
$seq = uc($seq);
my $GCMin = ($seq =~ tr/CGS//);
my $GCMax = ($seq =~ tr/CGSYRMKVHDBN//);
if(length($seq) < 14){
return((2 * (length($seq)-$GCMin) + 4 * ($GCMin)),
(2 * (length($seq)-$GCMax) + 4 * ($GCMax)));
} else {
return((64.9 + 41 * (($GCMin - 16.4) / length($seq))),
(64.9 + 41 * (($GCMax - 16.4) / length($seq))));
}
}
sub getSATm {
my ($seq, $saltConc) = @_;
if(length($seq) == 0){
return (undef,undef);
}
$seq =~ tr/ //d;
$seq = uc($seq);
my $GCMin = ($seq =~ tr/CGS//);
my $GCMax = ($seq =~ tr/CGSYRMKVHDBN//);
my $fGCMin = ($GCMin / length($seq)) * 100;
my $fGCMax = ($GCMin / length($seq)) * 100;
if (length($seq) < 14) {
return((2 * (length($seq)-$GCMin) + 4 * ($GCMin)+
21.6+(7.21*log($saltConc/1000))),
(2 * (length($seq)-$GCMax) + 4 * ($GCMax)+
21.6+(7.21*log($saltConc/1000))));
}
else {
return((100.5 + (0.41*$fGCMin) - (820 / length($seq))+
(7.21*log($saltConc/1000))),
(100.5 + (0.41*$fGCMax) - (820 / length($seq))+
(7.21*log($saltConc/1000))));
}
}
sub seqsDifferent {
my ($seq1, $seq2, $diffThreshold) = @_;
my $differences = 0;
for(my $i = 0; $i < length($seq1); $i++){
if(substr($seq1,$i,1) ne substr($seq2,$i,1)){
$differences++;
}
}
my $diffProp = $differences / length($seq1);
return($diffProp >= $diffThreshold);
}
my @addedSeqs = ();
my $sequence = "";
my $seqNum = 0;
my $maxHPlength = 3;
while($seqNum < $count){
$sequence = "";
my $lastHP = (length($prefix) == 0) ? "C" : substr($prefix,-1,1);
for(my $i = 0; $i < $length; $i++){
my @nextBaseOptions = ((length($lastHP) < $maxHPlength) &&
($i < ($length-$maxHPlength+1))) ?
@baseOrders :
grep {$_ ne substr($lastHP,0,1)} @baseOrders;
my $prob = rand();
my $nextBase = $nextBaseOptions[$#nextBaseOptions];
my $probMultiplier = (scalar(@nextBaseOptions) == 4) ? 1 : (4/3);
if($prob < ($baseProbs[0] * $probMultiplier)){
$nextBase = $nextBaseOptions[0];
} elsif($prob < ($baseProbs[1] * $probMultiplier)){
$nextBase = $nextBaseOptions[1];
} elsif($prob < ($baseProbs[2] * $probMultiplier)){
$nextBase = $nextBaseOptions[2];
}
$sequence .= $nextBase;
if($nextBase eq substr($lastHP,0,1)){
$lastHP .= $nextBase;
} else {
$lastHP = $nextBase;
}
}
my $addedDiffCount =
grep {seqsDifferent($_,$sequence,$threshold)} @addedSeqs;
if(!@addedSeqs || ($addedDiffCount == scalar(@addedSeqs))){
$seqNum++;
my $fullSeq = $prefix.$sequence.$suffix;
printf(">Barcode_%03d", $seqNum);
if($showTm =~ /e/){
my @Tm = getTm($fullSeq);
my @SATm = getSATm($fullSeq,50);
printf(" [Whole Sequence Tm (%0.0f-%0.0f °C); ".
"(%0.0f-%0.0f °C) in 50mM Na+]",
$Tm[0],$Tm[1], $SATm[0], $SATm[1]);
}
if($showTm =~ /p/){
my @Tm = getTm($prefix);
my @SATm = getSATm($prefix,50);
printf(" [Prefix Tm (%0.0f-%0.0f °C); ".
"(%0.0f-%0.0f °C) in 50mM Na+]",
$Tm[0],$Tm[1], $SATm[0], $SATm[1]);
}
if($showTm =~ /s/){
my @Tm = getTm($suffix);
my @SATm = getSATm($suffix,50);
printf(" [Suffix Tm (%0.0f-%0.0f °C); ".
"(%0.0f-%0.0f °C) in 50mM Na+]",
$Tm[0],$Tm[1], $SATm[0], $SATm[1]);
}
if($showTm =~ /\+([0-9]+)/){
my $startDist = $1;
my @Tm = getTm(substr($fullSeq,0,$startDist));
my @SATm = getSATm(substr($fullSeq,0,$startDist),50);
printf(" [Last %d bases Tm (%0.0f-%0.0f °C); ".
"(%0.0f-%0.0f °C) in 50mM Na+]", $startDist,
$Tm[0],$Tm[1], $SATm[0], $SATm[1]);
}
if($showTm =~ /-([0-9]+)/){
my $endDist = $1;
my @Tm = getTm(substr($fullSeq,-$endDist));
my @SATm = getSATm(substr($fullSeq,-$endDist),50);
printf(" [Last %d bases Tm (%0.0f-%0.0f °C); ".
"(%0.0f-%0.0f °C) in 50mM Na+]", $endDist,
$Tm[0],$Tm[1], $SATm[0], $SATm[1]);
}
printf("\n%s\n", $fullSeq);
push(@addedSeqs, $sequence);
}
}