-
Notifications
You must be signed in to change notification settings - Fork 16
/
sort_sequences_by_taxon.pl
166 lines (136 loc) · 5.64 KB
/
sort_sequences_by_taxon.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
#!/usr/bin/env perl
#####################################
## Robin van der Lee ##
## robinvanderlee AT gmail DOT com ##
############################################################################################################
## Genome-scale detection of positive selection in 9 primates predicts human-virus evolutionary conflicts ##
## Robin van der Lee, Laurens Wiel, Teunis J.P. van Dam, Martijn A. Huynen ##
############################################################################################################
use warnings;
use strict;
use Bio::Seq;
use Bio::SeqIO;
use Bio::AlignIO;
require('functions.pl');
### command line arguments
# 1: infile
my $fasta_alignment_file = "";
if(scalar @ARGV == 0){
die "Please provide a fasta input file\n";
} else {
$fasta_alignment_file = shift @ARGV;
die "Please provide a fasta input file\n" if $fasta_alignment_file !~ /fa[s]?$/;
}
# 2: outfile
my $outfile = "";
if(scalar @ARGV == 0){
die "Please provide a fasta output file\n";
} else {
$outfile = shift @ARGV;
}
### read fasta file
my $alignment_object = undef;
eval { $alignment_object = readFastaFile($fasta_alignment_file) };
die if ($@);
### define species of interest
my %species_of_interest = (
9606 => "homo_sapiens",
9598 => "pan_troglodytes",
9595 => "gorilla_gorilla",
9601 => "pongo_abelii",
61853 => "nomascus_leucogenys",
9544 => "macaca_mulatta",
9555 => "papio_anubis",
60711 => "chlorocebus_sabaeus",
9483 => "callithrix_jacchus"
); # 9 Simian primates
my @species_of_interest_display_order = (9606, 9598, 9595, 9601, 61853, 9544, 9555, 60711, 9483);
### sort sequences within alignments by species, based on taxonomy
my @sequence_objects_alignment_ordered = order_sequences_by_taxon($alignment_object, \@species_of_interest_display_order);
### write alignment to file
# my $outfile = $fasta_alignment_file;
# $outfile =~ s/(\.aln\.fa)$/-species-sorted$1/;
my $seqout = Bio::SeqIO->new
(-file => ">$outfile",
-format => 'fasta');
$seqout->write_seq(@sequence_objects_alignment_ordered);
# print "File $outfile has been generated\n";
# print STDERR "\nFINISHED\n";
#########################
####### FUNCTIONS #######
#########################
###########################################################################
###########################################################################
# sub writeAlignment{
# my ($alignment_file, $alignment) = @_;
# my $AlignIO_out = Bio::AlignIO->new(-file => ">$alignment_file", #-fh => \*STDOUT ,
# -format => 'fasta');
# $AlignIO_out->write_aln($alignment);
# # $AlignIO_out->write_aln($alignment->slice(1,1000,1));
# }
sub order_sequences_by_taxon{
# first argument: alignment object
my $alignment_object = shift @_;
# second argument: array of taxonomy numbers
my $species_order_ref = shift @_;
my @species_order = @{$species_order_ref};
# printArray(@species_order);
# get array of Bio::Seq objects, with -accession_number set as taxon
my @sequence_objects_alignment = get_sequence_object_array_from_alignment_object($alignment_object);
# foreach(@sequence_objects_alignment){
# print $_->display_name . "\n";
# print $_->accession_number . "\n\n";
# }
my @seq_objects_ordered = ();
foreach my $species_order_taxon (@species_order){
foreach my $seq_object (@sequence_objects_alignment){
if($species_order_taxon == $seq_object->accession_number){ # taxon number is stored in accession_number!
push(@seq_objects_ordered, $seq_object);
}
}
}
return @seq_objects_ordered;
}
##########################################################################
sub get_sequence_object_array_from_alignment_object{
# first argument: alignment object
my $alignment_object = shift @_;
my @sequence_objects_alignment = ();
foreach my $alignment_sequence ($alignment_object->each_seq) {
my @F = split /\_/, $alignment_sequence->display_id;
my $alignment_sequence_taxon = $F[0];
my $sequence_object = Bio::Seq->new(
-seq => $alignment_sequence->seq,
-display_id => $alignment_sequence->display_id,
-accession_number => $alignment_sequence_taxon, # store taxon in accession_number
);
push(@sequence_objects_alignment, $sequence_object)
}
# returns: array of Bio::Seq objects, with -accession_number set as taxon
return @sequence_objects_alignment;
}
###########################################################################
sub readFastaFile{
my $file = shift @_;
#stop function if file doesn't exists
eval{ checkIfExists($file) };
die if($@);
#read alignment file
my $AlignIO_in = Bio::AlignIO->new(-file => $file ,
-format => 'fasta');
my $alignment = $AlignIO_in->next_aln(); # Returns : a Bio::Align::AlignI compliant object
$alignment->set_displayname_flat(); # Function : Makes all the sequences be displayed as just their name, not name/start-end
#throw error if file contains another alignment
my $alignment2 = $AlignIO_in->next_aln();
if(defined $alignment2){
die "$file contains multiple alignments";
}
return $alignment;
}
###########################################################################
sub checkIfExists{
my $file = shift @_;
if(! -f $file){
die "File $file does not exist";
}
}