Package Bio :: Package ExPASy :: Module Prodoc
[hide private]
[frames] | no frames]

Source Code for Module Bio.ExPASy.Prodoc

  1  # Copyright 2000 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 work with the prosite.doc file from Prosite. 
  7   
  8  See http://www.expasy.ch/prosite/ 
  9   
 10  Tested with: 
 11  Release 15.0, July 1998 
 12  Release 16.0, July 1999 
 13  Release 20.22, 13 November 2007 
 14  Release 20.43, 10 February 2009 
 15   
 16   
 17  Functions: 
 18   - read               Read a Prodoc file containing exactly one Prodoc entry. 
 19   - parse              Iterates over entries in a Prodoc file. 
 20   
 21  Classes: 
 22   - Record             Holds Prodoc data. 
 23   - Reference          Holds data from a Prodoc reference. 
 24  """ 
 25   
 26  __docformat__ = "restructuredtext en" 
 27   
 28   
29 -def read(handle):
30 """Read in a record from a file with exactly one Prodoc record.""" 31 record = __read(handle) 32 # We should have reached the end of the record by now 33 line = handle.readline() 34 if line: 35 raise ValueError("More than one Prodoc record found") 36 return record
37 38
39 -def parse(handle):
40 """Iterate over the records in a Prodoc file.""" 41 while True: 42 record = __read(handle) 43 if not record: 44 return 45 yield record
46 47
48 -class Record(object):
49 """Holds information from a Prodoc record. 50 51 Members: 52 accession Accession number of the record. 53 prosite_refs List of tuples (prosite accession, prosite name). 54 text Free format text. 55 references List of reference objects. 56 57 """
58 - def __init__(self):
59 self.accession = '' 60 self.prosite_refs = [] 61 self.text = '' 62 self.references = []
63 64
65 -class Reference(object):
66 """Holds information from a Prodoc citation. 67 68 Members: 69 - number Number of the reference. (string) 70 - authors Names of the authors. 71 - citation Describes the citation. 72 73 """
74 - def __init__(self):
75 self.number = '' 76 self.authors = '' 77 self.citation = ''
78 79 # Below are private functions 80 81
82 -def __read_prosite_reference_line(record, line):
83 line = line.rstrip() 84 if line[-1] != '}': 85 raise ValueError("I don't understand the Prosite reference on line\n%s" % line) 86 acc, name = line[1:-1].split('; ') 87 record.prosite_refs.append((acc, name))
88 89
90 -def __read_text_line(record, line):
91 record.text += line 92 return True
93 94
95 -def __read_reference_start(record, line):
96 # Read the references 97 reference = Reference() 98 reference.number = line[1:3].strip() 99 if line[1] == 'E': 100 # If it's an electronic reference, then the URL is on the 101 # line, instead of the author. 102 reference.citation = line[4:].strip() 103 else: 104 reference.authors = line[4:].strip() 105 record.references.append(reference)
106 107
108 -def __read_reference_line(record, line):
109 if not line.strip(): 110 return False 111 reference = record.references[-1] 112 if line.startswith(' '): 113 if reference.authors[-1] == ',': 114 reference.authors += line[4:].rstrip() 115 else: 116 reference.citation += line[5:] 117 return True 118 raise Exception("I don't understand the reference line\n%s" % line)
119 120 126 127
128 -def __read(handle):
129 # Skip blank lines between records 130 for line in handle: 131 line = line.rstrip() 132 if line and not line.startswith("//"): 133 break 134 else: 135 return None 136 record = Record() 137 # Read the accession number 138 if not line.startswith("{PDOC"): 139 raise ValueError("Line does not start with '{PDOC':\n%s" % line) 140 if line[-1] != '}': 141 raise ValueError("I don't understand accession line\n%s" % line) 142 record.accession = line[1:-1] 143 # Read the Prosite references 144 for line in handle: 145 if line.startswith('{PS'): 146 __read_prosite_reference_line(record, line) 147 else: 148 break 149 else: 150 raise ValueError("Unexpected end of stream.") 151 # Read the actual text 152 if not line.startswith('{BEGIN'): 153 raise ValueError("Line does not start with '{BEGIN':\n%s" % line) 154 read_line = __read_text_line 155 for line in handle: 156 if line.startswith('{END}'): 157 # Clean up the record and return 158 for reference in record.references: 159 reference.citation = reference.citation.rstrip() 160 reference.authors = reference.authors.rstrip() 161 return record 162 elif line[0] == '[' and line[3] == ']' and line[4] == ' ': 163 __read_reference_start(record, line) 164 read_line = __read_reference_line 165 elif line.startswith('+----'): 166 read_line = __read_copyright_line 167 elif read_line: 168 if not read_line(record, line): 169 read_line = None 170 raise ValueError("Unexpected end of stream.")
171