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  Functions: 
 17   - read               Read a Prodoc file containing exactly one Prodoc entry. 
 18   - parse              Iterates over entries in a Prodoc file. 
 19   
 20  Classes: 
 21   - Record             Holds Prodoc data. 
 22   - Reference          Holds data from a Prodoc reference. 
 23   
 24  """ 
 25   
 26   
27 -def read(handle):
28 """Read in a record from a file with exactly one Prodoc record.""" 29 record = __read(handle) 30 # We should have reached the end of the record by now 31 line = handle.readline() 32 if line: 33 raise ValueError("More than one Prodoc record found") 34 return record
35 36
37 -def parse(handle):
38 """Iterate over the records in a Prodoc file.""" 39 while True: 40 record = __read(handle) 41 if not record: 42 return 43 yield record
44 45
46 -class Record(object):
47 """Holds information from a Prodoc record. 48 49 Attributes: 50 - accession Accession number of the record. 51 - prosite_refs List of tuples (prosite accession, prosite name). 52 - text Free format text. 53 - references List of reference objects. 54 55 """ 56
57 - def __init__(self):
58 self.accession = '' 59 self.prosite_refs = [] 60 self.text = '' 61 self.references = []
62 63
64 -class Reference(object):
65 """Holds information from a Prodoc citation. 66 67 Attributes: 68 - number Number of the reference. (string) 69 - authors Names of the authors. 70 - citation Describes the citation. 71 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