forked from Axolotl233/Simple_Script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blast.nr2go.pl
137 lines (130 loc) · 3.05 KB
/
Blast.nr2go.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
#! perl
use warnings;
use strict;
use Getopt::Long;
use File::Basename;
use Bio::SeqIO;
(my $blast,my $idmapping,my $max,my $out,my $i,my $c,my $pep,my $type);
GetOptions(
'blast=s' => \$blast,
'idmapping=s' => \$idmapping,
'max=s' => \$max,
'out=s' => \$out,
'ident=s' => \$i,
'cov=s' => \$c,
'pep=s' => \$pep,
'type=s' => \$type
);
$idmapping //= "/data/01/user112/database/nr/idmapping.tb";
$max //= 5;
$i //= 0.6;
$c //= 0.6;
$type //= "nr";
exit if (! $blast || ! $pep);
my %len = &load_fasta($pep);
my $name = basename $blast;
$out = $name.".$type.go.out";
my %blast = %{&blast_load($blast)};
die "zero blast\n" if scalar keys %blast == 0;
my %o;
open IN,'<',$idmapping or die "$!";
while(<IN>){
chomp;
my @l = split/\t/;
if($type eq "nr"){
if($l[3] ne ""){
next if $l[7] eq "";
if(exists $blast{$l[3]}){
(my $go = $l[7]) =~ s/\s+//;
my @gos = split/;/,$go;
for my $g (@{$blast{$l[3]}}){
push @{$o{$g}} , @gos;
}
}
}
if($l[-1] ne ""){
next if $l[7] eq "";
if(exists $blast{$l[-1]}){
(my $go = $l[7]) =~ s/\s+//;
my @gos = split/;/,$go;
for my $g (@{$blast{$l[-1]}}){
push @{$o{$g}} , @gos;
}
}
}
}elsif($type eq "swissprot"){
if($l[0] ne ""){
next if $l[7] eq "";
if(exists $blast{$l[0]}){
(my $go = $l[7]) =~ s/\s+//;
my @gos = split/;/,$go;
for my $g (@{$blast{$l[0]}}){
push @{$o{$g}} , @gos;
}
}
}
}
}
close IN;
open O,'>',"$out" or die "$!";
for my $k(sort {$a cmp $b} keys %o){
my $p = join"\t",@{$o{$k}};
print O "$k\t$p\n";
}
close O;
sub blast_load{
my $f = shift @_;
my %h;
open IN,'<', $f or die "$!";
while(<IN>){
chomp;
my @l = split/\t/;
if(exists $len{$l[0]}){
my $q_len = $len{$l[0]};
my $blst_i = $l[2]/100;
my $blst_c = ($l[7]-$l[6])/$q_len;
if($blst_i>=$i and $blst_c>=$c){
if($type eq "swissprot"){
$l[1] =~ s/(.*?)\..*/$1/;
}
push @{$h{$l[0]}} , [$l[1],$l[-1]];
}
}
}
close IN;
%h = &blast_filter(\%h,$max);
return \%h
}
sub blast_filter{
my $ref = shift @_;
my %h = %{$ref};
my %p;
for my $k (keys %h){
my @a = @{$h{$k}};
@a = sort{${$b}[1] <=> ${$a}[1]} @a;
my $num = scalar @a;
if($num >= $max){
for(my $i = 0;$i <= ($max-1);$i++){
my $n = @{$a[$i]}[0];
push @{$p{$n}} , $k;
}
}else{
for(my $i = 0;$i <= ($num-1);$i++){
my $n = @{$a[$i]}[0];
push @{$p{$n}} , $k;
}
}
}
return %p;
}
sub load_fasta{
my $pep = shift @_;
my %h;
my $s_obj = Bio::SeqIO -> new(-file => $pep);
while(my $s_io = $s_obj -> next_seq){
my $id =$s_io -> display_id;
my $len = $s_io -> length;
$h{$id} = $len;
}
return %h;
}