-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatch_extract_up_and_down.pl
152 lines (138 loc) · 4.15 KB
/
Batch_extract_up_and_down.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
#!/usr/bin/perl -w
use strict;
use warnings;
use File::Basename;
####################################step1
my @file1=glob "/share/nas1/honghh/personality/BMK180316-I997/BMK_5_DEG_Analysis/BMK_3_T*/BMK_1_*/*DEG.final.xls";
my @file2=glob "/share/nas1/honghh/personality/BMK180316-I997/Analysis_TF/DEG/*_vs_*.DEG.final.xls";
mkdir "result" unless(-d "result");
my $count=0;
for my $file1(@file1){
chomp $file1;
my $basename1=basename($file1);
#print $basename1,"\n";
for my $file2(@file2){
chomp $file2;
my $basename2=basename($file2);
#print $basename2,"\n";
if($basename2 =~ /$basename1/){
$count++;
print "$count\n";
open(IN1,$file1) or die $!;
open(IN2,$file2) or die $!;
open(OUT,">result/$basename2");
my %file=();
while(my $line=<IN2>){
chomp $line;
my ($gene_id,$left)=split /\s+/,$line,2;
$file{$gene_id}=$left;
}
while(<IN1>){
chomp;
my ($gene_id,$regulated)=(split /\s+/,$_)[0,-1];
for my $key(keys %file){
if($key=~/$gene_id/){
print OUT $key,"\t",$file{$key},"\t",$regulated,"\n";
}
}
}
}
close IN1;
close IN2;
close OUT;
}
}
print "result dir output finished!\n";
print "statistic starting...\n";
sleep(2);
#################################################################step2
my @file3=glob "/share/nas1/honghh/personality/BMK180316-I997/Analysis_TF/DEG/scripts/result/*";
while(my $line=<@file3>){
print $line,"\n";
}
my %statistic=();
mkdir "eventual_result" unless (-d "eventual_result");
for my $file_3(@file3){
chomp $file_3;
my $basename_3=basename($file_3);
print $basename_3,"\n";
open(IN,"$file_3") or die $!;
open(OUT,">eventual_result/$basename_3");
while(<IN>){
chomp;
my($tfname,$tfstyle,$regulated)=(split /\s+/,$_)[1,2,-1];
next if($tfstyle eq "TR");
$statistic{$regulated}{$tfstyle}{$tfname} +=1;
}
for my $key(sort {$b cmp $a} keys %statistic){
if($key eq "up"){
print OUT "UP statistic\n";
for my $sec_key(keys %{$statistic{$key}}){
for my $thir_key(keys %{$statistic{$key}{$sec_key}}){
print OUT $thir_key,"\t",$statistic{$key}{$sec_key}{$thir_key},"\t","up","\n";
}
}
}else{
print OUT "########################################\n";
print OUT "DOWN statistic\n";
for my $sec_key(keys %{$statistic{$key}}){
for my $thir_key(keys %{$statistic{$key}{$sec_key}}){
print OUT $thir_key,"\t",$statistic{$key}{$sec_key}{$thir_key},"\t","down","\n";
}
}
}
}
%statistic=();
close IN;
close OUT;
}
###########################################################step3
print "Due to draw, merge file start..\n";
print "Merge start...\n";
my @file4= glob "/share/nas1/honghh/personality/BMK180316-I997/Analysis_TF/DEG/scripts/eventual_result/*";
my %readdata=();
my %all=();
my $counttime=0;
mkdir "final_result" unless -d "final_result";
for my $file_4(@file4){
chomp$file_4;
$counttime++;
print $counttime,"\n";
my $basename_4=basename($file_4);
open(IN,"$file_4") or die $!;
open(OUT,">final_result/$basename_4");
while(<IN>){
chomp;
next if(/\#/);next if(/UP/);next if(/DOWN/);
my $mark=(split /\s+/,$_)[2];
if($mark eq "up"){
my ($name,$num)=(split /\s+/,$_);
$readdata{"up"}{$name}=$num;
$all{$name}++;
}else{
my($name,$num)=(split /\s+/,$_);
$readdata{"down"}{$name}=$num;
$all{$name}++;
}
}
close IN;
print OUT "TF_name","\t","Up","\t","Down","\n";
for my $key(sort{$all{$a}<=>$all{$b}} keys %all){
if(exists $readdata{"up"}{$key}){ #string "up" marked the up-regulation and the sec_key was recognized by exists
print OUT $key,"\t",$readdata{"up"}{$key},"\t";
}else{
print OUT $key,"\t","0","\t";
}
if(exists $readdata{"down"}{$key}){
print OUT $readdata{"down"}{$key},"\n";
}else{
print OUT "0","\n";
}
}
%readdata=(); #empty the hash
close OUT;
}
#############################################################################step4
print "All works finished, the result show below:\n";
my $cmd="ls /share/nas1/honghh/personality/BMK180316-I997/Analysis_TF/DEG/scripts/final_result";
system($cmd);