| 1 | package Boulder::Swissprot; | ||
| 2 | use Boulder::Stream; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Boulder::SwissProt - Fetch SwissProt data records as parsed Boulder Stones | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | == missing == | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | == missing == | ||
| 15 | |||
| 16 | =head1 SEE ALSO | ||
| 17 | |||
| 18 | L<Boulder>, L<Boulder::Blast>, L<Boulder::Genbank> | ||
| 19 | |||
| 20 | =head1 AUTHOR | ||
| 21 | |||
| 22 | Lincoln Stein <lstein@cshl.org>. | ||
| 23 | Luca I.G. Toldo <luca.toldo@merck.de> | ||
| 24 | |||
| 25 | Copyright (c) 1997 Lincoln D. Stein | ||
| 26 | Copyright (c) 1999 Luca I.G. Toldo | ||
| 27 | |||
| 28 | This library is free software; you can redistribute it and/or modify | ||
| 29 | it under the same terms as Perl itself. See DISCLAIMER.txt for | ||
| 30 | disclaimers of warranty. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | require Exporter; | ||
| 35 | @ISA = qw(Exporter Boulder::Stream); | ||
| 36 | @EXPORT = (); | ||
| 37 | @EXPORT_OK = (); | ||
| 38 | |||
| 39 | use Carp; | ||
| 40 | |||
| 41 | $VERSION = 1.0; | ||
| 42 | |||
| 43 | # Hard-coded defaults - must modify for your site | ||
| 44 | use constant YANK => '/usr/local/bin/yank'; | ||
| 45 | use constant DEFAULT_SW_PATH => 'new_seq.dat'; | ||
| 46 | |||
| 47 | # Genbank entry parsing constants | ||
| 48 | # (may need to adjust!) | ||
| 49 | $KEYCOL=0; | ||
| 50 | $VALUECOL=12; | ||
| 51 | $FEATURECOL=5; | ||
| 52 | $FEATUREVALCOL=21; | ||
| 53 | |||
| 54 | # new() takes named parameters: | ||
| 55 | # -accessor=> Reference to an object class that will return a series of | ||
| 56 | # Swissprot records. Predefined objects include 'Yank', 'Entrez' and 'File'. | ||
| 57 | # (defaults to 'Entrez'). | ||
| 58 | # -param=> Parameters to pass to the subroutine. Can be a list of accession numbers | ||
| 59 | # or an entrez query. | ||
| 60 | # -out=> Output filehandle. Defaults to STDOUT. | ||
| 61 | # | ||
| 62 | # If you don't use named parameters, then will assume method 'yank' on | ||
| 63 | # a list of accession numbers. | ||
| 64 | # e.g. | ||
| 65 | # $sw = new Boulder::Swissprot(-accessor=>'Yank',-param=>[qw/M57939 M28274 L36028/]); | ||
| 66 | sub new { | ||
| 67 | my($package,@parameters) = @_; | ||
| 68 | # superclass constructor | ||
| 69 | my($self) = new Boulder::Stream; | ||
| 70 | |||
| 71 | # figure out whether parameters are named. Look for | ||
| 72 | # an initial '-' | ||
| 73 | if ($parameters[0]=~/^-/) { | ||
| 74 | my(%parameters) = @parameters; | ||
| 75 | $self->{'accessor'}=$parameters{'-accessor'} || 'Entrez'; | ||
| 76 | $self->{'param'}=$parameters{'-param'}; | ||
| 77 | $self->{'OUT'}=$parameters{'-out'} || 'main::STDOUT'; | ||
| 78 | } else { | ||
| 79 | $self->{'accessor'}='Yank'; | ||
| 80 | $self->{'param'}=[@parameters]; | ||
| 81 | } | ||
| 82 | |||
| 83 | croak "Require parameters" unless defined($self->{'param'}); | ||
| 84 | $self->{'accessor'} = new {$self->{'accessor'}}($self->{'param'}); | ||
| 85 | |||
| 86 | return bless $self,$package; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub read_record { | ||
| 90 | my($self,@tags) = @_; | ||
| 91 | my($s); | ||
| 92 | |||
| 93 | if (wantarray) { | ||
| 94 | my(@result); | ||
| 95 | while (!$self->{'done'}) { | ||
| 96 | $s = $self->read_one_record(@tags); | ||
| 97 | next unless $s; | ||
| 98 | next if $query && !(&$query); | ||
| 99 | push(@result,$s); | ||
| 100 | } | ||
| 101 | return @result; | ||
| 102 | } | ||
| 103 | |||
| 104 | # we get here if in a scalar context | ||
| 105 | while (!$self->{'done'}) { | ||
| 106 | $s = $self->read_one_record(@tags); | ||
| 107 | next unless $s; | ||
| 108 | return $s unless $query; | ||
| 109 | return $s if &$query; | ||
| 110 | } | ||
| 111 | return undef; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub parse { | ||
| 115 | my $self = shift; | ||
| 116 | my $record = shift; | ||
| 117 | return unless $record; | ||
| 118 | |||
| 119 | my $tags = shift; | ||
| 120 | my %ok; | ||
| 121 | %ok = map {$_ => 1} @$tags if ref($tags) eq 'ARRAY'; | ||
| 122 | |||
| 123 | my($s,@lines,$line,$accumulated,$key,$keyword,$value,$feature,@features); | ||
| 124 | |||
| 125 | $s = new Stone; | ||
| 126 | @lines = split("\n",$record); | ||
| 127 | |||
| 128 | foreach $line (@lines) { | ||
| 129 | # special case for the sequence itself | ||
| 130 | if ($line=~/^SQ/) { | ||
| 131 | $self->_addToStone($key,$accumulated,$s,\%ok) if $key; | ||
| 132 | last; | ||
| 133 | } | ||
| 134 | if ($line=~/^ID /) { | ||
| 135 | ($key,$id)=split(/\s+/,$line); | ||
| 136 | $self->_addToStone('Identifier',$id,$s,\%ok); | ||
| 137 | next; | ||
| 138 | } elsif ($line =~/^AC /) { | ||
| 139 | ($key,$acc)=split(/\s+/,$line); $acc=~s/\;//g; | ||
| 140 | $self->_addToStone('Accession',$acc,$s,\%ok); | ||
| 141 | next; | ||
| 142 | } elsif ($line =~/^DE /) { | ||
| 143 | ($key,$des)=split(/\s+/,$line); $des=~s/\;//g; | ||
| 144 | $self->_addToStone('Description',$des,$s,\%ok); | ||
| 145 | next; | ||
| 146 | } elsif ($line =~/^OS /) { | ||
| 147 | ($key,$os)=split(/\s+/,$os); $os=~s/\;//g; | ||
| 148 | $self->_addToStone('Organism',$os,$s,\%ok); | ||
| 149 | next; | ||
| 150 | } elsif ($line =~/^GN /) { | ||
| 151 | ($key,$gn)=split(/\s+/,$line); $gn=~s/\.//g; | ||
| 152 | $self->_addToStone('Gene_name',$gn,$s,\%ok); | ||
| 153 | next; | ||
| 154 | } elsif ($line=~/^CC -!- FUNCTION:/) { | ||
| 155 | } elsif ($line=~/^CC -!- SUBCELLULAR LOCATION:/) { | ||
| 156 | } elsif ($line=~/^CC -!- SUBUNIT:/) { | ||
| 157 | } elsif ($line=~/^CC -!- SIMILARITY: :/) { | ||
| 158 | } | ||
| 159 | } | ||
| 160 | ($sequence)=$record=~/\nSQ.*\n([\s\S]+)/; | ||
| 161 | $sequence=~s/[\s0-9-]+//g; # remove white space | ||
| 162 | $self->_addToStone('Sequence',$sequence,$s,\%ok); | ||
| 163 | return $s; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub read_one_record { | ||
| 167 | my($self,@tags) = @_; | ||
| 168 | my(%ok); | ||
| 169 | |||
| 170 | my $accessor = $self->{'accessor'}; | ||
| 171 | my $record = $accessor->fetch_next(); | ||
| 172 | unless ($record) { | ||
| 173 | $self->{'done'}++; | ||
| 174 | return undef; | ||
| 175 | } | ||
| 176 | |||
| 177 | return $self->parse($record,\@tags); | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | sub _trim { | ||
| 182 | my($v) = @_; | ||
| 183 | $v=~s/^\s+//; | ||
| 184 | $v=~s/\s+$//; | ||
| 185 | return $v; | ||
| 186 | } | ||
| 187 | |||
| 188 | sub _canonicalize { | ||
| 189 | my $h = shift; | ||
| 190 | substr($h,0)=~tr/a-z/A-Z/; | ||
| 191 | substr($h,1,length($h)-1)=~tr/A-Z/a-z/; | ||
| 192 | $h; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub _addToStone { | ||
| 196 | my($self,$label,$value,$stone,$ok) = @_; | ||
| 197 | return unless !%{$ok} || $ok->{$label}; | ||
| 198 | $stone->insert(_canonicalize($label),$value); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub _addFeaturesToStone { | ||
| 202 | my($self,$features,$basecount,$stone,$ok) = @_; | ||
| 203 | |||
| 204 | # first add the basecount | ||
| 205 | if (!%{$ok} || $ok->{'BASECOUNT'}) { | ||
| 206 | my(%counts) = $basecount=~/(\d+)\s+([gatcGATC])/g; | ||
| 207 | %counts = reverse %counts; | ||
| 208 | $stone->insert('Basecount',new Stone(%counts)); | ||
| 209 | } | ||
| 210 | |||
| 211 | if (!%{$ok} || $ok->{'FEATURES'}) { | ||
| 212 | # now add the features | ||
| 213 | my($f) = new Stone; | ||
| 214 | foreach (@$features) { | ||
| 215 | my($q) = $_->{'value'}; | ||
| 216 | my($label) = _canonicalize($_->{'label'}); | ||
| 217 | my($position) = $q=~m!^([^/]+)!; | ||
| 218 | my @qualifiers = $q=~m!/(\w+)=([^/]+)!g; | ||
| 219 | my %qualifiers; | ||
| 220 | while (my($key,$value) = splice(@qualifiers,0,2)) { | ||
| 221 | $value =~ s/^\s*\"//; | ||
| 222 | $value =~s/\"\s*$//; | ||
| 223 | $value=~s/\s+//g if uc($key) eq 'TRANSLATION'; # get rid of spaces in protein translation | ||
| 224 | $qualifiers{_canonicalize($key)} = $value; | ||
| 225 | } | ||
| 226 | $f->insert($label=>new Stone('Position'=>$position,%qualifiers)); | ||
| 227 | } | ||
| 228 | $stone->insert('Features',$f); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | # ---------------------------------------------------------------------------------------- | ||
| 233 | # -------------------------- DEFINITION OF ACCESSOR OBJECTS ------------------------------ | ||
| 234 | package SwissprotAccessor; | ||
| 235 | use Carp; | ||
| 236 | |||
| 237 | sub new { | ||
| 238 | my($class,@parameters) = @_; | ||
| 239 | croak "SwissprotAccessor::new: Abstract class\n"; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub fetch_next { | ||
| 243 | my($self) = @_; | ||
| 244 | croak "SwissprotAccessor::fetch_next: Abstract class\n"; | ||
| 245 | } | ||
| 246 | |||
| 247 | sub DESTROY { | ||
| 248 | } | ||
| 249 | |||
| 250 | package Yank; | ||
| 251 | use Carp; | ||
| 252 | |||
| 253 | @ISA=qw(SwissprotAccessor); | ||
| 254 | $YANK = Boulder::Swissprot::YANK(); | ||
| 255 | |||
| 256 | sub new { | ||
| 257 | my($package,$param) = @_; | ||
| 258 | croak "Yank::new(): need at least one Swissprot acccession number" unless $param; | ||
| 259 | croak "Yank::new(): yank executable not found" unless -x $YANK; | ||
| 260 | my (@accession) = ref($param) eq 'ARRAY' ? @$param : $param; | ||
| 261 | my($tmpfile) = "/usr/tmp/yank$$"; | ||
| 262 | open (TMP,">$tmpfile") || croak "Yank::new(): couldn't open tmpfile $tmpfile for write: $!"; | ||
| 263 | print TMP join("\n",@accession),"\n"; | ||
| 264 | close TMP; | ||
| 265 | open(YANK,"$YANK < $tmpfile |") || croak "Yank::new(): couldn't open pipe from yank: $!"; | ||
| 266 | return bless {'tmpfile'=>$tmpfile,'fh'=>YANK},$package; | ||
| 267 | } | ||
| 268 | |||
| 269 | sub fetch_next { | ||
| 270 | my($self) = @_; | ||
| 271 | return undef unless $self->{'fh'}; | ||
| 272 | local($/) = "//\n"; | ||
| 273 | my($line); | ||
| 274 | my($fh) = $self->{'fh'}; | ||
| 275 | chomp($line = <$fh>); | ||
| 276 | return $line; | ||
| 277 | } | ||
| 278 | |||
| 279 | sub DESTROY { | ||
| 280 | my($self) = shift; | ||
| 281 | close $self->{'fh'} if $self->{'fh'}; | ||
| 282 | unlink $self->{'tmpfile'} if $self->{'tmpfile'} | ||
| 283 | } | ||
| 284 | |||
| 285 | package File; | ||
| 286 | use Carp; | ||
| 287 | @ISA=qw(SwissprotAccessor); | ||
| 288 | $DEFAULT_PATH = Boulder::Swissprot::DEFAULT_SW_PATH(); | ||
| 289 | |||
| 290 | sub new { | ||
| 291 | my($package,$path) = @_; | ||
| 292 | $path = $DEFAULT_PATH unless $path; | ||
| 293 | open (SW,$path) or croak "File::new(): couldn't open $path: $!"; | ||
| 294 | # read the junk at the beginning | ||
| 295 | my $found; | ||
| 296 | $_ = <SW>; | ||
| 297 | return bless {'fh'=>SW},$package; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub fetch_next { | ||
| 301 | my $self = shift; | ||
| 302 | return undef unless $self->{'fh'}; | ||
| 303 | local($/)="//\n"; | ||
| 304 | my($line); | ||
| 305 | my($fh) = $self->{'fh'}; | ||
| 306 | chomp($line = <$fh>); | ||
| 307 | return $line; | ||
| 308 | } | ||
| 309 | |||
| 310 | package Entrez; | ||
| 311 | use Carp; | ||
| 312 | use IO::Socket; | ||
| 313 | |||
| 314 | use constant HOST => 'www.ncbi.nlm.nih.gov'; | ||
| 315 | use constant URI => '/htbin-post/Entrez/query?form=6&Dopt=g&html=no'; | ||
| 316 | use constant PROTO => 'HTTP/1.0'; | ||
| 317 | use constant CRLF => "\r\n"; | ||
| 318 | |||
| 319 | @ISA=qw(SwissprotAccessor); | ||
| 320 | |||
| 321 | sub new { | ||
| 322 | my($package,$param) = @_; | ||
| 323 | croak "Entrez::new(): usage [list of accession numbers] or {args => values}" | ||
| 324 | unless $param; | ||
| 325 | my $self = {}; | ||
| 326 | |||
| 327 | $self->{query} = $param unless ref($param); | ||
| 328 | $self->{accession} = $param if ref($param) eq 'ARRAY'; | ||
| 329 | %$self = map { s/^-//; $_; } %$param if ref($param) eq 'HASH'; | ||
| 330 | $self->{query} || $self->{accession} | ||
| 331 | || croak "Must provide a 'query' or 'accession' argument"; | ||
| 332 | $self->{max} ||= 100; | ||
| 333 | $self->{'db'} ||= 'n'; | ||
| 334 | return bless $self,$package; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub fetch_next { | ||
| 338 | my $self = shift; | ||
| 339 | |||
| 340 | # if any additional records are left, then return them | ||
| 341 | if (@{$self->{'records'}}) { | ||
| 342 | my $data = shift @{$self->{'records'}}; | ||
| 343 | if ($data=~/\S/) { | ||
| 344 | $self->_cleanup(\$data); | ||
| 345 | return $data; | ||
| 346 | } else { | ||
| 347 | $self->{'records'} = []; | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | # if we have a socket open, then read a record | ||
| 352 | if ($self->{'socket'}) { | ||
| 353 | my $data = $self->{'socket'}->getline; | ||
| 354 | $self->_cleanup(\$data); | ||
| 355 | return $data; | ||
| 356 | } | ||
| 357 | |||
| 358 | # otherwise if we are reading from a series of accession numbers, | ||
| 359 | # do a one-time fetch | ||
| 360 | if (exists $self->{'accession'}) { | ||
| 361 | my $accession = shift @{$self->{'accession'}}; | ||
| 362 | return unless $accession; | ||
| 363 | |||
| 364 | my $sock = $self->_request(URI . "&db=$self->{db}&uid=$accession"); | ||
| 365 | return unless $sock; | ||
| 366 | |||
| 367 | @{$self->{'records'}} = $sock->getlines; | ||
| 368 | my $data = shift @{$self->{'records'}}; | ||
| 369 | $self->_cleanup(\$data); | ||
| 370 | return $data; | ||
| 371 | } | ||
| 372 | |||
| 373 | # Otherwise we are running a query. Need to set up the socket | ||
| 374 | $self->{query} =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg; | ||
| 375 | my $search = URI . "&db=$self->{db}&dispmax=$self->{max}&term=$self->{query}"; | ||
| 376 | $search .= "&relpubdate=$self->{age}" if $self->{age} > 0; | ||
| 377 | $self->{'socket'} = $self->_request($search); | ||
| 378 | return unless $self->{'socket'}; | ||
| 379 | |||
| 380 | my $data = $self->{'socket'}->getline; | ||
| 381 | $self->_cleanup(\$data); | ||
| 382 | return $data; | ||
| 383 | } | ||
| 384 | |||
| 385 | sub _cleanup { | ||
| 386 | my ($self,$d) = @_; | ||
| 387 | $$d =~ s/\A\s+//; | ||
| 388 | $$d=~s!//\n$!!; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub _request { | ||
| 392 | my $self = shift; | ||
| 393 | my $uri = shift; | ||
| 394 | my $sock = IO::Socket::INET->new( | ||
| 395 | PeerAddr => HOST, | ||
| 396 | PeerPort => 'http(80)', | ||
| 397 | Proto => 'tcp' | ||
| 398 | ); | ||
| 399 | return unless $sock; | ||
| 400 | print $sock "GET $uri ",PROTO,CRLF,CRLF; | ||
| 401 | $sock->input_record_separator( CRLF . CRLF); | ||
| 402 | my $header = $sock->getline; | ||
| 403 | return unless $header; | ||
| 404 | return unless $header =~ /^HTTP\/[\d.]+ 200/; | ||
| 405 | # read until we get to the '----' line | ||
| 406 | $sock->input_record_separator("\n"); | ||
| 407 | while ($_ = $sock->getline) { | ||
| 408 | return undef if /^ERROR/; | ||
| 409 | if (/^------/) { | ||
| 410 | $sock->input_record_separator("//\n"); | ||
| 411 | return $sock; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | return; | ||
| 415 | } | ||
| 416 | |||
| 417 | 1; | ||
| 418 | |||
| 419 | __END__ |