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