This file is indexed.

/usr/share/perl5/Boulder/Swissprot.pm is in libboulder-perl 1.30-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package Boulder::Swissprot;
use Boulder::Stream;

=head1 NAME

Boulder::SwissProt - Fetch SwissProt data records as parsed Boulder Stones

=head1 SYNOPSIS

 == missing ==

=head1 DESCRIPTION

 == missing ==

=head1 SEE ALSO

L<Boulder>, L<Boulder::Blast>, L<Boulder::Genbank>

=head1 AUTHOR

Lincoln Stein <lstein@cshl.org>.
Luca I.G. Toldo <luca.toldo@merck.de>

Copyright (c) 1997 Lincoln D. Stein
Copyright (c) 1999 Luca I.G. Toldo

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.  See DISCLAIMER.txt for
disclaimers of warranty.

=cut

require Exporter;
@ISA = qw(Exporter Boulder::Stream);
@EXPORT = ();
@EXPORT_OK = ();

use Carp;

$VERSION = 1.0;

# Hard-coded defaults - must modify for your site
use constant YANK            =>  '/usr/local/bin/yank';
use constant DEFAULT_SW_PATH =>  'new_seq.dat';

# Genbank entry parsing constants
# (may need to adjust!)
$KEYCOL=0;
$VALUECOL=12;
$FEATURECOL=5;
$FEATUREVALCOL=21;

# new() takes named parameters:
# -accessor=> Reference to an object class that will return a series of
#          Swissprot records.  Predefined objects include 'Yank', 'Entrez' and 'File'.
#           (defaults to 'Entrez').
# -param=>  Parameters to pass to the subroutine.  Can be a list of accession numbers
#           or an entrez query.
# -out=>    Output filehandle.  Defaults to STDOUT.
#
# If you don't use named parameters, then will assume method 'yank' on
# a list of accession numbers.
# e.g.
#        $sw = new Boulder::Swissprot(-accessor=>'Yank',-param=>[qw/M57939 M28274 L36028/]);
sub new {
    my($package,@parameters) = @_;
    # superclass constructor
    my($self) = new Boulder::Stream;
    
    # figure out whether parameters are named.  Look for
    # an initial '-'
    if ($parameters[0]=~/^-/) {
	my(%parameters) = @parameters;
	$self->{'accessor'}=$parameters{'-accessor'} || 'Entrez';
	$self->{'param'}=$parameters{'-param'};
	$self->{'OUT'}=$parameters{'-out'} || 'main::STDOUT';
    } else {
	$self->{'accessor'}='Yank';
	$self->{'param'}=[@parameters];
    }
    
    croak "Require parameters" unless defined($self->{'param'});
    $self->{'accessor'} = new {$self->{'accessor'}}($self->{'param'});
    
    return bless $self,$package;
}

sub read_record {
    my($self,@tags) = @_;
    my($s);

    if (wantarray) {
	my(@result);
	while (!$self->{'done'}) {
	    $s = $self->read_one_record(@tags);
	    next unless $s;
	    next if $query && !(&$query);
	    push(@result,$s);
	}
	return @result;
    } 

    # we get here if in a scalar context
    while (!$self->{'done'}) {
	$s = $self->read_one_record(@tags);
	next unless $s;
	return $s unless $query;
	return $s if &$query;
    }
    return undef;
}

sub parse {
  my $self = shift;
  my $record = shift;
  return unless $record;

  my $tags = shift;
  my %ok;
  %ok = map {$_ => 1} @$tags if ref($tags) eq 'ARRAY';
  
  my($s,@lines,$line,$accumulated,$key,$keyword,$value,$feature,@features);
  
  $s = new Stone;
  @lines = split("\n",$record);
  
  foreach $line (@lines) {
    # special case for the sequence itself
    if ($line=~/^SQ/) {
      $self->_addToStone($key,$accumulated,$s,\%ok) if $key;
      last;
    }    
    if ($line=~/^ID   /) {
       ($key,$id)=split(/\s+/,$line);
	$self->_addToStone('Identifier',$id,$s,\%ok);
      next;
    } elsif ($line =~/^AC  /) {
       ($key,$acc)=split(/\s+/,$line); $acc=~s/\;//g;
	$self->_addToStone('Accession',$acc,$s,\%ok);
      next;
    } elsif ($line =~/^DE  /) {
       ($key,$des)=split(/\s+/,$line); $des=~s/\;//g;
	$self->_addToStone('Description',$des,$s,\%ok);
      next;
    } elsif ($line =~/^OS  /) {
       ($key,$os)=split(/\s+/,$os); $os=~s/\;//g;
	$self->_addToStone('Organism',$os,$s,\%ok);
      next;
    } elsif ($line =~/^GN  /) {
       ($key,$gn)=split(/\s+/,$line); $gn=~s/\.//g;
	$self->_addToStone('Gene_name',$gn,$s,\%ok);
      next;
    } elsif ($line=~/^CC   -!- FUNCTION:/) {
    } elsif ($line=~/^CC   -!- SUBCELLULAR LOCATION:/) {
    } elsif ($line=~/^CC   -!- SUBUNIT:/) {
    } elsif ($line=~/^CC   -!- SIMILARITY: :/) {
    }
  }
  ($sequence)=$record=~/\nSQ.*\n([\s\S]+)/;
  $sequence=~s/[\s0-9-]+//g;  # remove white space
  $self->_addToStone('Sequence',$sequence,$s,\%ok);
  return $s;
}

sub read_one_record {
  my($self,@tags) = @_;
  my(%ok);
  
  my $accessor = $self->{'accessor'};
  my $record   = $accessor->fetch_next();
  unless ($record) {
    $self->{'done'}++;
    return undef;
  }

  return $self->parse($record,\@tags);
}


sub _trim {
    my($v) = @_;
    $v=~s/^\s+//;
    $v=~s/\s+$//;
    return $v;
}

sub _canonicalize {
  my $h = shift;
  substr($h,0)=~tr/a-z/A-Z/;
  substr($h,1,length($h)-1)=~tr/A-Z/a-z/;
  $h;
}

sub _addToStone {
    my($self,$label,$value,$stone,$ok) = @_;
    return unless !%{$ok} || $ok->{$label};
    $stone->insert(_canonicalize($label),$value);
}

sub _addFeaturesToStone {
    my($self,$features,$basecount,$stone,$ok) = @_;

    # first add the basecount
    if (!%{$ok} || $ok->{'BASECOUNT'}) {
	my(%counts) = $basecount=~/(\d+)\s+([gatcGATC])/g;
	%counts = reverse %counts;
	$stone->insert('Basecount',new Stone(%counts));
    }
    
    if (!%{$ok} || $ok->{'FEATURES'}) {
	# now add the features
	my($f) = new Stone;
	foreach (@$features) {
	    my($q) = $_->{'value'};
	    my($label) = _canonicalize($_->{'label'});
	    my($position) = $q=~m!^([^/]+)!;
	    my @qualifiers = $q=~m!/(\w+)=([^/]+)!g;
	    my %qualifiers;
	    while (my($key,$value) = splice(@qualifiers,0,2)) {
	      $value =~ s/^\s*\"//;
	      $value =~s/\"\s*$//;
	      $value=~s/\s+//g if uc($key) eq 'TRANSLATION';  # get rid of spaces in protein translation
	      $qualifiers{_canonicalize($key)} = $value;
	    }
	    $f->insert($label=>new Stone('Position'=>$position,%qualifiers));
	}
	$stone->insert('Features',$f);
    }
}

# ----------------------------------------------------------------------------------------
# -------------------------- DEFINITION OF ACCESSOR OBJECTS ------------------------------
package SwissprotAccessor;
use Carp;

sub new {
    my($class,@parameters) = @_;
    croak "SwissprotAccessor::new:  Abstract class\n";
}

sub fetch_next {
    my($self) = @_;
    croak "SwissprotAccessor::fetch_next: Abstract class\n";
}

sub DESTROY {
}

package Yank;
use Carp;

@ISA=qw(SwissprotAccessor);
$YANK = Boulder::Swissprot::YANK();

sub new {
    my($package,$param) = @_;
    croak "Yank::new(): need at least one Swissprot acccession number" unless $param;
    croak "Yank::new(): yank executable not found" unless -x $YANK;
    my (@accession) = ref($param) eq 'ARRAY' ? @$param : $param;
    my($tmpfile) = "/usr/tmp/yank$$";
    open (TMP,">$tmpfile") || croak "Yank::new(): couldn't open tmpfile $tmpfile for write: $!";
    print TMP join("\n",@accession),"\n";
    close TMP;
    open(YANK,"$YANK < $tmpfile |") || croak "Yank::new(): couldn't open pipe from yank: $!";
    return bless {'tmpfile'=>$tmpfile,'fh'=>YANK},$package;
}

sub fetch_next {
    my($self) = @_;
    return undef unless $self->{'fh'};
    local($/) = "//\n";
    my($line);
    my($fh) = $self->{'fh'};
    chomp($line = <$fh>);
    return $line;
}

sub DESTROY {
    my($self) = shift;
    close $self->{'fh'} if $self->{'fh'};
    unlink $self->{'tmpfile'} if $self->{'tmpfile'}
}

package File;
use Carp;
@ISA=qw(SwissprotAccessor);
$DEFAULT_PATH = Boulder::Swissprot::DEFAULT_SW_PATH();

sub new {
    my($package,$path) = @_;
    $path = $DEFAULT_PATH unless $path;
    open (SW,$path) or croak "File::new(): couldn't open $path: $!";
    # read the junk at the beginning
    my $found;
    $_ = <SW>;
    return bless {'fh'=>SW},$package;
}

sub fetch_next {
    my $self = shift;
    return undef unless $self->{'fh'};
    local($/)="//\n";
    my($line);
    my($fh) = $self->{'fh'};
    chomp($line = <$fh>);
    return $line;
}

package Entrez;
use Carp;
use IO::Socket;

use constant HOST  => 'www.ncbi.nlm.nih.gov';
use constant URI   => '/htbin-post/Entrez/query?form=6&Dopt=g&html=no';
use constant PROTO => 'HTTP/1.0';
use constant CRLF  => "\r\n";

@ISA=qw(SwissprotAccessor);

sub new {
    my($package,$param) = @_;
    croak "Entrez::new(): usage [list of accession numbers] or {args => values}" 
      unless $param;
    my $self = {};

    $self->{query}     = $param       unless ref($param);
    $self->{accession} = $param       if ref($param) eq 'ARRAY';
    %$self             = map { s/^-//; $_; } %$param  if ref($param) eq 'HASH';
    $self->{query} || $self->{accession} 
                   || croak "Must provide a 'query' or 'accession' argument";
    $self->{max}   ||= 100;
    $self->{'db'}  ||= 'n';
    return bless $self,$package;
}

sub fetch_next {
    my $self = shift;

    # if any additional records are left, then return them
    if (@{$self->{'records'}}) {
      my $data = shift @{$self->{'records'}};
      if ($data=~/\S/) {
	$self->_cleanup(\$data);
	return $data;
      } else {
	$self->{'records'} = [];
      }
    }

    # if we have a socket open, then read a record
    if ($self->{'socket'}) {
      my $data = $self->{'socket'}->getline;
      $self->_cleanup(\$data);
      return $data;
    }

    # otherwise if we are reading from a series of accession numbers,
    # do a one-time fetch
    if (exists $self->{'accession'}) {
      my $accession = shift @{$self->{'accession'}};
      return unless $accession;

      my $sock     = $self->_request(URI . "&db=$self->{db}&uid=$accession");
      return unless $sock;

      @{$self->{'records'}} = $sock->getlines;
      my $data = shift @{$self->{'records'}};
      $self->_cleanup(\$data);
      return $data;
    }

    # Otherwise we are running a query.  Need to set up the socket
    $self->{query} =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
    my $search = URI . "&db=$self->{db}&dispmax=$self->{max}&term=$self->{query}";
    $search .= "&relpubdate=$self->{age}" if $self->{age} > 0;
    $self->{'socket'} = $self->_request($search);
    return unless $self->{'socket'};

    my $data = $self->{'socket'}->getline;
    $self->_cleanup(\$data);
    return $data;
}

sub _cleanup {
  my ($self,$d) = @_;
  $$d =~ s/\A\s+//;
  $$d=~s!//\n$!!;
}

sub _request {
  my $self = shift;
  my $uri  = shift;
  my $sock = IO::Socket::INET->new(
				   PeerAddr => HOST,
				   PeerPort => 'http(80)',
				   Proto    => 'tcp'
				  );
  return unless $sock;
  print $sock "GET $uri ",PROTO,CRLF,CRLF;
  $sock->input_record_separator( CRLF . CRLF);
  my $header = $sock->getline;
  return unless $header;
  return unless $header =~ /^HTTP\/[\d.]+ 200/;
  # read until we get to the '----' line
  $sock->input_record_separator("\n");
  while ($_ = $sock->getline) {
    return undef if /^ERROR/;
    if (/^------/) {
      $sock->input_record_separator("//\n");
      return $sock;
    }
  }
  return;
}

1;

__END__