-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHapMaker.pl
executable file
·205 lines (186 loc) · 4.77 KB
/
HapMaker.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
#!/usr/bin/perl
# HapMaker.pl
# 23 May 2013 Nozomu Okuda
use strict;
# use warnings;
use Bio::SeqIO;
use Bio::Seq;
my $transWt = 0.5;
my $indelVsnp = 200/7200;
my $indelLength = 99;
usage() if(@ARGV != 5);
my $infile = shift;
my $in = Bio::SeqIO->new(-file => $infile, -format => 'fasta')->next_seq()->seq();
open(my $noChangeFh, '<'.shift) or die "Could not open no-change file: $!";
my $divRate = shift;
my $outname = shift;
my $out = Bio::SeqIO->new(-file => '>'.$outname.'.fa', -format => 'fasta');
open(my $locs, '>'.shift) or die "Could not write out locs file: $!";
print "##### Making variant from $infile with ", $divRate*100, "% heterozygosity\n";
# parse no change file and store values
my @noChange = ();
while(<$noChangeFh>) {
my $curLine = $_;
chomp $curLine;
next if length $curLine <= 0;
my @tmpArr = split('-', $curLine);
push(@noChange, [$tmpArr[0], $tmpArr[1]]);
}
if(@noChange) {
@noChange = sort({$a->[0] <=> $b->[0]} @noChange);
}
# set up array representation of new haplotype
my $totalBases = length($in);
my @newHap = ();
for(my $i = 1; $i <= $totalBases; $i++) {
push(@newHap, $i);
}
# undef the untouchable areas
for my $curArr (@noChange) {
my $startNoChange = $curArr->[0];
my $endNoChange = $curArr->[1];
for(my $i = $startNoChange - 1; $i < $endNoChange; $i++) {
$newHap[$i] = -1;
}
}
# introduce variation
my $totalChanges = int($totalBases * $divRate);
my $changeCounter = $totalChanges;
while($changeCounter > 0) {
if(rand() < $indelVsnp) {
introduceIndel();
} else {
introduceSnp();
}
}
print "Variation generation complete\n";
my $refPos = 1;
my $finalChange = 0;
my $complete = 0;
my $outHap = '';
print "Building variant\n";
my $newHapCount = scalar @newHap;
for my $curSpot (@newHap) {
my $locsInfo = "$refPos\n";
if($curSpot =~ /^INS/) {
$outHap .= substr($in, $refPos - 1, 1);
my $insLen = (split(' ', $curSpot))[1];
for(my $i = 0; $i < $insLen; $i++) {
$locsInfo .= "--\n";
$outHap .= generateBase();
}
$finalChange = $refPos;
} elsif($curSpot =~ /^DEL/) {
$locsInfo = "";
$finalChange = $refPos;
} elsif($curSpot =~ /^SNP/) {
$finalChange = $refPos;
my $old = substr($in, $refPos - 1, 1);
my $new = snpify($old);
$outHap .= $new;
$locsInfo = "$refPos\t$old->$new\n";
} else {
$outHap .= substr($in, $refPos - 1, 1);
}
print $locs $locsInfo;
$refPos++;
my $percDone = $refPos * 100 / $newHapCount;
if($percDone >= $complete) {
print $complete, "% complete\n";
$complete = int($percDone + 1);
}
}
print "##### Finished generating variant from $infile #####\n";
$out->write_seq(Bio::Seq->new(-seq => $outHap,
-id => $outname));
my $percChange = $totalChanges - $changeCounter;
$percChange *= 100;
$percChange /= $totalBases;
print $percChange, "% of genome changed\n";
print "$finalChange of ", $totalBases, " was the last nucleotide to be changed.\n";
close($locs);
##### SUBROUTINES #####
sub usage {
print "USAGE: HapMaker.pl [reference haplotype] [no-change file] [divergence rate] ",
"[output haplotype name] [locs filename]\n";
exit;
}
# both introduce subroutines must introduce their respective variations to the new haplotype
sub introduceIndel {
my $stretch = rand() < 0.291 ? 1 : int(rand($indelLength-1)) + 2;
# Mills et al. (2006) suggest that 29.1% of indels are single base
$changeCounter -= $stretch;
if(rand() < 0.5) { # insertion
while(1) {
my $curPos = int(rand($totalBases));
next if($curPos + 1 != $newHap[$curPos]);
$newHap[$curPos] = "INS $stretch";
last;
}
} else { # deletion
my $limit = $totalBases - $stretch + 1;
while(1) {
my $curPos = int(rand($limit));
next if($curPos + 1 != $newHap[$curPos]);
for(my $i = 0; $i < $stretch; $i++) {
$newHap[$curPos] = "DEL";
$curPos++;
}
last;
}
}
}
sub introduceSnp {
while(1) {
my $curPos = int(rand($totalBases));
next if($curPos + 1 != $newHap[$curPos]);
$newHap[$curPos] = "SNP";
last;
}
$changeCounter--;
}
sub snpify {
my $curBase = uc(shift);
if(rand() < $transWt) {
return transition($curBase);
} else {
return transvert($curBase);
}
}
sub transition {
my $curBase = shift;
if($curBase eq 'A') {
return 'G';
} elsif($curBase eq 'C') {
return 'T';
} elsif($curBase eq 'G') {
return 'A';
} elsif($curBase eq 'T') {
return 'T';
}
return generateBase(); # N case
}
sub transvert {
my $curBase = shift;
if($curBase eq 'A') {
return rand() < 0.5 ? 'T' : 'C';
} elsif($curBase eq 'C') {
return rand() < 0.5 ? 'A' : 'G';
} elsif($curBase eq 'G') {
return rand() < 0.5 ? 'T' : 'C';
} elsif($curBase eq 'T') {
return rand() < 0.5 ? 'A' : 'G';
}
return generateBase();
}
sub generateBase {
my $curRoll = int(rand(4));
if($curRoll == 0) {
return 'A';
} elsif($curRoll == 1) {
return 'C';
} elsif($curRoll == 2) {
return 'G';
}
return 'T';
}