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  Record            Stores the information about one keyword or one category 
14                    in the keywlist.txt file. 
15   
16  Functions: 
17  parse             Parses the keywlist.txt file and returns an iterator to 
18                    the records it contains. 
19  """ 
20   
21   
22 -class Record(dict):
23 """ 24 This record stores the information of one keyword or category in the 25 keywlist.txt as a Python dictionary. The keys in this dictionary are 26 the line codes that can appear in the keywlist.txt file: 27 28 --------- --------------------------- ---------------------- 29 Line code Content Occurrence in an entry 30 --------- --------------------------- ---------------------- 31 ID Identifier (keyword) Once; starts a keyword entry 32 IC Identifier (category) Once; starts a category entry 33 AC Accession (KW-xxxx) Once 34 DE Definition Once or more 35 SY Synonyms Optional; once or more 36 GO Gene ontology (GO) mapping Optional; once or more 37 HI Hierarchy Optional; once or more 38 WW Relevant WWW site Optional; once or more 39 CA Category Once per keyword entry; absent 40 in category entries 41 """
42 - def __init__(self):
43 dict.__init__(self) 44 for keyword in ("DE", "SY", "GO", "HI", "WW"): 45 self[keyword] = []
46 47
48 -def parse(handle):
49 record = Record() 50 # First, skip the header - look for start of a record 51 for line in handle: 52 if line.startswith("ID "): 53 # Looks like there was no header 54 record["ID"] = line[5:].strip() 55 break 56 if line.startswith("IC "): 57 # Looks like there was no header 58 record["IC"] = line[5:].strip() 59 break 60 # Now parse the records 61 for line in handle: 62 if line.startswith("-------------------------------------"): 63 # We have reached the footer 64 break 65 key = line[:2] 66 if key == "//": 67 record["DE"] = " ".join(record["DE"]) 68 record["SY"] = " ".join(record["SY"]) 69 yield record 70 record = Record() 71 elif line[2:5] == " ": 72 value = line[5:].strip() 73 if key in ("ID", "IC", "AC", "CA"): 74 record[key] = value 75 elif key in ("DE", "SY", "GO", "HI", "WW"): 76 record[key].append(value) 77 else: 78 print "Ignoring: %s" % line.strip() 79 # Read the footer and throw it away 80 for line in handle: 81 pass
82