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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
|
import string
import copy
import operator
import urllib
import sgmllib
import Bio.File
import Martel
from mx import TextTools
"""
The LocusLink site is:
http://www.ncbi.nlm.nih.gov/LocusLink/
Parses a Locus web page.
"""
def is_empty_container( item ):
response = 0
if is_container( item ):
if len( item ) == 0:
response = 1
return response
def is_container( item ):
response = 0
if type( item ) in [ type( [] ), type( {} ) ]:
response = 1
return response
def is_substring( a, b ):
if( a.find( b ) < 0 ):
return 0
else:
return 1
def print_params( params ):
print '%s!!!!!\n' % 'PARAMS'
for item in params:
print 'param ' + str( item )
print '-----------------'
def process_list( params ):
len_params = len( params )
container = []
while 1:
try:
element = params.pop()
except:
break
if is_close_token( element ): break
elif is_open_token( element ):
break
else:
container.append( element )
return container
def put( dict, key, val ):
if dict.has_key( key ):
element = dict[ key ]
dict[ key ] = [ element, val ]
else:
dict[ key ] = val
def process_dict( params ):
container = {}
while len( params ) > 0:
element = params.pop()
if type( element ) == type( {} ):
for key, val in element.items():
put( container, key, val )
elif is_close_token( element ): break
elif is_open_token( element ):
params.append( element )
else:
val = params.pop()
if type( val ) == type( [] ):
if len( val ) == 1:
val = val[ 0 ]
try:
put( container, element, val )
except:
print 'Element'
print element
params.append( element )
elif( not is_close_token( val ) ):
try:
put( container, element, val )
except:
print 'Element'
print element
params.append( element )
else:
break
return container
class Token:
def __init__( self, token ):
self.token = token
def __eq__( self, other ):
if not isinstance( other, self.__class__ ):
return 0
if self.token == other.token:
return 1
return 0
def __ne__( self, other ):
if not isinstance( other, Token ):
return 1
if self.token != other.token:
return 1
return 0
def __str__( self ):
output = 'token_%s\n' % self.token
return output
open_list = Token( 'open_list' )
close_list = Token( 'close_list' )
open_dict = Token( 'open_dict' )
close_dict = Token( 'close_dict' )
def is_open_token( target ):
answer = 0
if isinstance( target, Token ):
if ( open_list.__eq__( target ) ) or ( open_dict.__eq__(
target ) ):
answer = 1
return answer
def is_close_token( target ):
answer = 0
if isinstance( target, Token ):
if ( close_list.__eq__( target ) ) or ( close_dict.__eq__(
target ) ):
answer = 1
return answer
def is_token( target ):
return is_open_token( target ) or is_close_token( target )
class Url:
def __init__( self, url, label = '', description = '' ):
self.url = url
self.label = label
self.description = description
def __str__( self ):
output = '%s\n' % self.label
output = output + 'url = %s\n' % self.url
output = output + '%s\n' % self.description
return output
class Record(dict):
def __init__( self ):
dict.__init__( self )
def __str__( self ):
queue_keys = self.keys()
queue_keys.sort()
out = ''
for key in queue_keys:
out = out + '%s:\n' % key.upper()
out = out + self.print_item( self[ key ] )
out = out + '\n'
return out
def print_item( self, item, level = 1 ):
indent = ' '
out = ''
for j in range( 0, level ):
indent = indent + ' '
if( type( item ) == type( '' ) ):
if( item != '' ):
out = out + '%s%s\n' % ( indent, item )
elif( type( item ) == type([])):
for subitem in item:
out = out + self.print_item( subitem, level + 1 )
out = out + '----------------------------------------------\n'
elif( type( item ) == type ( {} ) ):
keys = item.keys()
keys.sort()
for subitem in keys:
out = out + '%skey is %s\n' % ( indent, subitem )
out = out + self.print_item( item[ subitem ], level + 1 )
elif( isinstance( item, dict ) ):
keys = item.keys()
keys.sort()
for subitem in keys:
out = out + '%skey is %s\n' % ( indent, subitem )
out = out + self.print_item( item[ subitem ], level + 1 )
else:
out = out + '%s%s\n' % ( indent, str( item ) )
return out
class LocusLinkParser( sgmllib.SGMLParser ):
def reset( self ):
sgmllib.SGMLParser.reset( self )
self.text = ''
self.record = Record()
self.open_tag_stack = []
self.open_tag = 'open_html'
self.outer_state = 'undefined'
self.section_state = 'undefined'
self.local_title = ''
self.structure_stack = []
self.category = ''
self.context_chain = []
self.outer_state_dict = { 'nomenclature' : 'nomenclature', 'overview' : 'overview', \
'function' : 'function', \
'relationships' : 'relationships', \
'locus' : 'locus', \
'map' : 'map', \
'refseq' : 'refseq', \
'genbank' : 'genbank', \
'external' : 'external_annotation', \
'additional' : 'additional_links' \
}
def parse( self, handle ):
self.reset()
self.feed( handle )
return self.record
#
# Assumes an empty line between records
#
def feed( self, handle ):
if isinstance(handle, Bio.File.UndoHandle):
uhandle = handle
else:
uhandle = Bio.File.UndoHandle(handle)
text = ''
while 1:
line = uhandle.readline()
if not line:
break
text = text + ' ' + line
sgmllib.SGMLParser.feed( self, text )
def get_text( self ):
text = self.text
self.text = ''
return text
def handle_comment( self, comment ):
while comment.startswith( '-' ):
comment = comment[ 1: ]
comment = comment.strip()
comment = comment.lower()
keys = self.outer_state_dict.keys()
for key in keys:
if comment.startswith( key ):
if key in [ 'nomenclature', 'overview', 'function',
'relationships', 'map', 'locus', 'external' ]:
self.structure_stack.append( open_dict )
elif key in [ 'genbank', 'additional' ]:
self.structure_stack.append( open_list )
elif key in [ 'refseq' ]:
self.structure_stack.append( open_list )
self.outer_state = key
self.section_state = 'local_title'
self.detail_state = 'undefined'
if( key == 'refseq' ):
self.detail_state = 'waiting_category'
else:
self.detail_state = 'waiting_key'
break
if comment.startswith( 'end' ):
if is_substring( comment.lower(), self.outer_state ):
if self.outer_state == 'refseq':
self.structure_stack.append( close_list )
elif self.outer_state == 'function':
self.structure_stack.append( close_list )
self.structure_stack.append( close_dict )
self.process_structure_stack()
while 1:
try:
item = self.structure_stack.pop()
except:
item = 'Not Available'
if not is_token( item ) : break
key = self.outer_state
self.record[ self.outer_state_dict[ key ] ] = item
self.outer_state = 'undefined'
def handle_data(self, newtext ):
newtext = string.strip( newtext )
self.text = self.text + newtext
def start_a( self, attrs ):
self.open_tag_stack.append( self.open_tag )
self.open_tag = 'open_a'
attr_dict = {}
for key, val in attrs:
attr_dict[ key ] = val
outer_state = self.outer_state
if( outer_state in [ 'nomenclature', 'overview', 'relationships', 'locus', 'map', 'genbank', 'refseq', 'additional', 'external' ] ):
if self.section_state == 'local_contents':
if self.detail_state in [ 'scan_val', 'unpaired_key' ]:
if attr_dict.has_key( 'href' ):
href = attr_dict[ 'href' ]
self.text = ''
self.structure_stack.append( Url( href, '' ) )
elif outer_state == 'function':
if self.section_state == 'local_contents':
if self.detail_state in [ 'scan_val', 'unpaired_key', 'may_be_val' ]:
if attr_dict.has_key( 'href' ):
href = attr_dict[ 'href' ]
self.text = ''
self.structure_stack.append( Url( href, '' ) )
def end_a( self ):
try:
self.open_tag = self.open_tag_stack.pop()
except:
self.open_tag = 'open_html'
outer_state = self.outer_state
if( outer_state in [ 'nomenclature', 'overview', 'relationships', 'locus', 'map', 'refseq', 'genbank', 'additional', 'external' ] ):
if self.section_state == 'local_contents':
if self.detail_state in [ 'scan_val', 'unpaired_key' ]:
text = self.get_text()
url = self.structure_stack.pop()
if isinstance( url, Url ):
url.label = text
self.structure_stack.append( url )
elif outer_state == 'function':
if self.section_state == 'local_contents':
if self.detail_state in [ 'scan_val', 'unpaired_key',
'may_be_val' ]:
text = self.get_text()
url = self.structure_stack.pop()
if isinstance( url, Url ):
url.label = text
self.structure_stack.append( url )
def start_b( self, attrs ):
self.open_tag_stack.append( self.open_tag )
self.open_tag = 'open_b'
outer_state = self.outer_state
if( outer_state in [ 'nomenclature', 'overview', 'function', 'relationships', 'locus', 'map', 'refseq', 'genbank', 'additional', 'external' ] ):
self.text = ''
def end_b( self ):
try:
self.open_tag = self.open_tag_stack.pop()
except:
self.open_tag = 'open_html'
outer_state = self.outer_state
if( outer_state in [ 'nomenclature', 'overview', 'function', 'relationships', 'locus', 'map', 'refseq', 'genbank', 'additional', 'external' ] ):
if self.section_state == 'local_contents':
text = self.get_text()
cols = text.split( ':', 1 )
key = cols[ 0 ]
if( outer_state == 'refseq' ):
self.structure_stack.append( cols[ 1 ] )
self.structure_stack.append( open_dict )
self.detail_state = 'waiting_key'
elif outer_state == 'relationships':
self.structure_stack.append( key )
self.structure_stack.append( open_list )
self.detail_state = 'skip'
elif outer_state == 'additional':
self.structure_stack.append( open_dict )
self.structure_stack.append( key )
self.structure_stack.append( open_list )
self.detail_state = 'unpaired_key'
elif outer_state == 'function':
if self.detail_state != 'waiting_key':
self.structure_stack.append( close_list )
self.structure_stack.append( key )
self.detail_state = 'unpaired_key'
self.structure_stack.append( open_list )
self.structure_stack.append( open_list )
try:
val = cols[ 1 ]
if val.strip() != '':
self.structure_stack.append( val )
self.detail_state = 'unpaired_key'
except IndexError:
pass
else:
if self.detail_state != 'waiting_key':
self.structure_stack.append( close_list )
self.detail_state = 'scan_val'
self.structure_stack.append( key )
self.structure_stack.append( open_list )
self.structure_stack.append( open_list )
try:
val = cols[ 1 ]
if val.strip() != '':
self.structure_stack.append( val )
except IndexError:
pass
def start_th( self, attrs ):
self.open_tag_stack.append( self.open_tag )
self.open_tag = 'open_th'
outer_state = self.outer_state
self.text = ''
if outer_state in [ 'function', 'relationships', 'map', 'locus', 'genbank', 'additional', 'external' ]:
if self.section_state == 'local_contents':
self.detail_state = 'scan_headings'
def end_th( self ):
try:
self.open_tag = self.open_tag_stack.pop()
except:
self.open_tag = 'open_html'
outer_state = self.outer_state
if outer_state == 'refseq':
if self.section_state == 'local_contents':
text = self.get_text()
cols = text.strip().split( ':', 1 )
if text.strip().lower().startswith( 'category' ):
self.structure_stack.append( open_dict )
self.structure_stack.append( cols[ 1 ] )
self.structure_stack.append( open_list )
self.structure_stack.append( open_dict )
self.detail_state = 'found_category'
elif self.detail_state in [ 'found_category', 'may_be_val' ]:
if text.strip() != '':
if self.detail_state != 'found_category':
self.structure_stack.append( close_list )
cols = text.split( ':' )
self.structure_stack.append( cols[ 0 ] )
self.structure_stack.append( open_list )
try:
val = cols[ 1 ]
self.structure_stack.append( open_list )
self.structure_stack.append( val )
self.detail_state = 'scan_val'
except IndexError:
self.detail_state = 'may_be_val'
def start_table( self, attrs ):
self.open_tag_stack.append( self.open_tag )
self.open_tag = 'open_table'
self.text = ''
if self.outer_state == 'genbank':
if self.section_state == 'local_contents':
self.detail_state = 'skip'
elif( self.outer_state in [ 'nomenclature', 'overview', 'relationships', 'locus', 'map', 'genbank', 'additional', 'external' ] ):
if self.section_state == 'local_contents':
self.detail_state = 'waiting_key'
def end_table( self ):
try:
self.open_tag = self.open_tag_stack.pop()
except:
self.open_tag = 'open_html'
if( self.section_state == 'local_title' ):
if self.outer_state == 'refseq':
self.section_state = 'local_contents'
elif self.outer_state == 'additional':
self.section_state = 'local_contents'
self.detail_state = 'scan_val'
else:
self.section_state = 'local_contents'
self.detail_state = 'waiting_key'
elif self.section_state == 'local_contents':
if( self.outer_state in [ 'nomenclature', 'relationships', 'locus', 'map', 'external' ] ):
self.structure_stack.append( close_list )
elif ( self.outer_state in [ 'genbank', 'additional' ] ):
if self.detail_state == 'scan_val':
self.structure_stack.append( close_list )
elif self.outer_state == 'refseq':
if self.detail_state in ['may_be_val', 'scan_val' ]:
self.structure_stack.append( close_list )
self.structure_stack.append( close_dict )
self.structure_stack.append( close_list )
self.structure_stack.append( close_dict )
self.detail_state = 'scan_category'
def start_tr( self, attrs ):
top = self.open_tag
self.open_tag_stack.append( self.open_tag )
if top == 'open_table_row':
if self.outer_state == 'refseq':
if self.section_state == 'local_contents':
if self.detail_state in [ 'scan_val', ]:
self.structure_stack.append( close_list )
self.detail_state = 'may_be_val'
self.open_tag_stack.pop()
self.open_tag = 'open_table_row'
self.text = ''
outer_state = self.outer_state
if( outer_state in [ 'relationships', 'locus', 'function', 'genbank', 'external'
] ):
if self.section_state == 'local_contents':
if self.detail_state == 'scan_val':
self.structure_stack.append( open_list )
elif outer_state == 'map':
if self.section_state == 'local_contents':
if self.detail_state == 'scan_val':
self.structure_stack.append( open_list )
elif outer_state == 'additional':
if self.section_state == 'local_contents':
self.detail_state = 'scan_val'
self.structure_stack.append( open_list )
def end_tr( self ):
try:
self.open_tag = self.open_tag_stack.pop()
except:
self.open_tag = 'open_html'
if self.section_state == 'local_contents':
if( self.outer_state in [ 'overview', 'nomenclature', 'relationships',
'locus', 'genbank', 'external' ] ):
if self.detail_state == 'scan_val':
self.structure_stack.append( close_list )
elif self.detail_state == 'unpaired_key':
self.structure_stack.append( close_list )
elif self.detail_state == 'skip':
self.detail_state = 'scan_val'
elif self.detail_state == 'scan_headings':
self.detail_state = 'scan_val'
elif self.outer_state in [ 'additional', ]:
if self.detail_state == 'unpaired_key':
self.structure_stack.append( close_list )
self.structure_stack.append( close_dict )
self.structure_stack.append( close_list )
elif self.detail_state == 'scan_val':
self.structure_stack.append( close_list )
elif self.outer_state in [ 'function', ]:
if self.detail_state == 'scan_headings':
self.detail_state = 'scan_val'
elif self.detail_state == 'unpaired_key':
self.detail_state = 'may_be_val'
self.structure_stack.append( close_list )
elif self.detail_state == 'scan_val':
self.detail_state = 'may_be_val'
self.structure_stack.append( close_list )
elif self.outer_state in [ 'refseq', ]:
if self.section_state == 'local_contents':
if self.detail_state == 'scan_val':
self.structure_stack.append( close_list )
self.detail_state = 'may_be_val'
elif self.outer_state == 'map':
if self.section_state == 'local_contents':
if self.detail_state == 'scan_val':
self.structure_stack.append( close_list )
self.detail_state = 'may_be_val'
def start_td( self, attrs ):
self.open_tag_stack.append( self.open_tag )
self.open_tag = 'open_table_data'
if self.outer_state in [ 'nomenclature', 'overview', 'relationships', 'map', 'locus', 'genbank', 'additional', 'external' ]:
if( self.section_state == 'local_contents' ):
self.text = ''
elif self.outer_state == 'refseq':
if self.section_state == 'local_contents':
self.text = ''
if self.detail_state == 'may_be_val':
self.structure_stack.append( open_list )
self.detail_state = 'scan_val'
def end_td( self ):
try:
self.open_tag = self.open_tag_stack.pop()
except:
self.open_tag = 'open_html'
if self.outer_state in [ 'nomenclature', 'overview', 'relationships', 'locus', 'genbank', 'additional', 'external' ]:
if( self.section_state == 'local_contents' ):
if self.detail_state == 'scan_val':
text = self.get_text()
if( text != '' ):
self.structure_stack.append( text )
elif self.outer_state == 'function':
if self.section_state == 'local_contents':
text = self.get_text()
if( text != '' ):
if self.detail_state == 'may_be_val':
if text.strip() != '':
self.structure_stack.append( open_list )
self.detail_state = 'scan_val'
if self.detail_state in [ 'unpaired_key', 'scan_val' ]:
self.structure_stack.append( text )
elif self.outer_state == 'map':
if self.section_state == 'local_contents':
text = self.get_text()
if( text != '' ):
if self.detail_state == 'may_be_val':
if text.strip() != '':
self.structure_stack.append( open_list )
self.detail_state = 'scan_val'
if self.detail_state == 'scan_val':
self.structure_stack.append( text )
elif self.outer_state == 'refseq':
if self.section_state == 'local_contents':
if self.detail_state == 'scan_val':
text = self.get_text()
if( text != '' ):
self.add_text_to_object( text )
def do_br( self, attrs ):
if self.outer_state in [ 'nomenclature', 'overview', 'function', 'relationships', 'map', 'locus', 'genbank', 'additional', 'external' ]:
if( self.section_state == 'local_contents' ):
if self.detail_state == 'scan_val':
if self.is_contained_by( 'open_table_data' ):
text = self.get_text()
if( text != '' ):
self.structure_stack.append( text )
def add_text_to_object( self, text ):
stack_item = self.structure_stack.pop()
if isinstance( stack_item, Url ):
if stack_item.description == '':
stack_item.description = text
self.structure_stack.append( stack_item )
else:
self.structure_stack.append( stack_item )
self.structure_stack.append( text )
def is_contained_by( self, tag ):
return tag in self.open_tag_stack
def process_structure_stack( self ):
params = []
outer_state = self.outer_state
if outer_state in [ 'nomenclature', 'overview', 'function', 'relationships', 'refseq', 'locus', 'map', 'genbank', 'additional', 'external' ]:
while len( self.structure_stack ) > 1:
len_stack = len( self.structure_stack )
# self.print_stack()
for i in range ( 0, len_stack ):
item = self.structure_stack.pop()
if not is_open_token( item ):
params.append( item )
else: break
if( open_list.__eq__( item ) ):
container = process_list( params )
params.append( container )
else:
container = process_dict( params )
if len( container ) > 0:
params.append( container )
if ( len( self.structure_stack ) == 0 ) or is_open_token(
self.structure_stack[ -1 ] ):
for j in range( 0, len( params ) ):
item = params.pop()
self.structure_stack.append( item )
params = []
def print_stack( self ):
print '%s!!!!!\n' % self.outer_state.upper()
for stack_item in self.structure_stack:
print 'stack has ' + str( stack_item )
print '-----------------'
if( __name__ == '__main__' ):
handle = open( 'Hs13225.htm')
undo_handle = Bio.File.UndoHandle( handle )
locuslink_parser = LocusLinkParser()
record = locuslink_parser.parse( handle )
print record
|