Back to index

python-biopython  1.60
Public Member Functions | Public Attributes | Private Member Functions
Bio.SeqIO.SeqXmlIO.SeqXmlWriter Class Reference
Inheritance diagram for Bio.SeqIO.SeqXmlIO.SeqXmlWriter:
Inheritance graph
[legend]
Collaboration diagram for Bio.SeqIO.SeqXmlIO.SeqXmlWriter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def write_header
def write_record
def write_footer

Public Attributes

 xml_generator
 source
 source_version
 species
 ncbiTaxId

Private Member Functions

def _write_species
def _write_description
def _write_seq
def _write_dbxrefs
def _write_properties

Detailed Description

Writes SeqRecords into seqXML file.

SeqXML requires the sequence alphabet be explicitly RNA, DNA or protein,
i.e. an instance or subclass of Bio.Alphapet.RNAAlphabet,
Bio.Alphapet.DNAAlphabet or Bio.Alphapet.ProteinAlphabet.

Definition at line 216 of file SeqXmlIO.py.


Constructor & Destructor Documentation

def Bio.SeqIO.SeqXmlIO.SeqXmlWriter.__init__ (   self,
  handle,
  source = None,
  source_version = None,
  species = None,
  ncbiTaxId = None 
)
Create Object and start the xml generator.

Definition at line 224 of file SeqXmlIO.py.

00224 
00225     def __init__(self, handle,source=None,source_version=None,species=None,ncbiTaxId=None):
00226         """Create Object and start the xml generator."""
00227         
00228         SequentialSequenceWriter.__init__(self, handle)
00229 
00230         self.xml_generator = XMLGenerator(handle, "utf-8")
00231         self.xml_generator.startDocument()
00232         self.source = source
00233         self.source_version = source_version
00234         self.species = species
00235         self.ncbiTaxId = ncbiTaxId
            

Member Function Documentation

def Bio.SeqIO.SeqXmlIO.SeqXmlWriter._write_dbxrefs (   self,
  record 
) [private]
Write all database cross references.

Definition at line 358 of file SeqXmlIO.py.

00358 
00359     def _write_dbxrefs(self,record):
00360         """Write all database cross references."""
00361         if record.dbxrefs != None:
00362             
00363             for dbxref in record.dbxrefs:
00364                 
00365                 if not isinstance(dbxref,basestring):
00366                     raise TypeError("dbxrefs should be of type list of string")
00367                 if dbxref.find(':') < 1:
00368                     raise ValueError("dbxrefs should be in the form ['source:id', 'source:id' ]")
00369                 
00370                 dbsource,dbid = dbxref.split(':',1)
00371                 
00372                 attr = { "source" : dbsource, "id" : dbid }
00373                 self.xml_generator.startElement("DBRef",AttributesImpl(attr))
00374                 self.xml_generator.endElement("DBRef")
00375     
    

Here is the caller graph for this function:

def Bio.SeqIO.SeqXmlIO.SeqXmlWriter._write_description (   self,
  record 
) [private]
Write the description if given.

Definition at line 311 of file SeqXmlIO.py.

00311 
00312     def _write_description(self,record):
00313         """Write the description if given."""
00314         
00315         if record.description:
00316             
00317             if not isinstance(record.description,basestring):
00318                 raise TypeError("Description should be of type string")
00319             
00320             description = record.description
00321             if description == "<unknown description>":
00322                 description = ""
00323             
00324             if len(record.description) > 0:
00325                 self.xml_generator.startElement("description",AttributesImpl( {} ))
00326                 self.xml_generator.characters(description)
00327                 self.xml_generator.endElement("description")
    

Here is the caller graph for this function:

def Bio.SeqIO.SeqXmlIO.SeqXmlWriter._write_properties (   self,
  record 
) [private]
Write all annotations that are key value pairs with values of a primitive type or list of primitive types.

Definition at line 376 of file SeqXmlIO.py.

00376 
00377     def _write_properties(self,record):
00378         """Write all annotations that are key value pairs with values of a primitive type or list of primitive types."""
00379         
00380         for key,value in record.annotations.items():
00381     
00382             if key not in ("organism","ncbi_taxid","source"):
00383             
00384                 if value == None:
00385                     
00386                     attr = { "name" : key }
00387                     self.xml_generator.startElement("property",AttributesImpl(attr))
00388                     self.xml_generator.endElement("property")
00389                 
00390                 elif isinstance(value,list):
00391                     
00392                     for v in value:
00393                         if isinstance(value,(int,float,basestring)):
00394                             attr = { "name" : key , "value" : v }
00395                             self.xml_generator.startElement("property",AttributesImpl(attr))
00396                             self.xml_generator.endElement("property")
00397                     
00398                 elif isinstance(value,(int,float,basestring)):
00399                 
00400                     attr = { "name" : key , "value" : str(value) }
00401                     self.xml_generator.startElement("property",AttributesImpl(attr))
                    self.xml_generator.endElement("property")

Here is the caller graph for this function:

def Bio.SeqIO.SeqXmlIO.SeqXmlWriter._write_seq (   self,
  record 
) [private]
Write the sequence.

Note that SeqXML requires a DNA, RNA or protein alphabet.

Definition at line 328 of file SeqXmlIO.py.

00328 
00329     def _write_seq(self,record):
00330         """Write the sequence.
00331         
00332         Note that SeqXML requires a DNA, RNA or protein alphabet.
00333         """
00334         
00335         if isinstance(record.seq,UnknownSeq):
00336             raise TypeError("Sequence type is UnknownSeq but SeqXML requires sequence")
00337         
00338         seq = record.seq.tostring()
00339         
00340         if not len(seq) > 0:
00341             raise ValueError("The sequence length should be greater than 0")
00342         
00343         #Get the base alphabet (underneath any Gapped or StopCodon encoding)
00344         alphabet = Alphabet._get_base_alphabet(record.seq.alphabet)
00345         if isinstance(alphabet,Alphabet.RNAAlphabet):
00346             seqElem = "RNAseq"
00347         elif isinstance(alphabet,Alphabet.DNAAlphabet):
00348             seqElem = "DNAseq"
00349         elif isinstance(alphabet,Alphabet.ProteinAlphabet):
00350             seqElem = "AAseq"
00351         else:
00352             raise ValueError("Need a DNA, RNA or Protein alphabet")
00353         
00354         self.xml_generator.startElement(seqElem,AttributesImpl( {} ))
00355         self.xml_generator.characters(seq)
00356         self.xml_generator.endElement(seqElem)    
00357         
    

Here is the caller graph for this function:

def Bio.SeqIO.SeqXmlIO.SeqXmlWriter._write_species (   self,
  record 
) [private]
Write the species if given.

Definition at line 292 of file SeqXmlIO.py.

00292 
00293     def _write_species(self,record):
00294         """Write the species if given."""
00295         
00296         if "organism" in record.annotations and "ncbi_taxid" in record.annotations:
00297             
00298             if not isinstance(record.annotations["organism"],basestring):
00299                 raise TypeError("organism should be of type string")
00300             
00301             if not isinstance(record.annotations["ncbi_taxid"],(basestring,int)):
00302                 raise TypeError("ncbiTaxID should be of type string or int")
00303             
00304             #The local species definition is only written if it differs from the global species definition
00305             if record.annotations["organism"] != self.species or record.annotations["ncbi_taxid"] != self.ncbiTaxId:
00306             
00307                 attr = { "name" : record.annotations["organism"], "ncbiTaxID" :record.annotations["ncbi_taxid"] }   
00308                 self.xml_generator.startElement("species",AttributesImpl(attr))
00309                 self.xml_generator.endElement("species")
00310             
            

Here is the caller graph for this function:

Close the root node and finish the XML document.

Definition at line 284 of file SeqXmlIO.py.

00284 
00285     def write_footer(self):
00286         """Close the root node and finish the XML document."""
00287         
00288         SequentialSequenceWriter.write_footer(self)
00289         
00290         self.xml_generator.endElement("seqXML")
00291         self.xml_generator.endDocument()
    
Write root node with document metadata.

Definition at line 236 of file SeqXmlIO.py.

00236 
00237     def write_header(self):
00238         """Write root node with document metadata."""
00239         SequentialSequenceWriter.write_header(self)
00240         
00241         attrs = {"xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
00242                  "xsi:noNamespaceSchemaLocation":"http://www.seqxml.org/0.4/seqxml.xsd",
00243                  "seqXMLversion":"0.4"}
00244         
00245         if self.source != None:
00246             attrs["source"] = self.source
00247         if self.source_version != None:
00248             attrs["sourceVersion"] = self.source_ersion
00249         if self.species != None:
00250             if not isinstance(species,basestring):
00251                 raise TypeError("species should be of type string")
00252             attrs["speciesName"] = self.species
00253         if self.ncbiTaxId != None:
00254             if not isinstance(self.ncbiTaxId,(basestring,int)):
00255                 raise TypeError("ncbiTaxID should be of type string or int")
00256             attrs["ncbiTaxID"] = self.ncbiTaxId
00257         
00258         self.xml_generator.startElement("seqXML", AttributesImpl(attrs))
00259         
    

Here is the caller graph for this function:

def Bio.SeqIO.SeqXmlIO.SeqXmlWriter.write_record (   self,
  record 
)
Write one record.

Definition at line 260 of file SeqXmlIO.py.

00260 
00261     def write_record(self, record):
00262         """Write one record."""
00263         
00264         if not record.id or record.id == "<unknown id>":
00265             raise ValueError("SeqXML requires identifier")
00266         
00267         if not isinstance(record.id,basestring):
00268             raise TypeError("Identifier should be of type string")
00269         
00270         attrb = {"id" : record.id}
00271         
00272         if "source" in record.annotations and self.source != record.annotations["source"]:
00273             if not isinstance(record.annotations["source"],basestring):
00274                 raise TypeError("source should be of type string")
00275             attrb["source"] = record.annotations["source"]
00276         
00277         self.xml_generator.startElement("entry", AttributesImpl(attrb))
00278         self._write_species(record)
00279         self._write_description(record)
00280         self._write_seq(record)
00281         self._write_dbxrefs(record)
00282         self._write_properties(record)
00283         self.xml_generator.endElement("entry")
    

Here is the call graph for this function:


Member Data Documentation

Definition at line 234 of file SeqXmlIO.py.

Definition at line 231 of file SeqXmlIO.py.

Definition at line 232 of file SeqXmlIO.py.

Definition at line 233 of file SeqXmlIO.py.

Definition at line 229 of file SeqXmlIO.py.


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