-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2_m3_2.pl
146 lines (139 loc) · 3.81 KB
/
m2_m3_2.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
#!/usr/bin/perl -w
#usage: perl m2_m3.pl <m2> <m3>
open FH1,"< $ARGV[0]";
open FH2,"> temp.txt";
my $line1 = <FH1>;
chomp $line1;
my @sample = split "\t",$line1;
pop @sample;
shift @sample;
print FH2 "",(join "\t",@sample),"\ttaxonomy\n";
while(<FH1>){
chomp;
my @temp = split "\t",$_;
shift @temp;
my $taxonomy = pop @temp;
my $abundance = join "\t",@temp;
my @temp2 = split "; ",$taxonomy;
if($taxonomy =~ m/^(k__.*?); (p__.*?); (c__.*?); (o__.*?); (f__.*?); (g__.*?); (s__.*?)$/){
my $k = $1;
my $p = "$1; $2";
my $c = "$1; $2; $3";
my $o = "$1; $2; $3; $4";
my $f = "$1; $2; $3; $4; $5";
my $g = "$1; $2; $3; $4; $5; $6";
my $s = "$1; $2; $3; $4; $5; $6; $7";
print FH2 "$abundance ","$k\n";
print FH2 "$abundance ","$p\n";
print FH2 "$abundance ","$c\n";
print FH2 "$abundance ","$o\n";
print FH2 "$abundance ","$f\n";
print FH2 "$abundance ","$g\n";
print FH2 "$abundance ","$s\n";
}
if($taxonomy =~ m/^(k__.*?); (p__.*?); (c__.*?); (o__.*?); (f__.*?); (g__.*?)$/ && ( ! ($taxonomy =~ m/s__/) ) ){
my $k = $1;
my $p = "$1; $2";
my $c = "$1; $2; $3";
my $o = "$1; $2; $3; $4";
my $f = "$1; $2; $3; $4; $5";
my $g = "$1; $2; $3; $4; $5; $6";
print FH2 "$abundance ","$k\n";
print FH2 "$abundance ","$p\n";
print FH2 "$abundance ","$c\n";
print FH2 "$abundance ","$o\n";
print FH2 "$abundance ","$f\n";
print FH2 "$abundance ","$g\n";
}
if($taxonomy =~ m/^(k__.*?); (p__.*?); (c__.*?); (o__.*?); (f__.*?)$/ && ( ! ($taxonomy =~ m/g__/) ) ){
my $k = $1;
my $p = "$1; $2";
my $c = "$1; $2; $3";
my $o = "$1; $2; $3; $4";
my $f = "$1; $2; $3; $4; $5";
print FH2 "$abundance ","$k\n";
print FH2 "$abundance ","$p\n";
print FH2 "$abundance ","$c\n";
print FH2 "$abundance ","$o\n";
print FH2 "$abundance ","$f\n";
}
if($taxonomy =~ m/^(k__.*?); (p__.*?); (c__.*?); (o__.*?)$/ && ( ! ($taxonomy =~ m/f__/) ) ){
my $k = $1;
my $p = "$1; $2";
my $c = "$1; $2; $3";
my $o = "$1; $2; $3; $4";
print FH2 "$abundance ","$k\n";
print FH2 "$abundance ","$p\n";
print FH2 "$abundance ","$c\n";
print FH2 "$abundance ","$o\n";
}
if($taxonomy =~ m/^(k__.*?); (p__.*?); (c__.*?)$/ && ( ! ($taxonomy =~ m/o__/) ) ){
my $k = $1;
my $p = "$1; $2";
my $c = "$1; $2; $3";
print FH2 "$abundance ","$k\n";
print FH2 "$abundance ","$p\n";
print FH2 "$abundance ","$c\n";
}
if($taxonomy =~ m/^(k__.*?); (p__.*?)$/ && ( ! ($taxonomy =~ m/c__/) ) ){
my $k = $1;
my $p = "$1; $2";
print FH2 "$abundance ","$k\n";
print FH2 "$abundance ","$p\n";
}
if($taxonomy =~ m/^(k__.*?)$/ && ( ! ($taxonomy =~ m/p__/) ) ){
my $k = $1;
print FH2 "$abundance ","$k\n";
}
}
close FH1;
close FH2;
open FH3,"< temp.txt";
my $head = <FH3>;
chomp $head;
my @temp3 = split "\t",$head;
pop @temp3;
my %hash;
while(<FH3>){
chomp;
my @temp = split "\t",$_;
my $taxonomy = pop @temp;
#print $temp3[0],"\n";
#print "$taxonomy\n";
foreach(0..$#temp3){
if(exists $hash{$taxonomy}{$temp3[$_]}){
#print "$temp[$_]\n";
$hash{$taxonomy}{$temp3[$_]} += $temp[$_];
}else{
$hash{$taxonomy}{$temp3[$_]} = $temp[$_];
}
}
}
close FH3;
open FH4,"> temp.txt";
my @sort_temp3 = sort @temp3;
print FH4 "OTU ID\t",(join "\t",@sort_temp3),"\ttaxonomy\n";
foreach my $taxonomy (sort keys %hash ){
my @temp;
foreach my $sample(sort @temp3){
push(@temp,$hash{$taxonomy}{$sample});
}
my $tax = (split "; ",$taxonomy)[-1];
print FH4 "$tax\t",(join "\t",@temp),"\t$taxonomy\n";
}
close FH4;
open FH5,"< temp.txt";
open FH6,"> $ARGV[1]";
print FH6 "",scalar <FH5>;
while(<FH5>){
chomp;
my @temp = split "\t",$_;
my $index = $#temp;
my $taxonomy_short = (split "; ",$temp[0])[-1];
my @temp3 = @temp[1..$index];
unshift(@temp3,$taxonomy_short);
print FH6 "",(join "\t",@temp3),"\n";
}
close FH5;
close FH6;
system("rm temp.txt");