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