-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncbiQuery.pl
141 lines (111 loc) · 3.46 KB
/
ncbiQuery.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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
use LWP::Simple;
use XML::Simple;
my %opts;
getopts('f:',\%opts);
my $scriptName;
if ($0 =~ /\//){
$0 =~ /\/.*\/(.+)$/;
$scriptName = $1;
}
else {
$scriptName = $0;
}
&varcheck;
print "query\torganism\tregionNote\tproteinProduct\tcdsNote\t%ident\talignLen\teValue\n";
open (BL,$opts{'f'}) or die "Can't open blast file $opts{'f'}\n";
my $pauseCount = 1;
my $loopCount = 1;
while (my $line = <BL>) {
print STDERR $loopCount++."\r";
next if $line =~/^\s*#/;
chomp $line;
my ($query,$hit,$ident,$alignLen,$eValue) = (split (/\t/,$line))[0,1,2,3,10];
my ($gi,$accession) = (split(/\|/,$hit))[1,3];
my $fetchDat = get("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=protein&id=$gi&rettype=gp&retmode=xml");
#print $fetchDat;
my ($cdsNote,$proteinProd,$regionNote) = ("","","");
my $xmlParser = new XML::Simple;
my $xml = $xmlParser->XMLin($fetchDat);
my @features = @{$xml->{'GBSeq'}->{'GBSeq_feature-table'}->{'GBFeature'}};
foreach my $featureHash (@features){
my @featureQuals;
if ($featureHash->{'GBFeature_key'} =~ /CDS/i){
if ($featureHash->{'GBFeature_quals'}->{'GBQualifier'}){
if (ref($featureHash->{'GBFeature_quals'}->{'GBQualifier'}) eq "HASH"){
my @quals = keys(%{$featureHash->{'GBFeature_quals'}->{'GBQualifier'}});
$cdsNote = "ERROR-Too many qual types:".join(":",@quals);
}
else {
foreach my $fQuals (@{$featureHash->{'GBFeature_quals'}->{'GBQualifier'}}){
if($fQuals->{'GBQualifier_name'} =~ /note/){
$cdsNote = $fQuals->{'GBQualifier_value'};
}
}
}
}
}
if ($featureHash->{'GBFeature_key'} =~ /region/i){
if ($featureHash->{'GBFeature_quals'}->{'GBQualifier'}){
if (ref($featureHash->{'GBFeature_quals'}->{'GBQualifier'}) eq "HASH"){
my @quals = keys(%{$featureHash->{'GBFeature_quals'}->{'GBQualifier'}});
$regionNote = "ERROR-Too many qual types:".join(":",@quals);
}
else {
foreach my $fQuals (@{$featureHash->{'GBFeature_quals'}->{'GBQualifier'}}){
if($fQuals->{'GBQualifier_name'} =~ /note/){
$regionNote = $fQuals->{'GBQualifier_value'};
}
}
}
}
}
if ($featureHash->{'GBFeature_key'} =~ /protein/i){
if ($featureHash->{'GBFeature_quals'}->{'GBQualifier'}){
if (ref($featureHash->{'GBFeature_quals'}->{'GBQualifier'}) eq "HASH"){
my @quals = keys(%{$featureHash->{'GBFeature_quals'}->{'GBQualifier'}});
$proteinProd = "ERROR-Too many qual types:".join(":",@quals);
}
else {
foreach my $fQuals (@{$featureHash->{'GBFeature_quals'}->{'GBQualifier'}}){
if($fQuals->{'GBQualifier_name'} =~ /product/){
$proteinProd = $fQuals->{'GBQualifier_value'};
}
}
}
}
}
}
my $organism = $xml->{'GBSeq'}->{'GBSeq_source'};
#my $definition = $xml->{'GBSeq'}->{'GBSeq_definition'};
print "$query\t$organism\t$regionNote\t$proteinProd\t$cdsNote\t$ident\t$alignLen\t$eValue\n";
if ($pauseCount++ > 2){
sleep(1);
$pauseCount = 1;
}
#print $fetchDat."\n";
}
sub varcheck {
my $errors = "";
if (!$opts{'f'}){
$errors .= "-f flag not provided\n";
}
elsif(!(-e $opts{'f'})) {
$errors .= "Can't open $opts{'f'}\n";
}
if ($errors ne "") {
print "\n$errors";
&usage;
}
}
sub usage{
print "\nusage: perl $scriptName <-f blastOut>\n";
print <<PRINTTHIS;
This program fetches additional information from NCBI based on gi number.
-f File containing tabular blast output (-m 8)
PRINTTHIS
exit;
}