-
Notifications
You must be signed in to change notification settings - Fork 129
/
clstr_merge.pl
executable file
·144 lines (126 loc) · 3.13 KB
/
clstr_merge.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
#!/usr/bin/env perl
# the order of clusters need to be identical
my ($master_clstr, @clstr) = @ARGV;
my $clstr_file_no = $#clstr+1;
my @fhs = ();
my @div_reps = ();
my @div_seqs = ();
my @div_rep_no = ();
for ($i=0; $i<$clstr_file_no; $i++) {
$fh = "FH" . $i;
open($fh, $clstr[$i]) || die "can not open $clstr[$i]";
$div_reps[$i] = "";
$div_seqs[$i] = "";
$div_rep_no[$i] = 0;
}
my $master_rep = "";
my $master_seq = "";
my $rep_no = 0;
open(TMP, $master_clstr) || die "can not open $master_clstr";
while($ll = <TMP>) {
if ($ll =~ /^>/) {
if ($master_rep) {
print $master_seq;
foreach ($i=0; $i<$clstr_file_no; $i++) {
$this_no = process_this($i, $master_rep, $rep_no);
$rep_no += $this_no;
}
}
$master_rep = "";
$master_seq = $ll;
$rep_no = 0;
}
else {
$master_seq .= $ll;
$rep_no++;
chop($ll);
if ($ll =~ /\*$/) {
$rep = "";
if ($ll =~ /(aa|nt), >(.+)\.\.\./) {
$rep = $2;
$master_rep = $rep;
}
else {
die "format error $ll";
}
}
}
}
if ($master_rep) {
print $master_seq;
foreach ($i=0; $i<$clstr_file_no; $i++) {
$this_no = process_this($i, $master_rep, $rep_no);
$rep_no += $this_no;
}
}
close(TMP);
for ($i=0; $i<$clstr_file_no; $i++) {
$fh = "FH" . $i;
close($fh);
}
sub process_this {
my ($i, $master_rep, $rep_no) = @_;
my $ll;
my ($j, $k);
$fh = "FH" . $i;
while($ll = <$fh>) {
if ($ll =~ /^>/) {
if ($div_reps[$i] eq $master_rep) {
if ($div_rep_no[$i] > 1) {
$j = $rep_no;
my @lls = split(/\n/,$div_seqs[$i]);
foreach $k (@lls) {
next if ($k =~ /\*$/);
$k =~ s/^\d+/$j/;
print $k, "\n";
$j++;
}
}
$div_reps[$i] = "";
$div_seqs[$i] = "";
my $t1 = $div_rep_no[$i];
$div_rep_no[$i] = 0;
return ($t1-1);
#return ($div_rep_no[$i]-1);
}
else {
$div_reps[$i] = "";
$div_seqs[$i] = "";
$div_rep_no[$i] = 0;
}
}
else {
$div_seqs[$i] .= $ll;
$div_rep_no[$i]++;
chop($ll);
if ($ll =~ /\*$/) {
my $rep = "";
if ($ll =~ /(aa|nt), >(.+)\.\.\./) {
$rep = $2;
$div_reps[$i] = $rep;
}
else {
die "format error $ll";
}
}
}
}
if ($div_reps[$i] eq $master_rep) {
if ($div_rep_no[$i] > 1) {
$j = $rep_no;
my @lls = split(/\n/,$div_seqs[$i]);
foreach $k (@lls) {
next if ($k =~ /\*$/);
$k =~ s/^\d+/$j/;
print $k, "\n";
$j++;
}
}
$div_reps[$i] = "";
$div_seqs[$i] = "";
my $t1 = $div_rep_no[$i];
$div_rep_no[$i] = 0;
return ($t1-1);
#return ($div_rep_no[$i]-1);
}
}