Package Bio :: Package SwissProt :: Module KeyWList
[hide private]
[frames] | no frames]

Source Code for Module Bio.SwissProt.KeyWList

 1  # Copyright 1999 by Jeffrey Chang.  All rights reserved. 
 2  # This code is part of the Biopython distribution and governed by its 
 3  # license.  Please see the LICENSE file that should have been included 
 4  # as part of this package. 
 5   
 6  """Code to parse the keywlist.txt file from SwissProt/UniProt 
 7   
 8  See: 
 9  http://www.expasy.ch/sprot/sprot-top.html 
10  ftp://ftp.expasy.org/databases/uniprot/current_release/knowledgebase/complete/docs/keywlist.txt 
11   
12  Classes: 
13   
14      - Record            Stores the information about one keyword or one category 
15        in the keywlist.txt file. 
16   
17  Functions: 
18   
19      - parse             Parses the keywlist.txt file and returns an iterator to 
20        the records it contains. 
21  """ 
22   
23  from __future__ import print_function 
24   
25   
26 -class Record(dict):
27 """ 28 This record stores the information of one keyword or category in the 29 keywlist.txt as a Python dictionary. The keys in this dictionary are 30 the line codes that can appear in the keywlist.txt file:: 31 32 --------- --------------------------- ---------------------- 33 Line code Content Occurrence in an entry 34 --------- --------------------------- ---------------------- 35 ID Identifier (keyword) Once; starts a keyword entry 36 IC Identifier (category) Once; starts a category entry 37 AC Accession (KW-xxxx) Once 38 DE Definition Once or more 39 SY Synonyms Optional; once or more 40 GO Gene ontology (GO) mapping Optional; once or more 41 HI Hierarchy Optional; once or more 42 WW Relevant WWW site Optional; once or more 43 CA Category Once per keyword entry; absent 44 in category entries 45 """
46 - def __init__(self):
47 dict.__init__(self) 48 for keyword in ("DE", "SY", "GO", "HI", "WW"): 49 self[keyword] = []
50 51
52 -def parse(handle):
53 record = Record() 54 # First, skip the header - look for start of a record 55 for line in handle: 56 if line.startswith("ID "): 57 # Looks like there was no header 58 record["ID"] = line[5:].strip() 59 break 60 if line.startswith("IC "): 61 # Looks like there was no header 62 record["IC"] = line[5:].strip() 63 break 64 # Now parse the records 65 for line in handle: 66 if line.startswith("-------------------------------------"): 67 # We have reached the footer 68 break 69 key = line[:2] 70 if key == "//": 71 record["DE"] = " ".join(record["DE"]) 72 record["SY"] = " ".join(record["SY"]) 73 yield record 74 record = Record() 75 elif line[2:5] == " ": 76 value = line[5:].strip() 77 if key in ("ID", "IC", "AC", "CA"): 78 record[key] = value 79 elif key in ("DE", "SY", "GO", "HI", "WW"): 80 record[key].append(value) 81 else: 82 print("Ignoring: %s" % line.strip()) 83 # Read the footer and throw it away 84 for line in handle: 85 pass
86