-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path_check_matrix.pl
executable file
·187 lines (154 loc) · 5.16 KB
/
_check_matrix.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
#!/usr/bin/perl -w
use strict;
use Bio::SeqIO;
# Script to analyze DNA polymorphisms along pre-aligned cp genomes
# Produces data files to be used as tracks with circos
# Bruno Contreras Moreira (1,2)
#1) Estacion Experimental de Aula Dei-CSIC, Zaragoza, Spain
#2) Fundacion ARAID, Zaragoza, Spain
die "# usage: $0 <file with multiple alignment of DNA sequences in FASTA format> " if(!$ARGV[0]);
my $MATRIXFILE = $ARGV[0];
my $REFSTRAIN = 'ABR6'; # name of reference strain, must be contained in multiple alignment
my $WINDOW = 100; # window size for calculating properties along the sequence
my @tracks = qw( N SNPs parsimony_info indels );
my ($feat,$gene,$CDScoords,$strand,$featseq,$exonseq,$strains,$length);
my ($present,$missing,$Ns,$protseq,$IR,$CDSok,$fnalign,$faalign);
my ($gaps,$SNPs,$pars,$align_length,$missense,$ref_tracks);
print "# params: MATRIXFILE=$MATRIXFILE REFSTRAIN=$REFSTRAIN WINDOW=$WINDOW\n";
# calc MSAs and print stats
($Ns,$gaps,$SNPs,$pars,$ref_tracks,$strains,$align_length) =
check_DNA_msa_ref($MATRIXFILE,$REFSTRAIN,$WINDOW);
print "\n#strains\tlength\tN\tindels\tSNPs\tparsimony-info\n";
print "$strains\t$align_length\t$Ns\t$gaps\t$SNPs\t$pars\n\n";
# save tracks to txt files
my @sorted_bins = sort {$a<=>$b} keys(%{$ref_tracks->{'N'}});
foreach my $track (@tracks)
{
print "# saving track '$track'\n";
open(TRACK,">_track$track.txt");
foreach my $bin (@sorted_bins)
{
#1 0 9999 0.00567
#1 10000 19999 0.00578
printf TRACK ("1 %d %d %1.5f\n",
$bin,$bin+$WINDOW-1,$ref_tracks->{$track}{$bin}/$ref_tracks->{"t$track"} || 0.00000);
}
close(TRACK);
}
#################################################
#################################################
sub read_FASTA_file_matrix
{
# in aligned FASTA format
# returns a ref to a matrix representing the input aligned sequences, with column 0 containing the headers
# first valid index (first sequence) is '0'
my ( $infile, $user_strain ) = @_;
my (@FASTA,$name,$seq,$nt,$letter,$n_of_sequences);
my $length = 0;
my $user_strain_nb = -1;
$n_of_sequences = -1;
open(FASTA,"<$infile") || die "# read_FASTA_sequence_matrix: cannot read $infile $!:\n";
while(<FASTA>)
{
next if(/^$/);
next if(/^#/);
if(/^\>(.*?)[\n\r]/)
{
$n_of_sequences++; # first sequence ID is 0
$name = $1;
$nt = 0;
$FASTA[$n_of_sequences][$nt++] = $name;
if($name eq $user_strain){ $user_strain_nb = $n_of_sequences }
}
elsif($n_of_sequences>-1)
{
$_ =~ s/[\s|\n]//g;
$_ =~ s/\·/-/g;
foreach $letter (split(//,$_))
{
$FASTA[$n_of_sequences][$nt++] = uc($letter);
}
}
}
close(FASTA);
my ($l,%length,);
foreach $seq (0 .. $#FASTA)
{
$l = scalar(@{$FASTA[$seq]}); #print "$l\n";
$length{$l} = 1;
}
if(scalar(keys(%length)) > 1)
{
die "# read_FASTA_sequence_matrix: input aligned sequences have different length\n";
}
return (\@FASTA,$n_of_sequences+1,$l-1,$user_strain_nb);
}
# requires sequences in input file to be aligned
# calculates conservation of aligned DNA sequences, ignoring Ns,
# and returns tracks of values of $window size with $refstrain coords
sub check_DNA_msa_ref
{
my ($infileFastaMSA,$refstrain,$window,$verbose) = @_;
my ($fasta_ref,$N,$L,$refstrain_nb) = read_FASTA_file_matrix($infileFastaMSA,$refstrain);
$N--;
if(!defined($fasta_ref)){ die "# check_DNA_msa: failed parsing file $infileFastaMSA\n" }
else{ print "# check_DNA_msa_ref: $refstrain -> $refstrain_nb\n\n"; }
my ($cons_segment_length,$bin) = (0,0);
my ($r,$c,$letter,$pars,$obsbases,%freq,%tracks);
my ($refc,$tgaps,$tSNPs,$tpars,$tNs) = (-1,0,0,0,0);
$tracks{'N'}{$bin} = $tracks{'SNPs'}{$bin} = $tracks{'parsimony_info'}{$bin} = $tracks{'indels'}{$bin} = 0;
foreach $c (1 .. $L)
{
$pars=0;
$obsbases='';
$freq{'A'} = $freq{'C'} = $freq{'G'} = $freq{'T'} = $freq{'-'} = $freq{'N'} = 0;
foreach $r (0 .. $N)
{
$letter = $fasta_ref->[$r][$c];
$freq{$letter}++;
if($r == $refstrain_nb && grep(/$letter/,('A','C','G','T')))
{
$refc++; #print "$refc\n";
}
}
foreach $letter ('A','C','G','T')
{
next if($freq{$letter} == 0);
if($freq{$letter} > 1){ $pars++ }
$obsbases .= $letter;
}
# Ns
$tNs += $freq{'N'};
$tracks{'N'}{$bin} += $freq{'N'};
$tracks{'tN'} += $freq{'N'};
# SNPs
if(length($obsbases)>1)
{
$tSNPs++;
$tracks{'SNPs'}{$bin}++;
$tracks{'tSNPs'}++;
}
# parsimony positions (al menos 2 letras aparecen al menos 2 veces)
# http://www.megasoftware.net/mega4/WebHelp/glossary/rh_parsimony_informative_site.htm
if($pars > 1)
{
$tpars++;
$tracks{'parsimony_info'}{$bin}++;
$tracks{'tparsimony_info'}++;
}
# columns with gaps
if($freq{'-'} > 1)
{
$tgaps++;
$tracks{'indels'}{$bin}++;
$tracks{'tindels'}++;
}
# update bin
if($refc > 0 && ($refc % $window) == 0)
{
$bin += $window;
$tracks{'N'}{$bin} = $tracks{'SNPs'}{$bin} = $tracks{'parsimony_info'}{$bin} = $tracks{'indels'}{$bin} = 0;
}
}
return ($tNs,$tgaps,$tSNPs,$tpars,\%tracks,$N+1,$L);
}