| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::SearchIO::Writer::BSMLResultWriter | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Jason Stajich <jason-at-bioperl-dot-org> | ||
| 7 | # | ||
| 8 | # Copyright Jason Stajich | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | |||
| 12 | # POD documentation - main docs before the code | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | FAST::Bio::SearchIO::Writer::BSMLResultWriter - BSML output writer | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use FAST::Bio::SearchIO; | ||
| 21 | my $in = FAST::Bio::SearchIO->new(-file => 'result.blast', | ||
| 22 | -format => 'blast'); | ||
| 23 | my $out = FAST::Bio::SearchIO->new(-output_format => 'BSMLResultWriter', | ||
| 24 | -file => ">result.bsml"); | ||
| 25 | while( my $r = $in->next_result ) { | ||
| 26 | $out->write_result($r); | ||
| 27 | } | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | This is a writer to produce BSML for a search result. | ||
| 32 | |||
| 33 | =head1 FEEDBACK | ||
| 34 | |||
| 35 | =head2 Mailing Lists | ||
| 36 | |||
| 37 | User feedback is an integral part of the evolution of this and other | ||
| 38 | Bioperl modules. Send your comments and suggestions preferably to | ||
| 39 | the Bioperl mailing list. Your participation is much appreciated. | ||
| 40 | |||
| 41 | bioperl-l@bioperl.org - General discussion | ||
| 42 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 43 | |||
| 44 | =head2 Support | ||
| 45 | |||
| 46 | Please direct usage questions or support issues to the mailing list: | ||
| 47 | |||
| 48 | I<bioperl-l@bioperl.org> | ||
| 49 | |||
| 50 | rather than to the module maintainer directly. Many experienced and | ||
| 51 | reponsive experts will be able look at the problem and quickly | ||
| 52 | address it. Please include a thorough description of the problem | ||
| 53 | with code and data examples if at all possible. | ||
| 54 | |||
| 55 | =head2 Reporting Bugs | ||
| 56 | |||
| 57 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 58 | of the bugs and their resolution. Bug reports can be submitted via | ||
| 59 | the web: | ||
| 60 | |||
| 61 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 62 | |||
| 63 | =head1 AUTHOR - Jason Stajich | ||
| 64 | |||
| 65 | Email jason-at-bioperl-dot-org | ||
| 66 | |||
| 67 | =head1 APPENDIX | ||
| 68 | |||
| 69 | The rest of the documentation details each of the object methods. | ||
| 70 | Internal methods are usually preceded with a _ | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | |||
| 75 | # Let the code begin... | ||
| 76 | |||
| 77 | |||
| 78 | package FAST::Bio::SearchIO::Writer::BSMLResultWriter; | ||
| 79 | use strict; | ||
| 80 | |||
| 81 | use XML::Writer; | ||
| 82 | use IO::String; | ||
| 83 | |||
| 84 | use base qw(FAST::Bio::Root::Root FAST::Bio::SearchIO::SearchWriterI); | ||
| 85 | |||
| 86 | |||
| 87 | =head2 new | ||
| 88 | |||
| 89 | Title : new | ||
| 90 | Usage : my $obj = FAST::Bio::SearchIO::Writer::BSMLResultWriter->new(); | ||
| 91 | Function: Builds a new FAST::Bio::SearchIO::Writer::BSMLResultWriter object | ||
| 92 | Returns : an instance of FAST::Bio::SearchIO::Writer::BSMLResultWriter | ||
| 93 | Args : | ||
| 94 | |||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub new { | ||
| 99 | my($class,@args) = @_; | ||
| 100 | |||
| 101 | my $self = $class->SUPER::new(@args); | ||
| 102 | return $self; | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 to_string | ||
| 106 | |||
| 107 | Purpose : Produces data for each Search::Result::ResultI in a string. | ||
| 108 | : This is an abstract method. For some useful implementations, | ||
| 109 | : see ResultTableWriter.pm, HitTableWriter.pm, | ||
| 110 | : and HSPTableWriter.pm. | ||
| 111 | Usage : print $writer->to_string( $result_obj, @args ); | ||
| 112 | Argument : $result_obj = A FAST::Bio::Search::Result::ResultI object | ||
| 113 | : @args = any additional arguments used by your implementation. | ||
| 114 | Returns : String containing data for each search Result or any of its | ||
| 115 | : sub-objects (Hits and HSPs). | ||
| 116 | Throws : n/a | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | # this implementation is largely adapted from the Incogen XSLT stylesheet | ||
| 121 | # to convert NCBI BLAST XML to BSML | ||
| 122 | |||
| 123 | sub to_string { | ||
| 124 | my ($self,$result,$num) = @_; | ||
| 125 | my $str = new IO::String(); | ||
| 126 | my $writer = new XML::Writer(OUTPUT => $str, | ||
| 127 | DATA_INDENT => 1, | ||
| 128 | DATA_MODE => 1); | ||
| 129 | $writer->xmlDecl('UTF-8'); | ||
| 130 | $writer->doctype('Bsml','-//EBI//Labbook, Inc. BSML DTD//EN', | ||
| 131 | 'http://www.labbook.com/dtd/bsml3_1.dtd'); | ||
| 132 | $writer->startTag('Bsml'); | ||
| 133 | $writer->startTag('Definitions'); | ||
| 134 | $writer->startTag('Sequences'); | ||
| 135 | my $reporttype = $result->algorithm; | ||
| 136 | my ($qmoltype,$hmoltype); | ||
| 137 | my $hit = $result->next_hit; | ||
| 138 | my $hsp = $hit->next_hsp; | ||
| 139 | if( $hsp->query->strand == 0 ) { $qmoltype = 'aa' } | ||
| 140 | else { $qmoltype = 'nt' } | ||
| 141 | |||
| 142 | if( $hsp->hit->strand == 0 ) { $hmoltype = 'aa' } | ||
| 143 | else { $hmoltype = 'nt' } | ||
| 144 | |||
| 145 | $writer->startTag('Sequence', | ||
| 146 | 'length' => $result->query_length, | ||
| 147 | 'title' => $result->query_name . " ". $result->query_description, | ||
| 148 | 'molecule' => $qmoltype, | ||
| 149 | 'representation' => 'virtual', | ||
| 150 | 'id' => $result->query_name | ||
| 151 | ); | ||
| 152 | # Here we're annotating the Query sequence with hits | ||
| 153 | # hence the Feature-table | ||
| 154 | $writer->startTag('Feature-tables'); | ||
| 155 | $writer->startTag('Feature-table', | ||
| 156 | 'title' => "$reporttype Result", | ||
| 157 | 'class' => $reporttype); | ||
| 158 | my ($hitnum,$hspnum) = (1,1); | ||
| 159 | foreach my $hit ( $result->hits ) { | ||
| 160 | $hspnum = 1; | ||
| 161 | foreach my $hsp ( $hit->hsps ) { | ||
| 162 | $writer->startTag('Feature', | ||
| 163 | 'class' => $reporttype, | ||
| 164 | 'value-type' => 'alignment', | ||
| 165 | 'title' => $hit->name. " ". $hit->description, | ||
| 166 | ); | ||
| 167 | |||
| 168 | $writer->emptyTag('Interval-loc', | ||
| 169 | 'startpos' => $hsp->query->start, | ||
| 170 | 'endpos' => $hsp->query->end); | ||
| 171 | $writer->emptyTag('Qualifier', | ||
| 172 | 'value-type' => 'score', | ||
| 173 | 'value' => $hsp->score, | ||
| 174 | ); | ||
| 175 | |||
| 176 | $writer->emptyTag('Qualifier', | ||
| 177 | 'value-type' => 'target-start', | ||
| 178 | 'value' => $hsp->hit->start, | ||
| 179 | ); | ||
| 180 | $writer->emptyTag('Qualifier', | ||
| 181 | 'value-type' => 'target-end', | ||
| 182 | 'value' => $hsp->hit->end, | ||
| 183 | ); | ||
| 184 | $writer->emptyTag('Link', | ||
| 185 | 'title' => 'alignment', | ||
| 186 | 'href' => sprintf("#SPA%d.%d",$hitnum,$hspnum) | ||
| 187 | ); | ||
| 188 | |||
| 189 | if( $hsp->hit->strand < 0 ) { | ||
| 190 | $writer->emptyTag('Qualifier', | ||
| 191 | 'value-type' => 'target-on-complement', | ||
| 192 | 'value' => 1, | ||
| 193 | ); | ||
| 194 | } | ||
| 195 | $hspnum++; | ||
| 196 | $writer->endTag('Feature'); | ||
| 197 | } | ||
| 198 | $hitnum++; | ||
| 199 | } | ||
| 200 | $writer->endTag('Feature-table'); | ||
| 201 | $writer->endTag('Feature-tables'); | ||
| 202 | $writer->endTag('Sequence'); | ||
| 203 | $writer->endTag('Sequences'); | ||
| 204 | |||
| 205 | $writer->startTag('Tables'); | ||
| 206 | $writer->startTag('Sequence-search-table', | ||
| 207 | 'search-type' => $reporttype, | ||
| 208 | 'query-length' => $result->query_length); | ||
| 209 | $hitnum = $hspnum = 1; | ||
| 210 | foreach my $hit ( $result->hits ) { | ||
| 211 | $hspnum = 1; | ||
| 212 | foreach my $hsp ( $hit->hsps ) { | ||
| 213 | $writer->startTag('Seq-pair-alignment', | ||
| 214 | 'id' => sprintf("SPA%d.%d",$hitnum,$hspnum), | ||
| 215 | 'method' => join(' ',$result->algorithm), | ||
| 216 | 'compxref' => sprintf("%s:%s", | ||
| 217 | '',$result->query_name), | ||
| 218 | 'refxref' => sprintf("%s:%s", | ||
| 219 | $result->database_name, | ||
| 220 | $hit->name), | ||
| 221 | 'refseq' => $hit->name, | ||
| 222 | 'title' => $result->query_name, | ||
| 223 | 'compseq' => $result->query_name, | ||
| 224 | 'compcaption' => $result->query_name . ' ' . | ||
| 225 | $result->query_description, | ||
| 226 | 'refcaption' => $hit->name . " ". | ||
| 227 | $hit->description, | ||
| 228 | 'totalscore' => $hsp->score, | ||
| 229 | 'refstart' => $hsp->query->start, | ||
| 230 | 'refend' => $hsp->query->end, | ||
| 231 | 'compstart' => $hsp->hit->start, | ||
| 232 | 'compend' => $hsp->hit->end, | ||
| 233 | 'complength' => $hit->length, | ||
| 234 | 'reflength' => $result->query_length); | ||
| 235 | |||
| 236 | $writer->emptyTag('Attribute', | ||
| 237 | 'name' => 'hit-num', | ||
| 238 | 'content' => $hitnum); | ||
| 239 | $writer->emptyTag('Attribute', | ||
| 240 | 'name' => 'hit-id', | ||
| 241 | 'content' => $hit->name); | ||
| 242 | $writer->emptyTag('Attribute', | ||
| 243 | 'name' => 'hsp-num', | ||
| 244 | 'content' => $hspnum); | ||
| 245 | $writer->emptyTag('Attribute', | ||
| 246 | 'name' => 'hsp-bit-score', | ||
| 247 | 'content' => $hsp->bits); | ||
| 248 | $writer->emptyTag('Attribute', | ||
| 249 | 'name' => 'hsp-evalue', | ||
| 250 | 'content' => $hsp->evalue); | ||
| 251 | $writer->emptyTag('Attribute', | ||
| 252 | 'name' => 'pattern-from', | ||
| 253 | 'content' => 0); | ||
| 254 | $writer->emptyTag('Attribute', | ||
| 255 | 'name' => 'pattern-to', | ||
| 256 | 'content' => 0); | ||
| 257 | $writer->emptyTag('Attribute', | ||
| 258 | 'name' => 'query-frame', | ||
| 259 | 'content' => $hsp->query->frame); | ||
| 260 | $writer->emptyTag('Attribute', | ||
| 261 | 'name' => 'hit-frame', | ||
| 262 | 'content' => $hsp->hit->frame * $hsp->hit->strand); | ||
| 263 | $writer->emptyTag('Attribute', | ||
| 264 | 'name' => 'percent_identity', | ||
| 265 | 'content' => sprintf("%.2f",$hsp->percent_identity)); | ||
| 266 | $writer->emptyTag('Attribute', | ||
| 267 | 'name' => 'percent_similarity', | ||
| 268 | 'content' => sprintf("%.2f",$hsp->frac_conserved('total') * 100)); | ||
| 269 | my $cons = $hsp->frac_conserved('total') * $hsp->length('total'); | ||
| 270 | my $ident = $hsp->frac_identical('total') * $hsp->length('total'); | ||
| 271 | |||
| 272 | $writer->emptyTag('Attribute', | ||
| 273 | 'name' => 'identity', | ||
| 274 | 'content' => $ident); | ||
| 275 | $writer->emptyTag('Attribute', | ||
| 276 | 'name' => 'positive', | ||
| 277 | 'content' => $cons); | ||
| 278 | $writer->emptyTag('Attribute', | ||
| 279 | 'name' => 'gaps', | ||
| 280 | 'content' => $hsp->gaps('total')); | ||
| 281 | $writer->emptyTag('Attribute', | ||
| 282 | 'name' => 'align-len', | ||
| 283 | 'content' => $hsp->length('total')); | ||
| 284 | $writer->emptyTag('Attribute', | ||
| 285 | 'name' => 'density', | ||
| 286 | 'content' => 0); | ||
| 287 | $writer->emptyTag('Attribute', | ||
| 288 | 'name' => 'hit-len', | ||
| 289 | 'content' => $hit->length); | ||
| 290 | my @extrafields; | ||
| 291 | |||
| 292 | $writer->emptyTag('Seq-pair-run', | ||
| 293 | 'runlength' => $hsp->hit->length, | ||
| 294 | 'comprunlength' => $hsp->hsp_length, | ||
| 295 | 'complength' => $hsp->hit->length, | ||
| 296 | 'compcomplement'=> $hsp->hit->strand < 0 ? 1 :0, | ||
| 297 | 'refcomplement' => $hsp->query->strand < 0 ? 1 :0, | ||
| 298 | 'refdata' => $hsp->query_string, | ||
| 299 | 'compdata' => $hsp->hit_string, | ||
| 300 | 'alignment' => $hsp->homology_string, | ||
| 301 | ); | ||
| 302 | $hspnum++; | ||
| 303 | $writer->endTag('Seq-pair-alignment'); | ||
| 304 | } | ||
| 305 | $hitnum++; | ||
| 306 | } | ||
| 307 | $writer->endTag('Sequence-search-table'); | ||
| 308 | $writer->endTag('Tables'); | ||
| 309 | |||
| 310 | $writer->startTag('Research'); | ||
| 311 | $writer->startTag('Analyses'); | ||
| 312 | $writer->startTag('Analysis'); | ||
| 313 | $writer->emptyTag('Attribute', | ||
| 314 | 'name' => 'program', | ||
| 315 | 'content' => $reporttype); | ||
| 316 | $writer->emptyTag('Attribute', | ||
| 317 | 'name' => 'version', | ||
| 318 | 'content' => join(' ',$reporttype, | ||
| 319 | $result->algorithm_version)); | ||
| 320 | $writer->emptyTag('Attribute', | ||
| 321 | 'name' => 'reference', | ||
| 322 | 'content' => $result->algorithm_reference); | ||
| 323 | $writer->emptyTag('Attribute', | ||
| 324 | 'name' => 'db', | ||
| 325 | 'content' => $result->database_name); | ||
| 326 | $writer->emptyTag('Attribute', | ||
| 327 | 'name' => 'db-size', | ||
| 328 | 'content' => $result->database_entries); | ||
| 329 | $writer->emptyTag('Attribute', | ||
| 330 | 'name' => 'db-length', | ||
| 331 | 'content' => $result->database_letters); | ||
| 332 | # $writer->emptyTag('Attribute', | ||
| 333 | # 'name' => 'iter-num', | ||
| 334 | # 'content' => $result->iteration_num); | ||
| 335 | foreach my $attr ( $result->available_parameters ) { | ||
| 336 | $writer->emptyTag('Attribute', | ||
| 337 | 'name' => $attr, | ||
| 338 | 'content' => $result->get_parameter($attr)); | ||
| 339 | } | ||
| 340 | foreach my $attr ( $result->available_statistics ) { | ||
| 341 | $writer->emptyTag('Attribute', | ||
| 342 | 'name' => $attr, | ||
| 343 | 'content' => $result->get_statistic($attr)); | ||
| 344 | } | ||
| 345 | $writer->endTag('Analysis'); | ||
| 346 | $writer->endTag('Analyses'); | ||
| 347 | $writer->endTag('Research'); | ||
| 348 | |||
| 349 | $writer->endTag('Definitions'); | ||
| 350 | $writer->endTag('Bsml'); | ||
| 351 | $writer->end(); | ||
| 352 | return ${$str->string_ref}; | ||
| 353 | } | ||
| 354 | 1; |