Back to index

python-biopython  1.60
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes
Bio.AlignIO.StockholmIO.StockholmWriter Class Reference
Inheritance diagram for Bio.AlignIO.StockholmIO.StockholmWriter:
Inheritance graph
[legend]
Collaboration diagram for Bio.AlignIO.StockholmIO.StockholmWriter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def write_alignment

Static Public Attributes

dictionary pfam_gr_mapping
dictionary pfam_gs_mapping

Private Member Functions

def _write_record

Private Attributes

 _length_of_sequences
 _ids_written

Detailed Description

Stockholm/PFAM alignment writer.

Definition at line 138 of file StockholmIO.py.


Member Function Documentation

def Bio.AlignIO.StockholmIO.StockholmWriter._write_record (   self,
  record 
) [private]
Write a single SeqRecord to the file

Definition at line 181 of file StockholmIO.py.

00181 
00182     def _write_record(self, record):
00183         """Write a single SeqRecord to the file"""
00184         if self._length_of_sequences != len(record.seq):
00185             raise ValueError("Sequences must all be the same length")
00186 
00187         #For the case for stockholm to stockholm, try and use record.name
00188         seq_name = record.id
00189         if record.name is not None:
00190             if "accession" in record.annotations:
00191                 if record.id == record.annotations["accession"]:
00192                     seq_name = record.name
00193 
00194         #In the Stockholm file format, spaces are not allowed in the id
00195         seq_name = seq_name.replace(" ","_")
00196 
00197         if "start" in record.annotations \
00198         and  "end" in record.annotations:
00199             suffix = "/%s-%s" % (str(record.annotations["start"]),
00200                                  str(record.annotations["end"]))
00201             if seq_name[-len(suffix):] != suffix:
00202                 seq_name = "%s/%s-%s" % (seq_name,
00203                                         str(record.annotations["start"]),
00204                                         str(record.annotations["end"]))
00205 
00206         if seq_name in self._ids_written:
00207             raise ValueError("Duplicate record identifier: %s" % seq_name)
00208         self._ids_written.append(seq_name)
00209         self.handle.write("%s %s\n" % (seq_name, record.seq.tostring()))
00210 
00211         #The recommended placement for GS lines (per sequence annotation)
00212         #is above the alignment (as a header block) or just below the
00213         #corresponding sequence.
00214         #
00215         #The recommended placement for GR lines (per sequence per column
00216         #annotation such as secondary structure) is just below the
00217         #corresponding sequence.
00218         #
00219         #We put both just below the corresponding sequence as this allows
00220         #us to write the file using a single pass through the records.
00221 
00222         #AC = Accession
00223         if "accession" in record.annotations:
00224             self.handle.write("#=GS %s AC %s\n" \
00225                 % (seq_name, self.clean(record.annotations["accession"])))
00226         elif record.id:
00227             self.handle.write("#=GS %s AC %s\n" \
00228                 % (seq_name, self.clean(record.id)))
00229         
00230         #DE = description
00231         if record.description:
00232             self.handle.write("#=GS %s DE %s\n" \
00233                 % (seq_name, self.clean(record.description)))
00234 
00235         #DE = database links
00236         for xref in record.dbxrefs:
00237             self.handle.write("#=GS %s DR %s\n" \
00238                 % (seq_name, self.clean(xref)))
00239 
00240         #GS = other per sequence annotation
00241         for key, value in record.annotations.iteritems():
00242             if key in self.pfam_gs_mapping:
00243                 data = self.clean(str(value))
00244                 if data:
00245                     self.handle.write("#=GS %s %s %s\n" \
00246                                       % (seq_name,
00247                                          self.clean(self.pfam_gs_mapping[key]),
00248                                          data))
00249             else:
00250                 #It doesn't follow the PFAM standards, but should we record
00251                 #this data anyway?
00252                 pass
00253 
00254         #GR = per row per column sequence annotation
00255         for key, value in record.letter_annotations.iteritems():
00256             if key in self.pfam_gr_mapping and len(str(value))==len(record.seq):
00257                 data = self.clean(str(value))
00258                 if data:
00259                     self.handle.write("#=GR %s %s %s\n" \
00260                                       % (seq_name,
00261                                          self.clean(self.pfam_gr_mapping[key]),
00262                                          data))
00263             else:
00264                 #It doesn't follow the PFAM standards, but should we record
00265                 #this data anyway?
00266                 pass
        

Here is the call graph for this function:

Use this to write (another) single alignment to an open file.

Note that sequences and their annotation are recorded
together (rather than having a block of annotation followed
by a block of aligned sequences).

Definition at line 155 of file StockholmIO.py.

00155 
00156     def write_alignment(self, alignment):
00157         """Use this to write (another) single alignment to an open file.
00158         
00159         Note that sequences and their annotation are recorded
00160         together (rather than having a block of annotation followed
00161         by a block of aligned sequences).
00162         """
00163         count = len(alignment)
00164         
00165         self._length_of_sequences = alignment.get_alignment_length()
00166         self._ids_written = []
00167         
00168         #NOTE - For now, the alignment object does not hold any per column
00169         #or per alignment annotation - only per sequence.
00170         
00171         if count == 0:
00172             raise ValueError("Must have at least one sequence")
00173         if self._length_of_sequences == 0:
00174             raise ValueError("Non-empty sequences are required")
00175 
00176         self.handle.write("# STOCKHOLM 1.0\n")
00177         self.handle.write("#=GF SQ %i\n" % count)
00178         for record in alignment:
00179             self._write_record(record)
00180         self.handle.write("//\n")


Member Data Documentation

Definition at line 165 of file StockholmIO.py.

Definition at line 164 of file StockholmIO.py.

Initial value:
{"secondary_structure" : "SS",
                       "surface_accessibility" : "SA",
                       "transmembrane" : "TM",
                       "posterior_probability" : "PP",
                       "ligand_binding" : "LI",
                       "active_site" : "AS",
                       "intron" : "IN"}

Definition at line 143 of file StockholmIO.py.

Initial value:
{"organism" : "OS",
                       "organism_classification" : "OC",
                       "look" : "LO"}

Definition at line 151 of file StockholmIO.py.


The documentation for this class was generated from the following file: