#!/usr/local/bin/perl -w # esumprotgi.pl # from ncbi:epost.p; File Description: ePost/eSummary calling example =item about collect gi from blastp table output, top 5? hits/query, fetch esummary for all uniq gi's ; merge results w/ query ID, bitscore change to 1. collect all gi's from all blastp, 2. fetch gi summaries to table file, 3. merge w/ blast results query, .. set dp=dvir ; cat tdprotnear23/$dp-blnear23i-sno.nodup-ncbinr.0?.blastp | grep -v '^#' \ | sort -k1,1 -k12,12nr | perl esumprotgi.pl -blast stdin -nhit 3 \ > $dp-blnear23i-sno.nodup-ncbinr.esum =item blastp2sumtable .. add this blastp merge, tophit collect, and merge with query ids and bitscore cat tdprotnear/dgri-blnear23i-sno.nodup-ncbinr.0?.blastp | grep -v '^#' |\ sort -k1,1 -k12,12nr | perl -ne\ '($g,@v)=split; $b=$v[-1]; if($g eq $lg){ $ng++; $p=($ng<5 and $b > 0.9 * $lb)?1:0;} \ else { $ng=0; $p=1; }print if($p); ($lb,$lg)=($b,$g);' \ > dgri-blnear23i-sno.nodup-ncbinr.blastp cat tdprotnear/dgri-blnear23i-sno.nodup-ncbinr.blastp | \ perl -ne'print "$1\n" if m/\sgi.(\d+)/;' | sort | uniq\ > tdprotnear/dgri-blnear23i-sno.nodup-ncbinr.gi =cut # --------------------------------------------------------------------------- my $eutils_root = "http://www.ncbi.nlm.nih.gov/entrez/eutils"; my $ePost_url = "$eutils_root/epost.fcgi"; my $eSummary_url = "$eutils_root/esummary.fcgi"; my $db_name = "protein"; # fixme option; "PubMed"; # --------------------------------------------------------------------------- use strict; use LWP::UserAgent; use LWP::Simple; use HTTP::Request; use HTTP::Headers; use CGI; use Getopt::Long; sub debug { warn @_ if(0); } my($gilist,$blasttable); my $nhits= 5; my $optok= GetOptions( "blast=s", \$blasttable, "gilist=s", \$gilist, "nhits=i", \$nhits, "db_name=s", \$db_name, ); die "usage: esummary.pl -blast blasttable | -gi gilist options: -nhits=$nhits ; -db protein|nucleotide [$db_name] e.g. cat tdprotnear/dgri-blnear23i-sno.nodup-ncbinr.0?.blastp | grep -v '^#' | sort -k1,1 -k12,12nr \\ perl esumprotgi.pl -blast stdin | sort -k1,1 -k3,3 " unless($optok); # --------------------------------------------------------------------------- # Read input file into variable $file # File name - forst argument $ARGV[0] my $gistring=""; my %blasttab; if($blasttable) { # cat tdprotnear/dgri-blnear23i-sno.nodup-ncbinr.0?.blastp | grep -v '^#' |\ # sort -k1,1 -k12,12nr | perl -ne\ # '($g,@v)=split; $b=$v[-1]; if($g eq $lg){ $ng++; $p=($ng<5 and $b > 0.9 * $lb)?1:0;} \ # else { $ng=0; $p=1; }print if($p); ($lb,$lg)=($b,$g);' \ # > dgri-blnear23i-sno.nodup-ncbinr.blastp # # cat tdprotnear/dgri-blnear23i-sno.nodup-ncbinr.blastp | \ # perl -ne'print "$1\n" if m/\sgi.(\d+)/;' | sort | uniq\ # > tdprotnear/dgri-blnear23i-sno.nodup-ncbinr.gi my($inh, $stdin, $lqid,$lbits,$p,$ng)= ("") x 20; if($blasttable =~ /^stdin|-/) { $inh= *STDIN; $stdin=1; } else { open(IN,$blasttable) or die "error for blast in: $blasttable"; $inh=*IN; } while(<$inh>) { next unless(/^\w/); my($qid,$sid,@v)=split; my $bits=$v[-1]; if($qid eq $lqid){ $ng++; $p=($ng < $nhits and $bits > 0.8 * $lbits)?1:0;} else { $ng=0; $p=1; } if($p) { my($gi)= $sid =~ m/gi\|(\d+)/; $blasttab{$gi} .= join("\t",$qid,$bits,"hit$ng")."\n";#? dont need sid } ($lbits,$lqid)=($bits,$qid); } close($inh) unless($stdin); #? err $gistring= join ",", sort keys %blasttab; } else { $gilist= shift @ARGV unless($gilist); undef $/; #for load whole file open IF, $gilist || die "Can't open for read: $!\n"; $gistring = ; close IF; debug "Loaded file: [$gistring]\n"; # Prepare file - substitute all separators to comma $gistring =~ s/\s+/,/gs; # debug "Prepared file: [$gistring]\n"; } #Create CGI param line my $form_data = "db=$db_name&id=$gistring"; my $ngi= $gistring =~ tr/,/,/; $ngi++ if($gistring); warn "ncbi esummary db=$db_name, ngi=$ngi\n"; my $result= callEutil($db_name,$form_data); #------------ process result ------------------------- my %doc; my %gidocs; my @tags= qw(Gi Caption TaxId Title ); # Extra < dont need @doc{@tags}=""; foreach (split "\n", $result) { $doc{Id}=$1 if(m,(\w+),); if(m,]*>([^<]+),);} } if(m,,) { printdoc(); } # savedoc(); } if(%blasttab) { print "#".join("\t",qw(query bits hit ncbigi acc taxid title)),"\n"; foreach my $gi (sort keys %blasttab) { my $doc = $gidocs{$gi} || "nodoc"; my $blast= $blasttab{$gi} || "noblast"; my @blast= split "\n", $blast; map{ print "$_\t$doc\n"; } @blast; ## qid, bits, hitnum, Gi, Caption, TaxId, Title } } sub callEutil { my($db_name,$form_data)=@_; # Create HTTP request my $headers = new HTTP::Headers( Accept => "text/html, text/plain", Content_Type => "application/x-www-form-urlencoded" ); my $request = new HTTP::Request("POST", $ePost_url, $headers ); $request->content($form_data); # Create the user agent object my $ua = new LWP::UserAgent; $ua->agent("eugenes.org"); # send file to ePost by HTTP my $response = $ua->request($request); debug "Responce status message: [" . $response->message . "]\n"; debug "Responce content: [" . $response->content . "]\n"; # Parse response->content and extract QueryKey & WebEnv $response->content =~ m|(\d+).*(\S+)|s; my $QueryKey = $1; my $WebEnv = $2; debug "\nEXTRACTED:\nQueryKey = $QueryKey;\nWebEnv = $WebEnv\n\n"; # --------------------------------------------------------------------------- # Retrieve DocSum from eSummary by simple::get method and print it # debug "eSummary result: \n"; my $result= get("$eSummary_url?db=$db_name&query_key=$QueryKey&WebEnv=$WebEnv"); # debug "]\n"; # print $result; return $result; } sub savedoc { if($doc{Id}) { $gidocs{ $doc{Id} }= join("\t", @doc{@tags}); } @doc{@tags}=""; } sub printdoc { if(%blasttab) { savedoc(); } else { print join("\t", @doc{@tags}),"\n" if(%doc); } @doc{@tags}=""; } =item url example curl 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=protein&id=28800982,28628843' perl ~/desktop/epost.pl test.gi | perl ^^ keep these fields as table: Gi/Id, Caption, Title, Extra, TaxId .. see below =cut =item eSummaryResult 28800982 AAO47091 hemochromatosis [Homo sapiens] gi|28800982|gb|AAO47091.1|[28800982] 28800982 2003/03/03 2003/03/03 0 9606 268 live 28628843 AAO49381 erythroid associated factor [Homo sapiens] gi|28628843|gb|AAO49381.1|AF485325_1[28628843] 28628843 2003/03/02 2003/03/02 0 9606 102 live =cut