Supply TEMPLATE and SUFFIX for temporary query sequence files.
[bioperl-run.git] / t / SoapEU-function.t
blobc0999487169e63412fd6c67332b1f1c088255724
1 #-*-perl-*-
2 #$Id$
3 #testing SoapEUtilities with network queries
4 # idea: reproduce the examples at
5 # http://www.bioperl.org/wiki/HOWTO:EUtilities_Cookbook
7 use strict;
8 use warnings;
9 our $home;
10 BEGIN {
11     use Bio::Root::Test;
12     use lib '.';
13     $home = '.'; # set to '.' for Build use,
14                       # '..' for debugging from .t file
15     unshift @INC, $home;
16     test_begin(-tests => 119,
17                -requires_modules => [qw(Bio::DB::ESoap
18                                         Bio::DB::ESoap::WSDL
19                                         Bio::DB::SoapEUtilities
20                                         Bio::DB::SoapEUtilities::Result
21                                         Bio::DB::SoapEUtilities::FetchAdaptor
22                                         Bio::DB::SoapEUtilities::LinkAdaptor
23                                         Bio::DB::SoapEUtilities::DocSumAdaptor
24                                         SOAP::Lite
25                                         XML::Twig
26                                         )],
27            -requires_email      => 1);
30 my $email = test_email();
32 diag("Using $email for tests") if test_debug();
34 my ($fac, $result, $seqio, $seq, $i);
35 my @prot_ids = qw(1621261 89318838 68536103 20807972 730439);
36 my @prot_ids_2 = qw(828392 790 470338);
37 my @accns = qw(CAB02640 EAS10332 YP_250808 NP_623143 P41007);
38 my $lg_contig = 27479347;
39 my $sciname_id = 527031;
40 my @gene_ids = qw(828392 790 470338);
41 my $nbr_test_id = 1621261;
42 my @linkout_test_ids = qw(28864546 53828898 14523048 14336674 1817575);
44 SKIP : {
45     test_skip(-tests => 119,
46               -requires_networking => 1);
47 ok $fac = Bio::DB::SoapEUtilities->new(-verbose => test_debug), "SoapEUtilities factory";
49 diag("Retrieve raw data records from GenBank, save raw data to file, then parse via Bio::SeqIO");
51 ok $result = $fac->efetch(-email =>$email, -db => 'protein', -id => \@prot_ids_2 )->run(-no_parse=>1), "run efetch, no parse methods";
53 ok $seqio = Bio::DB::SoapEUtilities::FetchAdaptor->new(-result=>$result), "create adaptor";
54 for ($i=0; $seq = $seqio->next_seq; $i++) {
55      isa_ok($seq, 'Bio::Seq::RichSeq');
56      ok $seq->id, "primary_id present";
57 1;}
58 is $i, 3, "iterated all seq objs";
59 ok $result = $fac->efetch( -rettype => 'fasta' )->run(-no_parse=>1), "run efetch, fasta return, no parse methods";
60 ok $seqio = Bio::DB::SoapEUtilities::FetchAdaptor->new(-result=>$result), "create adaptor";
61 for ($i=0; $seq = $seqio->next_seq; $i++) {
62      isa_ok($seq, 'Bio::Seq');
63      ok $seq->id, "primary_id present";
64 1;}
66 diag("Get accessions (actually accession.versions) for a list of GenBank IDs (GIs)");
67 # SOAP server doesn't seem to like 'acc' as a rettype, get in fasta format
68 # and use the result accessors
69 ok $fac->efetch->set_parameters(-db=>'protein',
70                                 -id=>\@prot_ids,
71                                 -rettype => 'fasta' ), "set rettype = fasta";
72 ok $result = $fac->efetch->run, "run query with methods parsing";
73 is scalar @{$result->TSeqSet_TSeq_TSeqAccver}, 5, "retrieved all accns via \$result->TSeqSet_TSeq_TSeqAccver";
74 diag("Get GIs for a list of accessions");
75 ok $fac->efetch->set_parameters( -id=>\@accns ), "set -id to accn list";
76 ok $result = $fac->efetch->run, "run query with methods parsing";
77 is scalar @{$result->TSeqSet_TSeq_TSeqGi}, 5, "retrieved all GIs via \$result->TSeqSet_TSeq_TSeqGi";
79 diag("Downloading a large contig");
80 ok $fac->efetch->reset_parameters( -db => 'nucleotide',
81                                    -email =>$email,
82                                    -id => $lg_contig), "set parms for lg contig example";
83 ok $seqio = $fac->efetch->run( -auto_adapt => 1 ), "run with auto_adapt";
85 ok $seq = $seqio->next_seq, "iterate adaptor";
86 ok !$seq->seq, "no sequence present (contig only)";
87 TODO: {
88     local $TODO = "why not work with gbwithparts?";
89     ok $fac->efetch->set_parameters( -rettype=>'gbwithparts' ), "rettype = gbwithparts";
90     dies_ok { $seqio = $fac->efetch->run( -auto_adapt => 1 ) };
91     diag("run with auto_adapt (dies not really ok...)");
93     ok $seq = $seqio->next_seq, "iterate adaptor";
94     if ($seq) { ok $seq->seq, "sequence now present"; } else {ok 1;}
97 diag("Get the scientific name for an organism");
98 ok $fac->efetch->reset_parameters( -email =>$email,
99                                   -db=>'taxonomy',
100                                   -id=>[$sciname_id, $sciname_id+1] ), "set params for sciname test";
101 ok my $spio = $fac->efetch->run(-auto_adapt => 1), "run with autoadapt";
102 ok my $sp = $spio->next_species;
103 like $sp->scientific_name, qr/Bacillus thuringiensis/, "sciname";
104 is ($sciname_id, $sp->ncbi_taxid, "taxid retrieved and correct");
106 ok $fac->esummary( -email =>$email, -db => 'taxonomy', -id => $sciname_id ), "set esummary parms";
107 ok my $docs = $fac->run(-auto_adapt=>1), "run with autoadapt";
108 ok my $doc = $docs->next_docsum, "iterate adaptor";
109 like $doc->ScientificName, qr/Bacillus thuringiensis/, "sciname";
110 is ($sciname_id, $doc->TaxId, "taxid retrieved and correct");
112 diag("Simple database query");
113 ok $fac->esearch( -email =>$email, -db=>'protein', -term=> 'BRCA and human', -usehistory=>1 );
114 ok $result = $fac->run, "run with method parsing";
115 is $result->QueryTranslation,
116    'BRCA[All Fields] AND ("Homo sapiens"[Organism] OR human[All Fields])',
117 "query translation";
118 cmp_ok $result->count, ">=", 73, "result count";
119 ok $fac->esearch->reset_parameters(-email =>$email,
120                                    -WebEnv => $result->webenv,
121                                    -QueryKey => $result->query_key,
122                                    -RetMax => 100 );
123 ok my $wresult = $fac->esearch->run, "run web environment query with retmax set";
124 cmp_ok $wresult->count, ">=", 73, "all ids retrieved";
126 diag("What databases are available for querying via eUtils?");
127 ok $result = $fac->einfo(-email =>$email)->run, "run einfo general query";
128 cmp_ok scalar(@{$result->dbs}), ">=", 42, "bunch o' dbs";
130 diag("What information is available for database 'x'?");
131 ok $result = $fac->einfo(-email =>$email,-db=>'pubmed')->run, "run pubmed info";
132 is $result->db, 'pubmed', "dbname";
133 cmp_ok $result->record_count, ">=", 19000000, "record count";
135 diag("How do I run a global query against all Entrez databases?");
137 ok my $queries = $fac->egquery(-email =>$email, -term => 'BRCA and human' )->run(-auto_adapt=>1), "run fac for eGQuery, autoadapt";
139 ok my $query = $queries->query_by_db('protein'), "query by db (protein)";
140 is $query->term, 'BRCA and human', "term accessor correct";
141 ok !$queries->query_by_db('bllog'), "no query by db (bllog)";
142 for ( $i=0; my $q = $queries->next_query; $i++ ) {
143     ok $q->db, "db (query $i)";
145 cmp_ok scalar($queries->found_in_dbs), ">=", 11, "found in enuf dbs";
146 cmp_ok scalar($queries->found_in_dbs), "<=", $i, "but not in too many";
148 diag("I want the document summaries for a list of IDs from database 'x'.");
149 ok  $docs = $fac->esummary(-email =>$email, -db=>'gene', -id=>\@prot_ids_2 )->run(-auto_adapt=>1), "run esummary, autoadapt";
150 my @tax = (3702, 9606, 9598);
151 my @acc = qw( NC_003075.7 NC_000002.11 NC_006469.3 );
152 for ($i=0; my $doc = $docs->next_docsum; $i++) {
153     is $doc->id, $prot_ids_2[$i], " id retrieved";
154     is $doc->TaxID, $tax[$i], " taxid retrieved correctly";
155     is $doc->GenomicInfo->{ChrAccVer}, $acc[$i], "chrom acc/ver retrieved correctly";
156     cmp_ok scalar ($doc->item_names), ">", 5, "bunch o' items";
158 is $i, 3, "got all docsums";
160 diag("I want a list of database 'x' UIDs that are linked from a list of database 'y' UIDs");
162 ok my $links =$fac->elink(-email =>$email,
163                           -db => 'nucleotide',
164                           -dbfrom => 'protein',
165                           -id => \@prot_ids )->run( -auto_adapt => 1),
166 "run elink protein->nucleotide, auto adapt";
167 my %h;
168 for ($i=0; my $linkset = $links->next_linkset; $i++) {
169     my @ids = $linkset->ids;
170     my @submitted_ids = $linkset->submitted_ids;
171     $h{$submitted_ids[0]} = @ids == 1 ? $ids[0] : [@ids];
173 for (keys %h) {
174     is_deeply( $links->id_map($_) || [], $h{$_}, "id_map correct ($_)");
175     1;
177 is $i, 5, "got all linksets";
182 # remove later
183 #sub test_input_file { "data/".shift };