-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_sequences.pl
259 lines (216 loc) · 7.22 KB
/
cluster_sequences.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env perl
# Clusters sequences by similarity.
# Usage:
# perl cluster_sequences.pl [sequences fasta file] [percent identity, for example 80]
# [file path of MAFFT executable file (mafft.bat) or mafft command]
# Prints to console. To print to file, use
# perl cluster_sequences.pl [sequences fasta file] [percent identity, for example 80]
# [file path of MAFFT executable file (mafft.bat) or mafft command] > [output table]
use strict;
use warnings;
my $sequences_fasta = $ARGV[0];
my $minimum_identity = $ARGV[1];
my $mafft_file_path_or_command = $ARGV[2];
my $TEMP_FILE_EXTENSION = "_temp.fasta";
my $ALIGNMENT_FILE_EXTENSION = "_aligned.fasta";
# verifies that input fasta exists
if(!$sequences_fasta or !-e $sequences_fasta or -z $sequences_fasta)
{
print STDERR "Error: input fasta not provided, does not exist, or empty:\n\t"
.$sequences_fasta."\n";
next;
}
# verifies that mafft executable exists or mafft command provided
if(!$mafft_file_path_or_command)
{
print STDERR "Error: mafft executable or command not provided:\n\t"
.$mafft_file_path_or_command."\n";
next;
}
# verifies that identity is >0
if(!$minimum_identity or $minimum_identity < 1)
{
print STDERR "Error: minimum identity not entered or is <1: ".$minimum_identity."\n";
next;
}
# reads in sequences
my %sequence_name_to_sequence = (); # key: sequence name -> value: sequence
my %sequence_name_to_length = (); # key: sequence name -> value: sequence length
open FASTA_FILE, "<$sequences_fasta" || die "Could not open $sequences_fasta to read; terminating =(\n";
my $sequence_name = "";
my $sequence = "";
while(<FASTA_FILE>) # for each line in the file
{
chomp;
if($_ =~ /^>(.*)$/) # header line
{
# processes previous sequence
if($sequence_name and $sequence)
{
$sequence_name_to_sequence{$sequence_name} = $sequence;
$sequence_name_to_length{$sequence_name} = sequence_length($sequence);
}
# stars processing this sequence
$sequence_name = $1;
$sequence = "";
}
else
{
$sequence .= $_;
}
}
close FASTA_FILE;
# processes final sequence
if($sequence_name and $sequence)
{
$sequence_name_to_sequence{$sequence_name} = $sequence;
$sequence_name_to_length{$sequence_name} = sequence_length($sequence);
}
# sort sequence names by their sequence lengths in descending order
my @sequence_names_sorted_by_sequence_length_descending = sort { $sequence_name_to_length{$b} <=> $sequence_name_to_length{$a} } keys %sequence_name_to_sequence;
# for each sequence, sorted from longest to shortest
my %cluster_id_to_name_of_longest_sequence = (); # key: cluster id -> value: sequence name of longest sequence in cluster
# my %cluster_id_to_sequences_list = (); # key: cluster id -> value: list of names of sequences in this cluster, as a string
my %sequence_name_to_cluster_id = (); # key: sequence name -> value: cluster sequence is assigned to
my $cluster_count = 0; # most recently created cluster
foreach my $sequence_name(@sequence_names_sorted_by_sequence_length_descending)
{
print STDERR "processing ".$sequence_name."...\n";
my $sequence = $sequence_name_to_sequence{$sequence_name};
my $sequence_assigned_to_cluster = 0;
# for each cluster:
foreach my $cluster_id(keys %cluster_id_to_name_of_longest_sequence)
{
if(!$sequence_assigned_to_cluster) # if sequence hasn't been assigned to a cluster:
{
my $name_of_longest_sequence_in_cluster = $cluster_id_to_name_of_longest_sequence{$cluster_id};
my $longest_sequence_in_cluster = $sequence_name_to_sequence{$name_of_longest_sequence_in_cluster};
# print sequence and longest cluster sequence to a file
my $temp_file = $sequences_fasta.$TEMP_FILE_EXTENSION;
open TEMP_FILE, ">$temp_file" || die "Could not open $temp_file to write; terminating =(\n";
print TEMP_FILE ">".$sequence_name."\n";
print TEMP_FILE $sequence."\n";
print TEMP_FILE ">".$name_of_longest_sequence_in_cluster."\n";
print TEMP_FILE $longest_sequence_in_cluster."\n";
close TEMP_FILE;
# align sequence to longest sequence in cluster
my $temp_file_aligned = $temp_file.$ALIGNMENT_FILE_EXTENSION;
`$mafft_file_path_or_command $temp_file > $temp_file_aligned`;
# read in aligned sequences
my $sequence_aligned = "";
my $longest_sequence_in_cluster_aligned = "";
open ALIGNMENT, "<$temp_file_aligned" || die "Could not open $temp_file_aligned to read; terminating =(\n";
my $current_sequence_name = "";
my $current_sequence = "";
while(<ALIGNMENT>) # for each line in the file
{
chomp;
if($_ =~ /^>(.*)$/) # header line
{
# processes previous sequence
if($current_sequence_name and $current_sequence)
{
$sequence_aligned = $current_sequence;
}
# stars processing this sequence
$current_sequence_name = $1;
$current_sequence = "";
}
else
{
$current_sequence .= $_;
}
}
close ALIGNMENT;
# processes final sequence
if($current_sequence_name and $current_sequence)
{
$longest_sequence_in_cluster_aligned = $current_sequence;
}
# count number bases of sequence matched by cluster sequence
my $number_bases_matched = 0;
for(my $base_index = 0; $base_index < length($longest_sequence_in_cluster_aligned); $base_index++)
{
my $sequence_base = substr($sequence_aligned, $base_index, 1);
my $longest_sequence_in_cluster_base = substr($longest_sequence_in_cluster_aligned, $base_index, 1);
if(is_unambiguous_base($sequence_base) and is_unambiguous_base($longest_sequence_in_cluster_base)
and $sequence_base eq $longest_sequence_in_cluster_base)
{
$number_bases_matched++;
}
}
# if proportion bases matched >80% of sequence length, add sequence to cluster and mark to stop comparing
if($number_bases_matched / $sequence_name_to_length{$sequence_name} * 100 >= $minimum_identity)
{
$sequence_assigned_to_cluster = 1;
$sequence_name_to_cluster_id{$sequence_name} = $cluster_id;
}
}
}
# if sequence hasn't been assigned to a cluster, create a new cluster with this sequence
if(!$sequence_assigned_to_cluster)
{
$cluster_count++;
$cluster_id_to_name_of_longest_sequence{$cluster_count} = $sequence_name;
$sequence_name_to_cluster_id{$sequence_name} = $cluster_count;
}
}
# for each sequence, print cluster id and sequence name, tab separated
foreach my $sequence_name(keys %sequence_name_to_cluster_id)
{
print $sequence_name_to_cluster_id{$sequence_name};
print "\t";
print $sequence_name;
print "\n";
}
# returns sequence length
sub sequence_length
{
my $sequence = $_[0];
# capitalize sequence
$sequence = uc($sequence);
# counts number bases or unambiguous bases in this sequence
my $sequence_length = 0;
foreach my $base(split //, $sequence)
{
if(is_base($base))
{
$sequence_length++;
}
}
return $sequence_length;
}
# returns 1 if base is A, T, C, G; returns 0 if not
# input base must be capitalized
sub is_unambiguous_base
{
my $base = $_[0]; # must be capitalized
if($base eq "A" or $base eq "T" or $base eq "C" or $base eq "G")
{
return 1;
}
return 0;
}
# returns 1 if base is not gap, 0 if base is a gap
sub is_base
{
my $base = $_[0];
# empty value
if(!$base)
{
return 0;
}
# only whitespace
if($base !~ /\S/)
{
return 0;
}
# gap
if($base eq "-")
{
return 0;
}
# base
return 1;
}
# July 19, 2024