| 1 | package Boulder::Omim; | ||
| 2 | # | ||
| 3 | use Boulder::Stream; | ||
| 4 | require Exporter; | ||
| 5 | @ISA = qw(Exporter Boulder::Stream); | ||
| 6 | @EXPORT = (); | ||
| 7 | @EXPORT_OK = (); | ||
| 8 | use Carp; | ||
| 9 | $VERSION=1.01; | ||
| 10 | use constant DEFAULT_OMIM_PATH => '/data/omim/omim.txt'; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Boulder::Omim - Fetch Omim data records as parsed Boulder Stones | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | # parse a file of Omim records | ||
| 19 | $om = new Boulder::Omim(-accessor=>'File', | ||
| 20 | -param => '/data/omim/omim.txt'); | ||
| 21 | while (my $s = $om->get) { | ||
| 22 | print $s->Identifier; | ||
| 23 | print $s->Text; | ||
| 24 | } | ||
| 25 | |||
| 26 | # parse flatfile records yourself | ||
| 27 | open (OM,"/data/omim/omim.txt"); | ||
| 28 | local $/ = "*RECORD*"; | ||
| 29 | while (<OM>) { | ||
| 30 | my $s = Boulder::Omim->parse($_); | ||
| 31 | # etc. | ||
| 32 | } | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | Boulder::Omim provides retrieval and parsing services for OMIM records | ||
| 37 | |||
| 38 | Boulder::Omim provides retrieval and parsing services for NCBI | ||
| 39 | Omim records. It returns Omim entries in L<Stone> | ||
| 40 | format, allowing easy access to the various fields and values. | ||
| 41 | Boulder::Omim is a descendent of Boulder::Stream, and provides a | ||
| 42 | stream-like interface to a series of Stone objects. | ||
| 43 | |||
| 44 | Access to Omim is provided by one I<accessors>, which | ||
| 45 | give access to local Omim database. When you | ||
| 46 | create a new Boulder::Omim stream, you provide the | ||
| 47 | accessors, along with accessor-specific parameters that control what | ||
| 48 | entries to fetch. The accessors is: | ||
| 49 | |||
| 50 | =over 2 | ||
| 51 | |||
| 52 | =item File | ||
| 53 | |||
| 54 | This provides access to local Omim entries by reading from a flat file | ||
| 55 | (typically omim.txt file downloadable from NCBI's Ftp site). | ||
| 56 | The stream will return a Stone corresponding to each of the entries in | ||
| 57 | the file, starting from the top of the file and working downward. The | ||
| 58 | parameter is the path to the local file. | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | It is also possible to parse a single Omim entry from a text string | ||
| 63 | stored in a scalar variable, returning a Stone object. | ||
| 64 | |||
| 65 | =head2 Boulder::Omim methods | ||
| 66 | |||
| 67 | This section lists the public methods that the I<Boulder::Omim> | ||
| 68 | class makes available. | ||
| 69 | |||
| 70 | =over 4 | ||
| 71 | |||
| 72 | =item new() | ||
| 73 | |||
| 74 | # Local fetch via File | ||
| 75 | $om=new Boulder::Omim(-accessor => 'File', | ||
| 76 | -param => '/data/omim/omim.txt'); | ||
| 77 | |||
| 78 | The new() method creates a new I<Boulder::Omim> stream on the | ||
| 79 | accessor provided. The only possible accessors is B<File>. | ||
| 80 | If successful, the method returns the stream | ||
| 81 | object. Otherwise it returns undef. | ||
| 82 | |||
| 83 | new() takes the following arguments: | ||
| 84 | |||
| 85 | -accessor Name of the accessor to use | ||
| 86 | -param Parameters to pass to the accessor | ||
| 87 | |||
| 88 | Specify the accessor to use with the B<-accessor> argument. If not | ||
| 89 | specified, it defaults to B<File>. | ||
| 90 | |||
| 91 | B<-param> is an accessor-specific argument. The possibilities is: | ||
| 92 | |||
| 93 | For B<File>, the B<-param> argument must point to a string-valued | ||
| 94 | scalar, which will be interpreted as the path to the file to read | ||
| 95 | Omim entries from. | ||
| 96 | |||
| 97 | =item get() | ||
| 98 | |||
| 99 | The get() method is inherited from I<Boulder::Stream>, and simply | ||
| 100 | returns the next parsed Omim Stone, or undef if there is nothing | ||
| 101 | more to fetch. It has the same semantics as the parent class, | ||
| 102 | including the ability to restrict access to certain top-level tags. | ||
| 103 | |||
| 104 | =item put() | ||
| 105 | |||
| 106 | The put() method is inherited from the parent Boulder::Stream class, | ||
| 107 | and will write the passed Stone to standard output in Boulder format. | ||
| 108 | This means that it is currently not possible to write a | ||
| 109 | Boulder::Omim object back into Omim flatfile form. | ||
| 110 | |||
| 111 | =back | ||
| 112 | |||
| 113 | =head1 OUTPUT TAGS | ||
| 114 | |||
| 115 | The tags returned by the parsing operation are taken from the names shown in the network | ||
| 116 | Entrez interface to Omim. | ||
| 117 | |||
| 118 | =head2 Top-Level Tags | ||
| 119 | |||
| 120 | These are tags that appear at the top level of the parsed Omim | ||
| 121 | entry. | ||
| 122 | |||
| 123 | =over 4 | ||
| 124 | |||
| 125 | =item Identifier | ||
| 126 | |||
| 127 | The Omim identifier of this entry. Identifier is a single-value tag. | ||
| 128 | |||
| 129 | Example: | ||
| 130 | |||
| 131 | my $identifierNo = $s->Identifier; | ||
| 132 | |||
| 133 | =item Title | ||
| 134 | |||
| 135 | The Omim title for this entry. | ||
| 136 | |||
| 137 | Example: | ||
| 138 | my $titledef=$s->Title; | ||
| 139 | |||
| 140 | =item Text | ||
| 141 | The Text of this Omim entry | ||
| 142 | |||
| 143 | Example: | ||
| 144 | my $thetext=$s->Text; | ||
| 145 | |||
| 146 | =item Mini | ||
| 147 | The text condensed version, also called "Mini" in Entrez interface | ||
| 148 | |||
| 149 | Example: | ||
| 150 | my $themini=$s->Mini; | ||
| 151 | |||
| 152 | =item SeeAlso | ||
| 153 | References to other relevant work. | ||
| 154 | |||
| 155 | Example: | ||
| 156 | my $thereviews=$s->Reviews; | ||
| 157 | |||
| 158 | =item CreationDate | ||
| 159 | This field contains the name of the person who originated the initial entry in OMIM and the date | ||
| 160 | it appeared in the database. The entry may have been subsequently added to, edited, or totally | ||
| 161 | rewritten by others, and their attribution is listed in the CONTRIBUTORS field. | ||
| 162 | |||
| 163 | Example: | ||
| 164 | my $theCreation=$s->CreationDate; | ||
| 165 | |||
| 166 | =item Contributors | ||
| 167 | This field contains a list, in chronological order, of the persons who have contributed significantly | ||
| 168 | to the content of the MIM entry. The name is followed by "updated", "edited" or "re-created". | ||
| 169 | |||
| 170 | Example: | ||
| 171 | my @theContributors=$s->Contributors; | ||
| 172 | |||
| 173 | =item History | ||
| 174 | This field contains the edit history of this record, with an identifier and a date in which minor changes | ||
| 175 | had been performed on the record. | ||
| 176 | |||
| 177 | Example: | ||
| 178 | my @theHistory=$s->History; | ||
| 179 | |||
| 180 | =item References | ||
| 181 | The references cited in the entry. | ||
| 182 | Example: | ||
| 183 | my @theReferences=$s->References; | ||
| 184 | |||
| 185 | =item ClinicalSynopsis | ||
| 186 | The content of the Clinical Synopsis data field. | ||
| 187 | Example: | ||
| 188 | my @theClinicalSynopsis=$s->ClinicalSynopsis; | ||
| 189 | |||
| 190 | =item AllelicVariants | ||
| 191 | The Allelic Variants | ||
| 192 | Example: | ||
| 193 | my @theAllelicVariants=$s->AllelicVariants; | ||
| 194 | |||
| 195 | =back | ||
| 196 | |||
| 197 | =head1 SEE ALSO | ||
| 198 | |||
| 199 | L<Boulder>, L<Boulder::Blast>, L<Boulder::Genbank> | ||
| 200 | |||
| 201 | =head1 AUTHOR | ||
| 202 | |||
| 203 | Lincoln Stein <lstein@cshl.org>. | ||
| 204 | Luca I.G. Toldo <luca.toldo@merck.de> | ||
| 205 | |||
| 206 | Copyright (c) 1997 Lincoln D. Stein | ||
| 207 | Copyright (c) 1999 Luca I.G. Toldo | ||
| 208 | |||
| 209 | This library is free software; you can redistribute it and/or modify | ||
| 210 | it under the same terms as Perl itself. See DISCLAIMER.txt for | ||
| 211 | disclaimers of warranty. | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | # | ||
| 216 | # Following did not require any changes compared to Genbank.pm | ||
| 217 | # | ||
| 218 | sub new { | ||
| 219 | my($package,@parameters) = @_; | ||
| 220 | # superclass constructor | ||
| 221 | my($self) = new Boulder::Stream; | ||
| 222 | |||
| 223 | # figure out whether parameters are named. Look for | ||
| 224 | # an initial '-' | ||
| 225 | if ($parameters[0]=~/^-/) { | ||
| 226 | my(%parameters) = @parameters; | ||
| 227 | $self->{'accessor'}=$parameters{'-accessor'} || 'File'; | ||
| 228 | $self->{'param'}=$parameters{'-param'}; | ||
| 229 | $self->{'OUT'}=$parameters{'-out'} || 'main::STDOUT'; | ||
| 230 | } else { | ||
| 231 | $self->{'accessor'}='File'; | ||
| 232 | $self->{'param'}=[@parameters]; | ||
| 233 | } | ||
| 234 | |||
| 235 | croak "Require parameters" unless defined($self->{'param'}); | ||
| 236 | $self->{'accessor'} = new {$self->{'accessor'}}($self->{'param'}); | ||
| 237 | |||
| 238 | return bless $self,$package; | ||
| 239 | } | ||
| 240 | |||
| 241 | # | ||
| 242 | # Following required no changes compared to Genbank.pm | ||
| 243 | # | ||
| 244 | sub read_record { | ||
| 245 | my($self,@tags) = @_; | ||
| 246 | my($s); | ||
| 247 | |||
| 248 | if (wantarray) { | ||
| 249 | my(@result); | ||
| 250 | while (!$self->{'done'}) { | ||
| 251 | $s = $self->read_one_record(@tags); | ||
| 252 | next unless $s; | ||
| 253 | next if $query && !(&$query); | ||
| 254 | push(@result,$s); | ||
| 255 | } | ||
| 256 | return @result; | ||
| 257 | } | ||
| 258 | |||
| 259 | # we get here if in a scalar context | ||
| 260 | while (!$self->{'done'}) { | ||
| 261 | $s = $self->read_one_record(@tags); | ||
| 262 | next unless $s; | ||
| 263 | return $s unless $query; | ||
| 264 | return $s if &$query; | ||
| 265 | } | ||
| 266 | return undef; | ||
| 267 | } | ||
| 268 | |||
| 269 | #<LIGT> | ||
| 270 | # Here is everything new | ||
| 271 | #</LIGT> | ||
| 272 | sub parse { | ||
| 273 | my $self = shift; | ||
| 274 | my $record = shift; | ||
| 275 | return unless $record; | ||
| 276 | my $tags = shift; | ||
| 277 | my %ok; | ||
| 278 | %ok = map {$_ => 1} @$tags if ref($tags) eq 'ARRAY'; | ||
| 279 | my($s,@lines,$line,$accumulated,$key,$keyword,$value,$feature,@features, $label); | ||
| 280 | |||
| 281 | $s = new Stone; | ||
| 282 | #<LIGT> following this line the parsing of the record must be done | ||
| 283 | # each key-value pair is stored by the following command: | ||
| 284 | # $self->_addToStone($key,$value,$stone,\%ok); | ||
| 285 | # | ||
| 286 | # Process new record lines | ||
| 287 | # | ||
| 288 | # | ||
| 289 | (@recordlines)=split(/\n/,$record); | ||
| 290 | for ($i=0; $i<=$#recordlines; $i++) { | ||
| 291 | $line=@recordlines[$i]; | ||
| 292 | if ($line=~/\*FIELD\* NO/) { | ||
| 293 | $label="ID"; $i++; $omimid=@recordlines[$i]; | ||
| 294 | } elsif ($line=~/\*FIELD\* TI/) { | ||
| 295 | $label="TI"; | ||
| 296 | } elsif ($line=~/\*FIELD\* TX/) { | ||
| 297 | $label="TX"; | ||
| 298 | } elsif ($line=~/\*FIELD\* RF/) { | ||
| 299 | $label="RF"; | ||
| 300 | } elsif ($line=~/\*FIELD\* CS/) { | ||
| 301 | $label="CS"; | ||
| 302 | } elsif ($line=~/\*FIELD\* CD/) { | ||
| 303 | $label="CD"; | ||
| 304 | } elsif ($line=~/\*FIELD\* ED/) { | ||
| 305 | $label="ED"; | ||
| 306 | } elsif ($line=~/\*FIELD\* AV/) { | ||
| 307 | $label="AV"; | ||
| 308 | } elsif ($line=~/\*FIELD\* SA/) { | ||
| 309 | $label="SA"; | ||
| 310 | } elsif ($line=~/\*FIELD\* CN/) { | ||
| 311 | $label="CN"; | ||
| 312 | } elsif ($line=~/\*FIELD\* MN/) { | ||
| 313 | $label="MN"; | ||
| 314 | } else { | ||
| 315 | if ($label=~/TI/) { | ||
| 316 | $ti{$omimid}.=$line; | ||
| 317 | } elsif ($label=~/TX/) { | ||
| 318 | $tx{$omimid}.=$line; | ||
| 319 | } elsif ($label=~/RF/) { | ||
| 320 | $rf{$omimid}.=$line."\n"; | ||
| 321 | } elsif ($label=~/CS/) { | ||
| 322 | $cs{$omimid}.=$line."\n"; | ||
| 323 | } elsif ($label=~/CD/) { | ||
| 324 | $cd{$omimid}.=$line."\n"; | ||
| 325 | } elsif ($label=~/ED/) { | ||
| 326 | $ed{$omimid}.=$line."\n"; | ||
| 327 | } elsif ($label=~/AV/) { | ||
| 328 | $av{$omimid}.=$line."\n"; | ||
| 329 | } elsif ($label=~/SA/) { | ||
| 330 | $sa{$omimid}.=$line; | ||
| 331 | } elsif ($label=~/CN/) { | ||
| 332 | $cn{$omimid}.=$line."\n"; | ||
| 333 | } elsif ($label=~/MN/) { | ||
| 334 | $mn{$omimid}.=$line; | ||
| 335 | } else { | ||
| 336 | } | ||
| 337 | } | ||
| 338 | } | ||
| 339 | if (defined($omimid)) { | ||
| 340 | # First add the single field records | ||
| 341 | $self->_addToStone('Identifier',$omimid,$s,\%ok); | ||
| 342 | $self->_addToStone('Title',$ti{$omimid},$s,\%ok); | ||
| 343 | $self->_addToStone('Text',$tx{$omimid},$s,\%ok); | ||
| 344 | $self->_addToStone('Mini',$mn{$omimid},$s,\%ok); | ||
| 345 | $self->_addToStone('SeeAlso',$sa{$omimid},$s,\%ok); | ||
| 346 | $self->_addToStone('CreationDate',$cd{$omimid},$s,\%ok); | ||
| 347 | #Then handle all other fields which may have multiple values | ||
| 348 | (@EDs)=split(/\n/,$ed{$omimid}); | ||
| 349 | foreach $edi (@EDs) { | ||
| 350 | $edi=~s/\n//g; | ||
| 351 | $self->_addToStone('History',$edi,$s,\%ok); | ||
| 352 | } | ||
| 353 | # | ||
| 354 | (@CNs)=split(/\n/,$cn{$omimid}); | ||
| 355 | foreach $cni (@CNs) { | ||
| 356 | $cni=~s/\n//g; | ||
| 357 | $self->_addToStone('Contributors',$cni,$s,\%ok); | ||
| 358 | } | ||
| 359 | # | ||
| 360 | (@references)=split(/\n\n/,$rf{$omimid}); | ||
| 361 | foreach $reference (@references) { | ||
| 362 | $reference=~s/\n//g; | ||
| 363 | $self->_addToStone('References',$reference,$s,\%ok); | ||
| 364 | } | ||
| 365 | # | ||
| 366 | (@ClinicalSynopsis)=split(/\n\n/,$cs{$omimid}); | ||
| 367 | foreach $main (@ClinicalSynopsis) { | ||
| 368 | $main=~s/\n//g; | ||
| 369 | ($id,$values)=split(/:/,$main); | ||
| 370 | (@lines)=split(/;/,$values); | ||
| 371 | foreach (@lines) { | ||
| 372 | $_=~s/\s+/ /g; | ||
| 373 | (@toclean)=split(//,$_); $maxlen=$#toclean; | ||
| 374 | for ($i=0; $i<=$maxlen; $i++ ) { | ||
| 375 | $curchar=shift(@toclean); | ||
| 376 | if ($curchar=~/\s/) { | ||
| 377 | } else { | ||
| 378 | unshift(@toclean,$curchar); | ||
| 379 | $i=$maxlen+1000; | ||
| 380 | } | ||
| 381 | } | ||
| 382 | $tmpval=join('',@toclean); | ||
| 383 | $self->_addToStone('ClinicalSynopsis',$tmpval,$s,\%ok); | ||
| 384 | } | ||
| 385 | } | ||
| 386 | # | ||
| 387 | (@AllelicVariants)=split(/\n\n\./,$av{$omimid}); | ||
| 388 | foreach $variant (@AllelicVariants) { | ||
| 389 | undef $variantf; | ||
| 390 | (@details)=split(/\n/,$variant); | ||
| 391 | $idtmp=@details[0]; | ||
| 392 | for ($i=1; $i<=$#details; $i++ ) { | ||
| 393 | $variantf.=@details[$i]." "; | ||
| 394 | } | ||
| 395 | $self->_addToStone('AlelicVariants',$variantf,$s,\%ok); | ||
| 396 | } | ||
| 397 | undef $ti{$omimid}, $tx{$omimid}, $sa{$omimid}, $mn{$omimid}; | ||
| 398 | undef $cd{$omimid}, $ed{$omimid}, $cn{$omimid}; | ||
| 399 | undef $rf{$omimid}, $av{$omimid}, $cs{$omimid}; | ||
| 400 | } | ||
| 401 | #</LIGT> | ||
| 402 | return $s; | ||
| 403 | } | ||
| 404 | |||
| 405 | # | ||
| 406 | # Following is unchanged from Genbank.pm | ||
| 407 | # | ||
| 408 | sub read_one_record { | ||
| 409 | my($self,@tags) = @_; | ||
| 410 | my(%ok); | ||
| 411 | my $accessor = $self->{'accessor'}; | ||
| 412 | my $record = $accessor->fetch_next(); | ||
| 413 | unless ($record) { | ||
| 414 | $self->{'done'}++; | ||
| 415 | return undef; | ||
| 416 | } | ||
| 417 | |||
| 418 | return $self->parse($record,\@tags); | ||
| 419 | } | ||
| 420 | |||
| 421 | # | ||
| 422 | # Following is unchanged from Genbank.pm | ||
| 423 | # | ||
| 424 | sub _trim { | ||
| 425 | my($v) = @_; | ||
| 426 | $v=~s/^\s+//; | ||
| 427 | $v=~s/\s+$//; | ||
| 428 | return $v; | ||
| 429 | } | ||
| 430 | |||
| 431 | # | ||
| 432 | # Following is unchanged from Genbank.pm | ||
| 433 | # | ||
| 434 | sub _canonicalize { | ||
| 435 | my $h = shift; | ||
| 436 | substr($h,0)=~tr/a-z/A-Z/; | ||
| 437 | substr($h,1,length($h)-1)=~tr/A-Z/a-z/; | ||
| 438 | $h; | ||
| 439 | } | ||
| 440 | |||
| 441 | # | ||
| 442 | # Following is unchanged from Genbank.pm | ||
| 443 | # | ||
| 444 | sub _addToStone { | ||
| 445 | my($self,$xlabel,$value,$stone,$ok) = @_; | ||
| 446 | return unless !%{$ok} || $ok->{$xlabel}; | ||
| 447 | $stone->insert(_canonicalize($xlabel),$value); | ||
| 448 | } | ||
| 449 | |||
| 450 | #<LIGT> | ||
| 451 | # Following is entirely rewritten | ||
| 452 | #</LIGT> | ||
| 453 | sub _addFeaturesToStone { | ||
| 454 | my($self,$features,$basecount,$stone,$ok) = @_; | ||
| 455 | } | ||
| 456 | |||
| 457 | |||
| 458 | |||
| 459 | # -------------------------- DEFINITION OF ACCESSOR OBJECTS ------------------------------ | ||
| 460 | #<LIGT> | ||
| 461 | #only name changes for avoid namespace collisions | ||
| 462 | #</LIGT> | ||
| 463 | package OmimAccessor; | ||
| 464 | use Carp; | ||
| 465 | |||
| 466 | sub new { | ||
| 467 | my($class,@parameters) = @_; | ||
| 468 | croak "OmimAccessor::new: Abstract class\n"; | ||
| 469 | } | ||
| 470 | |||
| 471 | sub fetch_next { | ||
| 472 | my($self) = @_; | ||
| 473 | croak "OmimAccessor::fetch_next: Abstract class\n"; | ||
| 474 | } | ||
| 475 | |||
| 476 | sub DESTROY { | ||
| 477 | } | ||
| 478 | |||
| 479 | #<LIGT> | ||
| 480 | # Following, only the File package since the only one supported. | ||
| 481 | # If other access methods must be supported, then here appropriate | ||
| 482 | # packages and methods must be implemented | ||
| 483 | #</LIGT> | ||
| 484 | package File; | ||
| 485 | use Carp; | ||
| 486 | @ISA=qw(OmimAccessor); | ||
| 487 | $DEFAULT_PATH = Boulder::Omim::DEFAULT_OMIM_PATH(); | ||
| 488 | |||
| 489 | #<LIGT> | ||
| 490 | # Following, removed the search for the string locus in the file | ||
| 491 | # as validation that the input be compliant with parser | ||
| 492 | #</LIGT> | ||
| 493 | sub new { | ||
| 494 | my($package,$path) = @_; | ||
| 495 | $path = $DEFAULT_PATH unless $path; | ||
| 496 | open (OM,$path) or croak "File::new(): couldn't open $path: $!"; | ||
| 497 | # read the junk at the beginning | ||
| 498 | my $found; $found++; | ||
| 499 | croak "File::new(): $path doesn't look like a Omim flat file" | ||
| 500 | unless $found; | ||
| 501 | $_ = <OM>; | ||
| 502 | return bless {'fh'=>OM},$package; | ||
| 503 | } | ||
| 504 | |||
| 505 | #<LIGT> | ||
| 506 | # Following, changed the record separator | ||
| 507 | #</LIGT> | ||
| 508 | sub fetch_next { | ||
| 509 | my $self = shift; | ||
| 510 | return undef unless $self->{'fh'}; | ||
| 511 | local($/)="*RECORD*\n"; | ||
| 512 | my($line); | ||
| 513 | my($fh) = $self->{'fh'}; | ||
| 514 | chomp($line = <$fh>); | ||
| 515 | return $line; | ||
| 516 | } | ||
| 517 | |||
| 518 | 1; | ||
| 519 | |||
| 520 | __END__ |