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