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   
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 Members: 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 - def __init__(self):
57 self.accession = '' 58 self.prosite_refs = [] 59 self.text = '' 60 self.references = []
61 62
63 -class Reference(object):
64 """Holds information from a Prodoc citation. 65 66 Members: 67 - number Number of the reference. (string) 68 - authors Names of the authors. 69 - citation Describes the citation. 70 71 """
72 - def __init__(self):
73 self.number = '' 74 self.authors = '' 75 self.citation = ''
76 77 # Below are private functions 78 79
80 -def __read_prosite_reference_line(record, line):
81 line = line.rstrip() 82 if line[-1] != '}': 83 raise ValueError("I don't understand the Prosite reference on line\n%s" % line) 84 acc, name = line[1:-1].split('; ') 85 record.prosite_refs.append((acc, name))
86 87
88 -def __read_text_line(record, line):
89 record.text += line 90 return True
91 92
93 -def __read_reference_start(record, line):
94 # Read the references 95 reference = Reference() 96 reference.number = line[1:3].strip() 97 if line[1] == 'E': 98 # If it's an electronic reference, then the URL is on the 99 # line, instead of the author. 100 reference.citation = line[4:].strip() 101 else: 102 reference.authors = line[4:].strip() 103 record.references.append(reference)
104 105
106 -def __read_reference_line(record, line):
107 if not line.strip(): 108 return False 109 reference = record.references[-1] 110 if line.startswith(' '): 111 if reference.authors[-1] == ',': 112 reference.authors += line[4:].rstrip() 113 else: 114 reference.citation += line[5:] 115 return True 116 raise Exception("I don't understand the reference line\n%s" % line)
117 118 124 125
126 -def __read(handle):
127 # Skip blank lines between records 128 for line in handle: 129 line = line.rstrip() 130 if line and not line.startswith("//"): 131 break 132 else: 133 return None 134 record = Record() 135 # Read the accession number 136 if not line.startswith("{PDOC"): 137 raise ValueError("Line does not start with '{PDOC':\n%s" % line) 138 if line[-1] != '}': 139 raise ValueError("I don't understand accession line\n%s" % line) 140 record.accession = line[1:-1] 141 # Read the Prosite references 142 for line in handle: 143 if line.startswith('{PS'): 144 __read_prosite_reference_line(record, line) 145 else: 146 break 147 else: 148 raise ValueError("Unexpected end of stream.") 149 # Read the actual text 150 if not line.startswith('{BEGIN'): 151 raise ValueError("Line does not start with '{BEGIN':\n%s" % line) 152 read_line = __read_text_line 153 for line in handle: 154 if line.startswith('{END}'): 155 # Clean up the record and return 156 for reference in record.references: 157 reference.citation = reference.citation.rstrip() 158 reference.authors = reference.authors.rstrip() 159 return record 160 elif line[0] == '[' and line[3] == ']' and line[4] == ' ': 161 __read_reference_start(record, line) 162 read_line = __read_reference_line 163 elif line.startswith('+----'): 164 read_line = __read_copyright_line 165 elif read_line: 166 if not read_line(record, line): 167 read_line = None 168 raise ValueError("Unexpected end of stream.")
169