| 1 | package Bio::Phylo::PhyloWS::Service::Timetree; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | use base 'Bio::Phylo::PhyloWS::Service'; | ||
| 6 | use Bio::Phylo::IO 'parse'; | ||
| 7 | use Bio::Phylo::Factory; | ||
| 8 | use Bio::Phylo::Util::Logger; | ||
| 9 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 10 | use Bio::Phylo::Util::CONSTANT qw'looks_like_hash looks_like_instance'; | ||
| 11 | use Bio::Phylo::Util::Dependency qw'LWP::UserAgent HTML::TreeBuilder::XPath URI::Escape'; | ||
| 12 | use constant URL => 'http://timetree.org/time_query.php?'; | ||
| 13 | use constant NCBI => 'http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id='; | ||
| 14 | |||
| 15 | # http://localhost/nexml/service/timetree/phylows/tree/find?query=dcterms.identifier=9606%20and%20dcterms.identifier=9597 | ||
| 16 | { | ||
| 17 | my $fac = Bio::Phylo::Factory->new; | ||
| 18 | my $logger = Bio::Phylo::Util::Logger->new; | ||
| 19 | |||
| 20 | $logger->VERBOSE( | ||
| 21 | '-level' => 4, | ||
| 22 | '-class' => 'Bio::Phylo::Parsers::Timetree', | ||
| 23 | ); | ||
| 24 | |||
| 25 | my $parse_html = sub { | ||
| 26 | my ( $content ) = @_; | ||
| 27 | my $tree = HTML::TreeBuilder::XPath->new; | ||
| 28 | $tree->parse_content($content); | ||
| 29 | my ($title) = $tree->findnodes(q{//td[@class='titleBlack']/strong}); | ||
| 30 | my $str = $title->as_text; | ||
| 31 | $str =~ s/^(\d+\.\d+).*$/$1/; | ||
| 32 | return $str; | ||
| 33 | }; | ||
| 34 | |||
| 35 | my $get_query_result_raw = sub { | ||
| 36 | my ( $self, $taxon_a, $taxon_b ) = @_; | ||
| 37 | my $lua = LWP::UserAgent->new; | ||
| 38 | my $url = URL . "taxon_a=${taxon_a}&taxon_b=${taxon_b}"; | ||
| 39 | my $response = $lua->get($url); | ||
| 40 | if ( $response->is_success ) { | ||
| 41 | my $content = $response->content; | ||
| 42 | utf8::decode($content); | ||
| 43 | return $content; | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | throw 'NetworkError' => $response->status_line; | ||
| 47 | } | ||
| 48 | }; | ||
| 49 | |||
| 50 | my $create_project = sub { | ||
| 51 | my ($name_a,$name_b) = @_; | ||
| 52 | my $project = $fac->create_project; | ||
| 53 | my $forest = $fac->create_forest; | ||
| 54 | my $taxa = $fac->create_taxa; | ||
| 55 | my $taxona = $fac->create_taxon( | ||
| 56 | '-name' => $name_a, | ||
| 57 | '-link' => NCBI . $name_a, | ||
| 58 | '-guid' => $name_a, | ||
| 59 | ); | ||
| 60 | my $taxonb = $fac->create_taxon( | ||
| 61 | '-name' => $name_b, | ||
| 62 | '-link' => NCBI . $name_b, | ||
| 63 | '-guid' => $name_b, | ||
| 64 | ); | ||
| 65 | |||
| 66 | $project->insert( $taxa, $forest ); | ||
| 67 | $taxa->insert( $taxona, $taxonb ); | ||
| 68 | $forest->set_taxa($taxa); | ||
| 69 | return ( $project, $taxona, $taxonb, $forest ); | ||
| 70 | }; | ||
| 71 | |||
| 72 | =head1 NAME | ||
| 73 | |||
| 74 | Bio::Phylo::PhyloWS::Service::Timetree - PhyloWS service wrapper for Timetree | ||
| 75 | |||
| 76 | =head1 SYNOPSIS | ||
| 77 | |||
| 78 | # inside a CGI script: | ||
| 79 | use CGI; | ||
| 80 | use Bio::Phylo::PhyloWS::Service::Timetree; | ||
| 81 | |||
| 82 | my $cgi = CGI->new; | ||
| 83 | my $service = Bio::Phylo::PhyloWS::Service::Timetree->new( '-url' => $url ); | ||
| 84 | $service->handle_request($cgi); | ||
| 85 | |||
| 86 | =head1 DESCRIPTION | ||
| 87 | |||
| 88 | This is an example implementation of a PhyloWS service. The service | ||
| 89 | wraps around the timetree web site (using screen scraping) and returns | ||
| 90 | project objects that include a tree for every search result. | ||
| 91 | |||
| 92 | =head1 METHODS | ||
| 93 | |||
| 94 | =head2 ACCESSORS | ||
| 95 | |||
| 96 | =over | ||
| 97 | |||
| 98 | =item get_query_result() | ||
| 99 | |||
| 100 | Gets a phylows cql query result | ||
| 101 | |||
| 102 | Type : Accessor | ||
| 103 | Title : get_query_result | ||
| 104 | Usage : my $result = $obj->get_query_result( $query ); | ||
| 105 | Function: Gets a query result | ||
| 106 | Returns : Bio::Phylo::Project | ||
| 107 | Args : Required: $query | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub get_query_result { | ||
| 112 | my ( $self, $query ) = @_; | ||
| 113 | my ( $taxon_a, $taxon_b ); | ||
| 114 | |||
| 115 | # clean up CQL query | ||
| 116 | $query = URI::Escape::uri_unescape($query); | ||
| 117 | if ( $query =~ m/^\D*(\d+)\D+(\d+)/ ) { | ||
| 118 | ( $taxon_a, $taxon_b ) = ( $1, $2 ); | ||
| 119 | } | ||
| 120 | |||
| 121 | # download timetree result | ||
| 122 | my $content = $get_query_result_raw->( $self, $taxon_a, $taxon_b ); | ||
| 123 | |||
| 124 | # parse timetree result | ||
| 125 | my $age = $parse_html->( $content ); | ||
| 126 | |||
| 127 | # populate project | ||
| 128 | my ( $project, $taxona, $taxonb, $forest ) = $create_project->($taxon_a,$taxon_b); | ||
| 129 | my $tree = $fac->create_tree; | ||
| 130 | my $tree_root = $fac->create_node; | ||
| 131 | |||
| 132 | my $node_a = $fac->create_node( | ||
| 133 | '-branch_length' => $age, | ||
| 134 | '-parent' => $tree_root, | ||
| 135 | '-taxon' => $taxona, | ||
| 136 | '-name' => $taxona->get_name, | ||
| 137 | '-link' => NCBI . $taxona->get_name, | ||
| 138 | '-guid' => $taxona->get_name, | ||
| 139 | ); | ||
| 140 | |||
| 141 | my $node_b = $fac->create_node( | ||
| 142 | '-branch_length' => $age, | ||
| 143 | '-parent' => $tree_root, | ||
| 144 | '-taxon' => $taxonb, | ||
| 145 | '-name' => $taxonb->get_name, | ||
| 146 | '-link' => NCBI . $taxonb->get_name, | ||
| 147 | '-guid' => $taxonb->get_name, | ||
| 148 | ); | ||
| 149 | |||
| 150 | $tree->insert( $tree_root, $node_a, $node_b ); | ||
| 151 | $forest->insert($tree); | ||
| 152 | |||
| 153 | # return project | ||
| 154 | return $project; | ||
| 155 | } | ||
| 156 | |||
| 157 | =item get_supported_formats() | ||
| 158 | |||
| 159 | Gets an array ref of supported formats | ||
| 160 | |||
| 161 | Type : Accessor | ||
| 162 | Title : get_supported_formats | ||
| 163 | Usage : my @formats = @{ $obj->get_supported_formats }; | ||
| 164 | Function: Gets an array ref of supported formats | ||
| 165 | Returns : [ qw(nexml nexus newick html) ] | ||
| 166 | Args : NONE | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub get_supported_formats { [qw(nexml nexus newick html)] } | ||
| 171 | |||
| 172 | =item get_redirect() | ||
| 173 | |||
| 174 | Gets a redirect URL if relevant | ||
| 175 | |||
| 176 | Type : Accessor | ||
| 177 | Title : get_redirect | ||
| 178 | Usage : my $url = $obj->get_redirect; | ||
| 179 | Function: Gets a redirect URL if relevant | ||
| 180 | Returns : String | ||
| 181 | Args : $cgi | ||
| 182 | Comments: This method is called by handle_request so that | ||
| 183 | services can 303 redirect a record lookup to | ||
| 184 | another URL. By default, this method returns | ||
| 185 | undef (i.e. no redirect), but if this implementation | ||
| 186 | is called to handle a request that specifies | ||
| 187 | 'format=html' the request is forwarded to the | ||
| 188 | appropriate page on the http://timetree.org website | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | sub get_redirect { | ||
| 193 | my ( $self, $cgi ) = @_; | ||
| 194 | if ( $cgi->param('format') eq 'html' ) { | ||
| 195 | my $query = $cgi->param('query'); | ||
| 196 | $query = URI::Escape::uri_unescape($query); | ||
| 197 | my ( $taxon_a, $taxon_b ); | ||
| 198 | if ( $query =~ m/^\D*(\d+)\D+(\d+)/ ) { | ||
| 199 | ( $taxon_a, $taxon_b ) = ( $1, $2 ); | ||
| 200 | } | ||
| 201 | my $url = URL . "taxon_a=${taxon_a}&taxon_b=${taxon_b}"; | ||
| 202 | return $url; | ||
| 203 | } | ||
| 204 | return; | ||
| 205 | } | ||
| 206 | |||
| 207 | =back | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | # podinherit_insert_token | ||
| 212 | |||
| 213 | =head1 SEE ALSO | ||
| 214 | |||
| 215 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 216 | for any user or developer questions and discussions. | ||
| 217 | |||
| 218 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com> | ||
| 219 | |||
| 220 | =head1 CITATION | ||
| 221 | |||
| 222 | If you use Bio::Phylo in published research, please cite it: | ||
| 223 | |||
| 224 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 225 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 226 | I<BMC Bioinformatics> B<12>:63. | ||
| 227 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | } | ||
| 232 | 1; |