forked from stanleyouth/-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m4_2_cns.pl
152 lines (141 loc) · 2.9 KB
/
m4_2_cns.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
#!/perl -w
use strict;
use Getopt::Long;
use Data::Dumper;
my ($m4,$ext_length);
GetOptions(
'm4=s'=>\$m4,
'extend_len=s'=>\$ext_length,
);
open IN,$m4 or die $!;
$/="\n\n\n\n";<IN>;<IN>;<IN>;$/="\n";
while(<IN>){
chomp;
my $line=$_;
if($line=~/^$/){
$/="\n\n\n\n";
<IN>;
$/="\n";
next;
}
my $query=$line;
$/="\n\n";
my $db=<IN>;
chomp $db;
$/="\n";
# print "$query\n$db\n";exit;
my $cns=&find_indel($query,$db,$ext_length);
print $cns."\n";
next;
}
sub find_indel{
my ($query,$db,$ext_l)=@_;
$query=~s/\s+\S+$//;
my @db_raw=split("\n",$db);
# print Dumper @db_raw;
my @db;
foreach my $db_line(@db_raw){
$db_line=~s/\s+\S+$//;
push @db,$db_line;
}
my %posi;
my $count=0;
# my @all;
# push @all,(@db,$query);
# print Dumper @all;
for my $find_dash_line(($query,@db)){
$count++;
while($find_dash_line=~/-/g){
my $posi=pos($find_dash_line);
# print $posi."\n";
push @{$posi{$count}},$posi-1;
}
}
# print Dumper %posi;
# print "\n\n\n";
my $posi_p=\%posi;
## break point
my %region=&merge($ext_l,$posi_p);
my $region=\%region;
my $db_2=\@db;
my $cns_seq=&cns($region,$query,$db_2);
return $cns_seq;
}
sub merge{
my ($ext,$position)=@_;
my %posi=%{$position};
# print Dumper %posi;
my %groups;
my @a;
for my $key( keys %posi){
my @loc=@{$posi{$key}};
my @loc_s=sort{$a<=>$b}@loc;
push @a,@loc_s;
}
@a=sort{$a<=>$b}@a;
# print Dumper @a;
my @group_b;
push @group_b,0;
#print $#a;
for (my $i=0;$i<$#a;$i++){
if($a[$i+1]-$a[$i]<=$ext){
next;
}else{
push @group_b,$i+1;
}
}
# print Dumper @group_b;
push @group_b,$#a+1;
# print Dumper @group_b;
my $boundry_count=0;
for(my $i=0;$i<$#group_b;$i++){
$boundry_count++;
push @{$groups{$boundry_count}},($a[$group_b[$i]],$a[$group_b[$i+1]-1]);
}
# print Dumper %groups;
# print"\n\n\n";
return %groups;
}
sub cns{
my ($region,$query,$db)=@_;
my %region=%{$region};
my %cns_to_query;
my @db=@{$db};
my @key=keys %region;
@key=sort{$a<=>$b}@key;
# print Dumper @key;
# print Dumper %region;
for my $key( @key ){
my @b=@{$region{$key}};
my @polymo;
for my $line(@db){
my $Localseq=substr($line,$b[0],$b[1]-$b[0]+1);
$Localseq=~s/\s//g;
push @polymo,$Localseq if length($Localseq)>=1;
}
my %freq;
for my $line_2(@polymo){
$line_2=~s/-//g;
$line_2="Del" if ($line_2 eq "");
$freq{$line_2}++;
}
# print Dumper %freq;
my @freq_s=sort { $freq{$b} <=> $freq{$a} } keys %freq;
# print Dumper @freq_s;
push @{$cns_to_query{$key}},(@b,$freq_s[0]);
# print $freq{$freq_s[0]}."\n\n";
}
my $start=0;
my $cns;
# print Dumper %cns_to_query;
for my $key_2( @key ){
$cns.=substr($query,$start,$cns_to_query{$key_2}[0]-$start);
$cns_to_query{$key_2}[2]="" if($cns_to_query{$key_2}[2] eq "Del");
# print $cns_to_query{$key_2}[2]."\n\n";
$cns.=$cns_to_query{$key_2}[2];
$start=$cns_to_query{$key_2}[1]+1;
}
$cns.=substr($query,$start,);
$cns=~s/^\S+\s+\S+\s+//;
return $cns;
}