-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeLoci.pl
More file actions
executable file
·231 lines (197 loc) · 6.1 KB
/
makeLoci.pl
File metadata and controls
executable file
·231 lines (197 loc) · 6.1 KB
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
#!/usr/bin/perl -w
#
# makeLoci_haps.pl
#
# Make a file with locus information (instead of SNPs) for individuals
# This should be able to be used with IMa2 or Migrate with minor modifications
#
# October 2, 2013
# Liz Cooper
use strict;
use Data::Dumper;
# Get the sites list file and the consensus fasta file names from the command line
# Also get the name of the output file and the file with the individual names
my ($USAGE) = "\n$0 <sites.list> <consensus.fasta> <output.loci> <names.txt>
\tsites.list = The sites file created by getSitelist.pl
\tconsensus.fasta = The reference fasta file
\toutput.loci = The name of the output file to create
\tnames.txt = A file with 1 row per population, and a comma delimited list of names on each row\n\n";
unless (@ARGV) {
print $USAGE;
exit;
}
my ($sitesfile, $tagfile, $output, $namesfile) = @ARGV;
# Do an initial read through of the sites list to get all of the
# tags and the positions associated with them into an array
my %sites = ();
my %genotypes = ();
open (SITES, $sitesfile) || die "\nUnable to open the file $sitesfile!\n";
while (<SITES>) {
chomp $_;
my @info = split(/\t/, $_);
$info[0] =~ s/^JH//g;
if (exists $sites{$info[0]}) {
my @temp = @{$sites{$info[0]}};
push (@temp, $info[1]);
@{$sites{$info[0]}} = @temp;
my @populations = @{$genotypes{$info[0]}};
my @new_pops = ();
for (my $i = 0; $i < scalar @populations; $i++) {
my @individuals = split(/,/, $populations[$i]);
my @new_inds = ();
for (my $k = 0; $k < scalar @individuals; $k++) {
my $new_string = $individuals[$k] . substr($info[$i+2], $k, 1);
push (@new_inds, $new_string);
}
my $new_ind_string = join(",", @new_inds);
push (@new_pops, $new_ind_string);
}
@{$genotypes{$info[0]}} = @new_pops;
} else {
my @temp = ();
push (@temp, $info[1]);
@{$sites{$info[0]}} = @temp;
my @populations = @info[2..((scalar @info) -1)];
my @new_populations = ();
foreach my $p (@populations) {
my @new_p = split('', $p);
my $new_string = join(",", @new_p);
push (@new_populations, $new_string);
}
@{$genotypes{$info[0]}} = @new_populations;
}
}
close(SITES);
#print Dumper (\%genotypes);
# Save the names of the individuals in seperate arrays stored in a hash
my %names = ();
open (NAMES, $namesfile) || die "\nUnable to open the file $namesfile!\n";
my $pop_number = 0;
while (<NAMES>) {
chomp $_;
my @pop_array = split(/,/, $_);
@{$names{$pop_number}} = @pop_array;
$pop_number++;
}
close(NAMES);
# Read through the consensus file and save the tag sequence for every listed tag
my %tags = ();
open (FASTA, $tagfile) || die "\nUnable to open the file $tagfile!\n";
my $id = -1;
my $seq = '';
while (<FASTA>) {
chomp $_;
if ($_ =~ /^>/) {
my @info = split(/\|/, $_);
$info[0] =~ s/^>//;
unless (length $seq == 0) {
if (exists $sites{$id}) {
$tags{$id} = $seq;
}
}
$id = $info[0];
$seq = '';
} else {
$seq = $_;
}
}
if (exists $sites{$id}) {
$tags{$id} = $seq;
}
close(FASTA);
# Now open the output file for printing
open (OUT, ">$output") || die "\nUnable to open the file $output!\n";
# Start going through the list of positions in the sites array
my @site_keys = keys %sites;
my @sorted_keys = sort {$a <=> $b} @site_keys;
print scalar @sorted_keys, "\n";
foreach my $key (@sorted_keys) {
# Print out the locus name before printing the individuals
print OUT "Locus ", $key, "\n";
# Get the list of positions and the list of genotypes for this site
# Get the consensus sequence for the site
my @snp_positions = @{$sites{$key}};
my @ind_genotypes = @{$genotypes{$key}};
my $consensus = $tags{$key};
# Get a list of the reference bases at every SNP position
my @ref_bases = ();
foreach my $snp_pos (@snp_positions) {
my $ref = substr($consensus, ($snp_pos-1), 1);
push (@ref_bases, $ref);
}
# Now, go through every individual in each population, and print 2 DNA strings
my @populations = keys %names;
my @pop_sort = sort {$a <=> $b} @populations;
foreach my $population (@pop_sort) {
my @individuals = split(/,/, $ind_genotypes[$population]);
my @name_array = @{$names{$population}};
for (my $i = 0; $i < scalar @name_array; $i++) {
my $string1 = $consensus;
my $string2 = $consensus;
for (my $j = 0; $j < scalar (@snp_positions); $j++) {
my $new_base = substr($individuals[$i], $j, 1);
if ($new_base =~ /[MRWSYK]/) {
my $base2 = get_snp_bases($new_base, $ref_bases[$j]);
substr($string1, ($snp_positions[$j] - 1), 1) = $ref_bases[$j];
substr($string2, ($snp_positions[$j] - 1), 1) = $base2;
} else {
substr($string1, ($snp_positions[$j] - 1), 1) = $new_base;
substr($string2, ($snp_positions[$j] - 1), 1) = $new_base;
}
}
print OUT $name_array[$i], "_1", " ", " ", $string1, "\n";
print OUT $name_array[$i], "_2", " ", " ", $string2, "\n";
}
}
}
close(OUT);
exit;
##########################################################################
# Subroutine(s)
##########################################################################
sub get_snp_bases {
my ($het, $ref) = @_;
my $new = '';
if ($ref =~ /A/) {
if ($het =~ /M/) {
$new = 'C';
} elsif ($het =~ /R/) {
$new = 'G';
} elsif ($het =~ /W/) {
$new = 'T';
} else {
print "ERROR: HET CODE IS: $het and REF BASE IS: $ref!\n";
}
} elsif ($ref =~ /C/) {
if ($het =~ /M/) {
$new = 'A';
} elsif ($het =~ /S/) {
$new = 'G';
} elsif ($het =~ /Y/) {
$new = 'T';
} else {
print "ERROR: HET CODE IS: $het and REF BASE IS: $ref!\n";
}
} elsif ($ref =~ /G/) {
if ($het =~ /R/) {
$new = 'A';
} elsif ($het =~ /S/) {
$new = 'C';
} elsif ($het =~ /K/) {
$new = 'T';
} else {
print "ERROR: HET CODE IS: $het and REF BASE IS: $ref!\n";
}
} elsif ($ref =~ /T/) {
if ($het =~ /W/) {
$new = 'A';
} elsif ($het =~ /Y/) {
$new = 'C';
} elsif ($het =~ /K/) {
$new = 'G';
} else {
print "ERROR: HET CODE IS: $het and REF BASE IS: $ref!\n";
}
}
return($new);
}