-
Notifications
You must be signed in to change notification settings - Fork 0
/
zblocks.pl
98 lines (87 loc) · 2.22 KB
/
zblocks.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
#!/usr/bin/perl
# Retira todos os sítios nas extreminades do alinhamento fasta sequencial dado em $ARGV[0] que possuam uma proporção de gaps superior a $ARGV[1] (dado em %), além dos sítios no interior do alinhamento que possuam um npumero de non-gap characters inferior a $ARGV[2]
use strict;
use warnings;
my $data = $ARGV[0];
open IN, "<$data" or die("Could not open fasta alignment.");
if(!defined($ARGV[1])){
die("Please, specify trimming threshold (max % of gaps at edge sites).")
}
if(!defined($ARGV[2])){
die("Please, specify gap threshold (min number of non-gap characters at each 'middle' site).")
}
#parseia alinhamento
my @headers;
my @bases = [];
my @align;
foreach my $line(<IN>){
chomp($line);
if ($line =~ />.+/){
push(@headers,$line);
}
else{
@bases = split("",$line);
@align = (@align,[@bases]);
}
}
close IN;
#define limite final
my $trimthresh = ($ARGV[1]/100)*scalar(@align);
my $gapthresh = $ARGV[2];
my $endcut;
for(my $i=scalar(@bases)-1;$i>=0;$i--){
my $gap = 0;
for(my $j=0;$j<scalar(@align);$j++){
if($align[$j][$i] eq "-"){
$gap++;
}
}
if($gap<=$trimthresh){
$endcut=$i;
last;
}
}
#define limite inicial e quais sítios serão selecionados
my $startcut;
my @slctsites;
my $mid = 0;
my $blkbrk = 1;
my $inblock = 0;
my $gappymidsites = 0;
for(my $i=0;$i<=$endcut;$i++){
my $gap = 0;
for(my $j=0;$j<scalar(@align);$j++){
if($align[$j][$i] eq "-"){
$gap++;
}
}
if($mid==0 && $gap<=$trimthresh){
$startcut = $i;
$mid = 1;
}
if($mid==1){
if((scalar(@align)-$gap)>=$gapthresh){
push(@slctsites,$i);
if($blkbrk==1){
$blkbrk=0;
$inblock++;
}
}
else{
$gappymidsites++;
$blkbrk=1;
}
}
}
#reescreve alinhamento com os sitios selecionados
open OUT, ">$data-zb";
for(my $i=0;$i<scalar(@align);$i++){
print OUT $headers[$i]."\n";
foreach(@slctsites){
print OUT $align[$i][$_];
}
print OUT "\n";
}
my $slice = eval sprintf("%.2f",((scalar(@slctsites)/scalar(@bases))*100));
print "\n$data\n\n".scalar(@slctsites)." sites ($slice% in $inblock block(s)) selected out of ".scalar(@bases)." original sites:\n\t-".($startcut)." sites trimmed left\n\t-$gappymidsites middle 'gappy sites' deleted\n\t-".(scalar(@bases)-($endcut+1))." sites trimmed right\n\n";
exit;