<?xml version="1.0" encoding="ascii"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Bio.SeqIO</title> <link rel="stylesheet" href="epydoc.css" type="text/css" /> <script type="text/javascript" src="epydoc.js"></script> </head> <body bgcolor="white" text="black" link="blue" vlink="#204080" alink="#204080"> <!-- ==================== NAVIGATION BAR ==================== --> <table class="navbar" border="0" width="100%" cellpadding="0" bgcolor="#a0c0ff" cellspacing="0"> <tr valign="middle"> <!-- Tree link --> <th> <a href="module-tree.html">Trees</a> </th> <!-- Index link --> <th> <a href="identifier-index.html">Indices</a> </th> <!-- Help link --> <th> <a href="help.html">Help</a> </th> <th class="navbar" width="100%"></th> </tr> </table> <table width="100%" cellpadding="0" cellspacing="0"> <tr valign="top"> <td width="100%"> <span class="breadcrumbs"> <a href="Bio-module.html">Package Bio</a> :: Package SeqIO </span> </td> <td> <table cellpadding="0" cellspacing="0"> <!-- hide/show private --> <tr><td align="right"><span class="options">[<a href="javascript:void(0);" class="privatelink" onclick="toggle_private();">hide private</a>]</span></td></tr> <tr><td align="right"><span class="options" >[<a href="frames.html" target="_top">frames</a >] | <a href="Bio.SeqIO-module.html" target="_top">no frames</a>]</span></td></tr> </table> </td> </tr> </table> <!-- ==================== PACKAGE DESCRIPTION ==================== --> <h1 class="epydoc">Package SeqIO</h1><p class="nomargin-top"><span class="codelink"><a href="Bio.SeqIO-pysrc.html">source code</a></span></p> <p>Sequence input/output as SeqRecord objects.</p> <p>Bio.SeqIO is also documented at <a href="http://biopython.org/wiki/SeqIO" target="_top">http://biopython.org/wiki/SeqIO</a> and by a whole chapter in our tutorial:</p> <ul> <li> <a href="http://biopython.org/DIST/docs/tutorial/Tutorial.html" target="_top">http://biopython.org/DIST/docs/tutorial/Tutorial.html</a> </li> <li> <a href="http://biopython.org/DIST/docs/tutorial/Tutorial.pdf" target="_top">http://biopython.org/DIST/docs/tutorial/Tutorial.pdf</a> </li> </ul> <h1 class="heading">Input</h1> <p>The main function is Bio.SeqIO.parse(...) which takes an input file handle (or in recent versions of Biopython alternatively a filename as a string), and format string. This returns an iterator giving SeqRecord objects:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span><span class="py-keyword">for</span> record <span class="py-keyword">in</span> SeqIO.parse(<span class="py-string">"Fasta/f002"</span>, <span class="py-string">"fasta"</span>): <span class="py-more">... </span> <span class="py-keyword">print</span> record.id, len(record) <span class="py-output">gi|1348912|gb|G26680|G26680 633</span> <span class="py-output">gi|1348917|gb|G26685|G26685 413</span> <span class="py-output">gi|1592936|gb|G29385|G29385 471</span></pre> <p>Note that the parse() function will invoke the relevant parser for the format with its default settings. You may want more control, in which case you need to create a format specific sequence iterator directly.</p> <h1 class="heading">Input - Single Records</h1> <p>If you expect your file to contain one-and-only-one record, then we provide the following 'helper' function which will return a single SeqRecord, or raise an exception if there are no records or more than one record:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>record = SeqIO.read(<span class="py-string">"Fasta/f001"</span>, <span class="py-string">"fasta"</span>) <span class="py-prompt">>>> </span><span class="py-keyword">print</span> record.id, len(record) <span class="py-output">gi|3318709|pdb|1A91| 79</span></pre> <p>This style is useful when you expect a single record only (and would consider multiple records an error). For example, when dealing with GenBank files for bacterial genomes or chromosomes, there is normally only a single record. Alternatively, use this with a handle when downloading a single record from the internet.</p> <p>However, if you just want the first record from a file containing multiple record, use the iterator's next() method:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>record = SeqIO.parse(<span class="py-string">"Fasta/f002"</span>, <span class="py-string">"fasta"</span>).next() <span class="py-prompt">>>> </span><span class="py-keyword">print</span> record.id, len(record) <span class="py-output">gi|1348912|gb|G26680|G26680 633</span></pre> <p>The above code will work as long as the file contains at least one record. Note that if there is more than one record, the remaining records will be silently ignored.</p> <h1 class="heading">Input - Multiple Records</h1> <p>For non-interlaced files (e.g. Fasta, GenBank, EMBL) with multiple records using a sequence iterator can save you a lot of memory (RAM). There is less benefit for interlaced file formats (e.g. most multiple alignment file formats). However, an iterator only lets you access the records one by one.</p> <p>If you want random access to the records by number, turn this into a list:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>records = list(SeqIO.parse(<span class="py-string">"Fasta/f002"</span>, <span class="py-string">"fasta"</span>)) <span class="py-prompt">>>> </span>len(records) <span class="py-output">3</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> records[1].id <span class="py-output">gi|1348917|gb|G26685|G26685</span></pre> <p>If you want random access to the records by a key such as the record id, turn the iterator into a dictionary:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>record_dict = SeqIO.to_dict(SeqIO.parse(<span class="py-string">"Fasta/f002"</span>, <span class="py-string">"fasta"</span>)) <span class="py-prompt">>>> </span>len(record_dict) <span class="py-output">3</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> len(record_dict[<span class="py-string">"gi|1348917|gb|G26685|G26685"</span>]) <span class="py-output">413</span></pre> <p>However, using list() or the to_dict() function will load all the records into memory at once, and therefore is not possible on very large files. Instead, for *some* file formats Bio.SeqIO provides an indexing approach providing dictionary like access to any record. For example,</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>record_dict = SeqIO.index(<span class="py-string">"Fasta/f002"</span>, <span class="py-string">"fasta"</span>) <span class="py-prompt">>>> </span>len(record_dict) <span class="py-output">3</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> len(record_dict[<span class="py-string">"gi|1348917|gb|G26685|G26685"</span>]) <span class="py-output">413</span></pre> <p>Many but not all of the supported input file formats can be indexed like this. For example "fasta", "fastq", "qual" and even the binary format "sff" work, but alignment formats like "phylip", "clustalw" and "nexus" will not.</p> <p>In most cases you can also use SeqIO.index to get the record from the file as a raw string (not a SeqRecord). This can be useful for example to extract a sub-set of records from a file where SeqIO cannot output the file format (e.g. the plain text SwissProt format, "swiss") or where it is important to keep the output 100% identical to the input). For example,</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>record_dict = SeqIO.index(<span class="py-string">"Fasta/f002"</span>, <span class="py-string">"fasta"</span>) <span class="py-prompt">>>> </span>len(record_dict) <span class="py-output">3</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> record_dict.get_raw(<span class="py-string">"gi|1348917|gb|G26685|G26685"</span>).decode() <span class="py-output">>gi|1348917|gb|G26685|G26685 human STS STS_D11734.</span> <span class="py-output">CGGAGCCAGCGAGCATATGCTGCATGAGGACCTTTCTATCTTACATTATGGCTGGGAATCTTACTCTTTC</span> <span class="py-output">ATCTGATACCTTGTTCAGATTTCAAAATAGTTGTAGCCTTATCCTGGTTTTACAGATGTGAAACTTTCAA</span> <span class="py-output">GAGATTTACTGACTTTCCTAGAATAGTTTCTCTACTGGAAACCTGATGCTTTTATAAGCCATTGTGATTA</span> <span class="py-output">GGATGACTGTTACAGGCTTAGCTTTGTGTGAAANCCAGTCACCTTTCTCCTAGGTAATGAGTAGTGCTGT</span> <span class="py-output">TCATATTACTNTAAGTTCTATAGCATACTTGCNATCCTTTANCCATGCTTATCATANGTACCATTTGAGG</span> <span class="py-output">AATTGNTTTGCCCTTTTGGGTTTNTTNTTGGTAAANNNTTCCCGGGTGGGGGNGGTNNNGAAA</span> <span class="py-output"><BLANKLINE></span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> record_dict[<span class="py-string">"gi|1348917|gb|G26685|G26685"</span>].format(<span class="py-string">"fasta"</span>) <span class="py-output">>gi|1348917|gb|G26685|G26685 human STS STS_D11734.</span> <span class="py-output">CGGAGCCAGCGAGCATATGCTGCATGAGGACCTTTCTATCTTACATTATGGCTGGGAATC</span> <span class="py-output">TTACTCTTTCATCTGATACCTTGTTCAGATTTCAAAATAGTTGTAGCCTTATCCTGGTTT</span> <span class="py-output">TACAGATGTGAAACTTTCAAGAGATTTACTGACTTTCCTAGAATAGTTTCTCTACTGGAA</span> <span class="py-output">ACCTGATGCTTTTATAAGCCATTGTGATTAGGATGACTGTTACAGGCTTAGCTTTGTGTG</span> <span class="py-output">AAANCCAGTCACCTTTCTCCTAGGTAATGAGTAGTGCTGTTCATATTACTNTAAGTTCTA</span> <span class="py-output">TAGCATACTTGCNATCCTTTANCCATGCTTATCATANGTACCATTTGAGGAATTGNTTTG</span> <span class="py-output">CCCTTTTGGGTTTNTTNTTGGTAAANNNTTCCCGGGTGGGGGNGGTNNNGAAA</span> <span class="py-output"><BLANKLINE></span></pre> <p>Here the original file and what Biopython would output differ in the line wrapping. Also note that under Python 3, the get_raw method will return a bytes string, hence the use of decode to turn it into a (unicode) string. This is uncessary on Python 2.</p> <h1 class="heading">Input - Alignments</h1> <p>You can read in alignment files as alignment objects using Bio.AlignIO. Alternatively, reading in an alignment file format via Bio.SeqIO will give you a SeqRecord for each row of each alignment:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span><span class="py-keyword">for</span> record <span class="py-keyword">in</span> SeqIO.parse(<span class="py-string">"Clustalw/hedgehog.aln"</span>, <span class="py-string">"clustal"</span>): <span class="py-more">... </span> <span class="py-keyword">print</span> record.id, len(record) <span class="py-output">gi|167877390|gb|EDS40773.1| 447</span> <span class="py-output">gi|167234445|ref|NP_001107837. 447</span> <span class="py-output">gi|74100009|gb|AAZ99217.1| 447</span> <span class="py-output">gi|13990994|dbj|BAA33523.2| 447</span> <span class="py-output">gi|56122354|gb|AAV74328.1| 447</span></pre> <h1 class="heading">Output</h1> <p>Use the function Bio.SeqIO.write(...), which takes a complete set of SeqRecord objects (either as a list, or an iterator), an output file handle (or in recent versions of Biopython an output filename as a string) and of course the file format:</p> <pre class="literalblock"> from Bio import SeqIO records = ... SeqIO.write(records, "example.faa", "fasta") </pre> <p>Or, using a handle:</p> <pre class="literalblock"> from Bio import SeqIO records = ... handle = open("example.faa", "w") SeqIO.write(records, handle, "fasta") handle.close() </pre> <p>You are expected to call this function once (with all your records) and if using a handle, make sure you close it to flush the data to the hard disk.</p> <h1 class="heading">Output - Advanced</h1> <p>The effect of calling write() multiple times on a single file will vary depending on the file format, and is best avoided unless you have a strong reason to do so.</p> <p>If you give a filename, then each time you call write() the existing file will be overwriten. For sequential files formats (e.g. fasta, genbank) each "record block" holds a single sequence. For these files it would probably be safe to call write() multiple times by re-using the same handle.</p> <p>However, trying this for certain alignment formats (e.g. phylip, clustal, stockholm) would have the effect of concatenating several multiple sequence alignments together. Such files are created by the PHYLIP suite of programs for bootstrap analysis, but it is clearer to do this via Bio.AlignIO instead.</p> <h1 class="heading">Conversion</h1> <p>The Bio.SeqIO.convert(...) function allows an easy interface for simple file format conversions. Additionally, it may use file format specific optimisations so this should be the fastest way too.</p> <p>In general however, you can combine the Bio.SeqIO.parse(...) function with the Bio.SeqIO.write(...) function for sequence file conversion. Using generator expressions or generator functions provides a memory efficient way to perform filtering or other extra operations as part of the process.</p> <h1 class="heading">File Formats</h1> <p>When specifying the file format, use lowercase strings. The same format names are also used in Bio.AlignIO and include the following:</p> <ul> <li> abif - Applied Biosystem's sequencing trace format </li> <li> ace - Reads the contig sequences from an ACE assembly file. </li> <li> embl - The EMBL flat file format. Uses Bio.GenBank internally. </li> <li> fasta - The generic sequence file format where each record starts with an identifer line starting with a ">" character, followed by lines of sequence. </li> <li> fastq - A "FASTA like" format used by Sanger which also stores PHRED sequence quality values (with an ASCII offset of 33). </li> <li> fastq-sanger - An alias for "fastq" for consistency with BioPerl and EMBOSS </li> <li> fastq-solexa - Original Solexa/Illumnia variant of the FASTQ format which encodes Solexa quality scores (not PHRED quality scores) with an ASCII offset of 64. </li> <li> fastq-illumina - Solexa/Illumina 1.3 to 1.7 variant of the FASTQ format which encodes PHRED quality scores with an ASCII offset of 64 (not 33). Note as of version 1.8 of the CASAVA pipeline Illumina will produce FASTQ files using the standard Sanger encoding. </li> <li> genbank - The GenBank or GenPept flat file format. </li> <li> gb - An alias for "genbank", for consistency with NCBI Entrez Utilities </li> <li> ig - The IntelliGenetics file format, apparently the same as the MASE alignment format. </li> <li> imgt - An EMBL like format from IMGT where the feature tables are more indented to allow for longer feature types. </li> <li> phd - Output from PHRED, used by PHRAP and CONSED for input. </li> <li> pir - A "FASTA like" format introduced by the National Biomedical Research Foundation (NBRF) for the Protein Information Resource (PIR) database, now part of UniProt. </li> <li> seqxml - SeqXML, simple XML format described in Schmitt et al (2011). </li> <li> sff - Standard Flowgram Format (SFF), typical output from Roche 454. </li> <li> sff-trim - Standard Flowgram Format (SFF) with given trimming applied. </li> <li> swiss - Plain text Swiss-Prot aka UniProt format. </li> <li> tab - Simple two column tab separated sequence files, where each line holds a record's identifier and sequence. For example, this is used as by Aligent's eArray software when saving microarray probes in a minimal tab delimited text file. </li> <li> qual - A "FASTA like" format holding PHRED quality values from sequencing DNA, but no actual sequences (usually provided in separate FASTA files). </li> <li> uniprot-xml - The UniProt XML format (replacement for the SwissProt plain text format which we call "swiss") </li> </ul> <p>Note that while Bio.SeqIO can read all the above file formats, it cannot write to all of them.</p> <p>You can also use any file format supported by Bio.AlignIO, such as "nexus", "phlip" and "stockholm", which gives you access to the individual sequences making up each alignment as SeqRecords.</p> <!-- ==================== SUBMODULES ==================== --> <a name="section-Submodules"></a> <table class="summary" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr bgcolor="#70b0f0" class="table-header"> <td colspan="2" class="table-header"> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr valign="top"> <td align="left"><span class="table-header">Submodules</span></td> <td align="right" valign="top" ><span class="options">[<a href="#section-Submodules" class="privatelink" onclick="toggle_private();" >hide private</a>]</span></td> </tr> </table> </td> </tr> <tr><td class="summary"> <ul class="nomargin"> <li> <strong class="uidlink"><a href="Bio.SeqIO.AbiIO-module.html">Bio.SeqIO.AbiIO</a></strong>: <em class="summary">Bio.SeqIO parser for the ABI format.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.AceIO-module.html">Bio.SeqIO.AceIO</a></strong>: <em class="summary">Bio.SeqIO support for the "ace" file format.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.FastaIO-module.html">Bio.SeqIO.FastaIO</a></strong>: <em class="summary">Bio.SeqIO support for the "fasta" (aka FastA or Pearson) file format.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.IgIO-module.html">Bio.SeqIO.IgIO</a></strong>: <em class="summary">Bio.SeqIO support for the "ig" (IntelliGenetics or MASE) file format.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.InsdcIO-module.html">Bio.SeqIO.InsdcIO</a></strong>: <em class="summary">Bio.SeqIO support for the "genbank" and "embl" file formats.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.Interfaces-module.html">Bio.SeqIO.Interfaces</a></strong>: <em class="summary">Bio.SeqIO support module (not for general use).</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.PhdIO-module.html">Bio.SeqIO.PhdIO</a></strong>: <em class="summary">Bio.SeqIO support for the "phd" file format.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.PirIO-module.html">Bio.SeqIO.PirIO</a></strong>: <em class="summary">Bio.SeqIO support for the "pir" (aka PIR or NBRF) file format.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.QualityIO-module.html">Bio.SeqIO.QualityIO</a></strong>: <em class="summary">Bio.SeqIO support for the FASTQ and QUAL file formats.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.SeqXmlIO-module.html">Bio.SeqIO.SeqXmlIO</a></strong>: <em class="summary">Bio.SeqIO support for the "seqxml" file format, SeqXML.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.SffIO-module.html">Bio.SeqIO.SffIO</a></strong>: <em class="summary">Bio.SeqIO support for the binary Standard Flowgram Format (SFF) file format.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.SwissIO-module.html">Bio.SeqIO.SwissIO</a></strong>: <em class="summary">Bio.SeqIO support for the "swiss" (aka SwissProt/UniProt) file format.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.TabIO-module.html">Bio.SeqIO.TabIO</a></strong>: <em class="summary">Bio.SeqIO support for the "tab" (simple tab separated) file format.</em> </li> <li> <strong class="uidlink"><a href="Bio.SeqIO.UniprotIO-module.html">Bio.SeqIO.UniprotIO</a></strong>: <em class="summary">Bio.SeqIO support for the "uniprot-xml" file format.</em> </li> <li class="private"> <strong class="uidlink"><a href="Bio.SeqIO._convert-module.html">Bio.SeqIO._convert</a></strong>: <em class="summary">Optimised sequence conversion code (PRIVATE).</em> </li> <li class="private"> <strong class="uidlink"><a href="Bio.SeqIO._index-module.html">Bio.SeqIO._index</a></strong>: <em class="summary">Dictionary like indexing of sequence files (PRIVATE).</em> </li> </ul></td></tr> </table> <br /> <!-- ==================== FUNCTIONS ==================== --> <a name="section-Functions"></a> <table class="summary" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr bgcolor="#70b0f0" class="table-header"> <td colspan="2" class="table-header"> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr valign="top"> <td align="left"><span class="table-header">Functions</span></td> <td align="right" valign="top" ><span class="options">[<a href="#section-Functions" class="privatelink" onclick="toggle_private();" >hide private</a>]</span></td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="Bio.SeqIO-module.html#write" class="summary-sig-name">write</a>(<span class="summary-sig-arg">sequences</span>, <span class="summary-sig-arg">handle</span>, <span class="summary-sig-arg">format</span>)</span><br /> Write complete set of sequences to a file.</td> <td align="right" valign="top"> <span class="codelink"><a href="Bio.SeqIO-pysrc.html#write">source code</a></span> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="Bio.SeqIO-module.html#parse" class="summary-sig-name">parse</a>(<span class="summary-sig-arg">handle</span>, <span class="summary-sig-arg">format</span>, <span class="summary-sig-arg">alphabet</span>=<span class="summary-sig-default">None</span>)</span><br /> Turns a sequence file into an iterator returning SeqRecords.</td> <td align="right" valign="top"> <span class="codelink"><a href="Bio.SeqIO-pysrc.html#parse">source code</a></span> </td> </tr> </table> </td> </tr> <tr class="private"> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a name="_iterate_via_AlignIO"></a><span class="summary-sig-name">_iterate_via_AlignIO</span>(<span class="summary-sig-arg">handle</span>, <span class="summary-sig-arg">format</span>, <span class="summary-sig-arg">alphabet</span>)</span><br /> Iterate over all records in several alignments (PRIVATE).</td> <td align="right" valign="top"> <span class="codelink"><a href="Bio.SeqIO-pysrc.html#_iterate_via_AlignIO">source code</a></span> </td> </tr> </table> </td> </tr> <tr class="private"> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a name="_force_alphabet"></a><span class="summary-sig-name">_force_alphabet</span>(<span class="summary-sig-arg">record_iterator</span>, <span class="summary-sig-arg">alphabet</span>)</span><br /> Iterate over records, over-riding the alphabet (PRIVATE).</td> <td align="right" valign="top"> <span class="codelink"><a href="Bio.SeqIO-pysrc.html#_force_alphabet">source code</a></span> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="Bio.SeqIO-module.html#read" class="summary-sig-name">read</a>(<span class="summary-sig-arg">handle</span>, <span class="summary-sig-arg">format</span>, <span class="summary-sig-arg">alphabet</span>=<span class="summary-sig-default">None</span>)</span><br /> Turns a sequence file into a single SeqRecord.</td> <td align="right" valign="top"> <span class="codelink"><a href="Bio.SeqIO-pysrc.html#read">source code</a></span> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="Bio.SeqIO-module.html#to_dict" class="summary-sig-name">to_dict</a>(<span class="summary-sig-arg">sequences</span>, <span class="summary-sig-arg">key_function</span>=<span class="summary-sig-default">None</span>)</span><br /> Turns a sequence iterator or list into a dictionary.</td> <td align="right" valign="top"> <span class="codelink"><a href="Bio.SeqIO-pysrc.html#to_dict">source code</a></span> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="Bio.SeqIO-module.html#index" class="summary-sig-name">index</a>(<span class="summary-sig-arg">filename</span>, <span class="summary-sig-arg">format</span>, <span class="summary-sig-arg">alphabet</span>=<span class="summary-sig-default">None</span>, <span class="summary-sig-arg">key_function</span>=<span class="summary-sig-default">None</span>)</span><br /> Indexes a sequence file and returns a dictionary like object.</td> <td align="right" valign="top"> <span class="codelink"><a href="Bio.SeqIO-pysrc.html#index">source code</a></span> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="Bio.SeqIO-module.html#index_db" class="summary-sig-name">index_db</a>(<span class="summary-sig-arg">index_filename</span>, <span class="summary-sig-arg">filenames</span>=<span class="summary-sig-default">None</span>, <span class="summary-sig-arg">format</span>=<span class="summary-sig-default">None</span>, <span class="summary-sig-arg">alphabet</span>=<span class="summary-sig-default">None</span>, <span class="summary-sig-arg">key_function</span>=<span class="summary-sig-default">None</span>)</span><br /> Index several sequence files and return a dictionary like object.</td> <td align="right" valign="top"> <span class="codelink"><a href="Bio.SeqIO-pysrc.html#index_db">source code</a></span> </td> </tr> </table> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="Bio.SeqIO-module.html#convert" class="summary-sig-name">convert</a>(<span class="summary-sig-arg">in_file</span>, <span class="summary-sig-arg">in_format</span>, <span class="summary-sig-arg">out_file</span>, <span class="summary-sig-arg">out_format</span>, <span class="summary-sig-arg">alphabet</span>=<span class="summary-sig-default">None</span>)</span><br /> Convert between two sequence file formats, return number of records.</td> <td align="right" valign="top"> <span class="codelink"><a href="Bio.SeqIO-pysrc.html#convert">source code</a></span> </td> </tr> </table> </td> </tr> <tr class="private"> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><span class="summary-sig"><a href="Bio.SeqIO-module.html#_test" class="summary-sig-name" onclick="show_private();">_test</a>()</span><br /> Run the Bio.SeqIO module's doctests.</td> <td align="right" valign="top"> <span class="codelink"><a href="Bio.SeqIO-pysrc.html#_test">source code</a></span> </td> </tr> </table> </td> </tr> </table> <!-- ==================== VARIABLES ==================== --> <a name="section-Variables"></a> <table class="summary" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr bgcolor="#70b0f0" class="table-header"> <td colspan="2" class="table-header"> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr valign="top"> <td align="left"><span class="table-header">Variables</span></td> <td align="right" valign="top" ><span class="options">[<a href="#section-Variables" class="privatelink" onclick="toggle_private();" >hide private</a>]</span></td> </tr> </table> </td> </tr> <tr class="private"> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <a href="Bio.SeqIO-module.html#_FormatToIterator" class="summary-name" onclick="show_private();">_FormatToIterator</a> = <code title="{"fasta": FastaIO.FastaIterator, "gb": InsdcIO.GenBankIterator, "genba\ nk": InsdcIO.GenBankIterator, "genbank-cds": InsdcIO.GenBankCdsFeature\ Iterator, "embl": InsdcIO.EmblIterator, "embl-cds": InsdcIO.EmblCdsFea\ tureIterator, "imgt": InsdcIO.ImgtIterator, "ig": IgIO.IgIterator, "sw\ iss": SwissIO.SwissIterator, "phd": PhdIO.PhdIterator, "ace": AceIO.Ac\ eIterator, "tab": TabIO.TabIterator, "pir": PirIO.PirIterator, "fastq"\ : QualityIO.FastqPhredIterator, "fastq-sanger": QualityIO.FastqPhredIt\ erator, "fastq-solexa": QualityIO.FastqSolexaIterator, "fastq-illumina\ ...">{"fasta": FastaIO.FastaIterator, "gb": Ins<code class="variable-ellipsis">...</code></code> </td> </tr> <tr class="private"> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <a href="Bio.SeqIO-module.html#_FormatToWriter" class="summary-name" onclick="show_private();">_FormatToWriter</a> = <code title="{'embl': <class 'Bio.SeqIO.InsdcIO.EmblWriter'>, 'fasta': <class 'Bio.SeqIO.FastaIO.FastaWriter'>, 'fastq': <class 'Bio.SeqIO.QualityIO.FastqPhredWriter'>, 'fastq-illumina': <class 'Bio.SeqIO.QualityIO.FastqIlluminaWriter'>, 'fastq-sanger': <class 'Bio.SeqIO.QualityIO.FastqPhredWriter'>, 'fastq-solexa': <class 'Bio.SeqIO.QualityIO.FastqSolexaWriter'>, 'gb': <class 'Bio.SeqIO.InsdcIO.GenBankWriter'>, 'genbank': <class 'Bio.SeqIO.InsdcIO.GenBankWriter'>, ..."><code class="variable-group">{</code><code class="variable-quote">'</code><code class="variable-string">embl</code><code class="variable-quote">'</code><code class="variable-op">: </code><class 'Bio.SeqIO.InsdcIO.EmblWrite<code class="variable-ellipsis">...</code></code> </td> </tr> <tr class="private"> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <a name="_BinaryFormats"></a><span class="summary-name">_BinaryFormats</span> = <code title="['sff', 'sff-trim', 'abi', 'abi-trim']"><code class="variable-group">[</code><code class="variable-quote">'</code><code class="variable-string">sff</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">sff-trim</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">abi</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">abi-trim</code><code class="variable-quote">'</code><code class="variable-group">]</code></code> </td> </tr> <tr> <td width="15%" align="right" valign="top" class="summary"> <span class="summary-type"> </span> </td><td class="summary"> <a name="__package__"></a><span class="summary-name">__package__</span> = <code title="'Bio.SeqIO'"><code class="variable-quote">'</code><code class="variable-string">Bio.SeqIO</code><code class="variable-quote">'</code></code> </td> </tr> </table> <!-- ==================== FUNCTION DETAILS ==================== --> <a name="section-FunctionDetails"></a> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr bgcolor="#70b0f0" class="table-header"> <td colspan="2" class="table-header"> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr valign="top"> <td align="left"><span class="table-header">Function Details</span></td> <td align="right" valign="top" ><span class="options">[<a href="#section-FunctionDetails" class="privatelink" onclick="toggle_private();" >hide private</a>]</span></td> </tr> </table> </td> </tr> </table> <a name="write"></a> <div> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">write</span>(<span class="sig-arg">sequences</span>, <span class="sig-arg">handle</span>, <span class="sig-arg">format</span>)</span> </h3> </td><td align="right" valign="top" ><span class="codelink"><a href="Bio.SeqIO-pysrc.html#write">source code</a></span> </td> </tr></table> <p>Write complete set of sequences to a file.</p> <ul> <li> sequences - A list (or iterator) of SeqRecord objects, or (if using Biopython 1.54 or later) a single SeqRecord. </li> <li> handle - File handle object to write to, or filename as string (note older versions of Biopython only took a handle). </li> <li> format - lower case string describing the file format to write. </li> </ul> <p>You should close the handle after calling this function.</p> <p>Returns the number of records written (as an integer).</p> <dl class="fields"> </dl> </td></tr></table> </div> <a name="parse"></a> <div> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">parse</span>(<span class="sig-arg">handle</span>, <span class="sig-arg">format</span>, <span class="sig-arg">alphabet</span>=<span class="sig-default">None</span>)</span> </h3> </td><td align="right" valign="top" ><span class="codelink"><a href="Bio.SeqIO-pysrc.html#parse">source code</a></span> </td> </tr></table> <p>Turns a sequence file into an iterator returning SeqRecords.</p> <ul> <li> handle - handle to the file, or the filename as a string (note older verions of Biopython only took a handle). </li> <li> format - lower case string describing the file format. </li> <li> alphabet - optional Alphabet object, useful when the sequence type cannot be automatically inferred from the file itself (e.g. format="fasta" or "tab") </li> </ul> <p>Typical usage, opening a file to read in, and looping over the record(s):</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>filename = <span class="py-string">"Fasta/sweetpea.nu"</span> <span class="py-prompt">>>> </span><span class="py-keyword">for</span> record <span class="py-keyword">in</span> SeqIO.parse(filename, <span class="py-string">"fasta"</span>): <span class="py-more">... </span> <span class="py-keyword">print</span> <span class="py-string">"ID"</span>, record.id <span class="py-more">... </span> <span class="py-keyword">print</span> <span class="py-string">"Sequence length"</span>, len(record) <span class="py-more">... </span> <span class="py-keyword">print</span> <span class="py-string">"Sequence alphabet"</span>, record.seq.alphabet <span class="py-output">ID gi|3176602|gb|U78617.1|LOU78617</span> <span class="py-output">Sequence length 309</span> <span class="py-output">Sequence alphabet SingleLetterAlphabet()</span></pre> <p>For file formats like FASTA where the alphabet cannot be determined, it may be useful to specify the alphabet explicitly:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio.Alphabet <span class="py-keyword">import</span> generic_dna <span class="py-prompt">>>> </span>filename = <span class="py-string">"Fasta/sweetpea.nu"</span> <span class="py-prompt">>>> </span><span class="py-keyword">for</span> record <span class="py-keyword">in</span> SeqIO.parse(filename, <span class="py-string">"fasta"</span>, generic_dna): <span class="py-more">... </span> <span class="py-keyword">print</span> <span class="py-string">"ID"</span>, record.id <span class="py-more">... </span> <span class="py-keyword">print</span> <span class="py-string">"Sequence length"</span>, len(record) <span class="py-more">... </span> <span class="py-keyword">print</span> <span class="py-string">"Sequence alphabet"</span>, record.seq.alphabet <span class="py-output">ID gi|3176602|gb|U78617.1|LOU78617</span> <span class="py-output">Sequence length 309</span> <span class="py-output">Sequence alphabet DNAAlphabet()</span></pre> <p>If you have a string 'data' containing the file contents, you must first turn this into a handle in order to parse it:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span>data = <span class="py-string">">Alpha\nACCGGATGTA\n>Beta\nAGGCTCGGTTA\n"</span> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span><span class="py-keyword">from</span> StringIO <span class="py-keyword">import</span> StringIO <span class="py-prompt">>>> </span><span class="py-keyword">for</span> record <span class="py-keyword">in</span> SeqIO.parse(StringIO(data), <span class="py-string">"fasta"</span>): <span class="py-more">... </span> <span class="py-keyword">print</span> record.id, record.seq <span class="py-output">Alpha ACCGGATGTA</span> <span class="py-output">Beta AGGCTCGGTTA</span></pre> <p>Use the Bio.SeqIO.read(...) function when you expect a single record only.</p> <dl class="fields"> </dl> </td></tr></table> </div> <a name="read"></a> <div> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">read</span>(<span class="sig-arg">handle</span>, <span class="sig-arg">format</span>, <span class="sig-arg">alphabet</span>=<span class="sig-default">None</span>)</span> </h3> </td><td align="right" valign="top" ><span class="codelink"><a href="Bio.SeqIO-pysrc.html#read">source code</a></span> </td> </tr></table> <p>Turns a sequence file into a single SeqRecord.</p> <ul> <li> handle - handle to the file, or the filename as a string (note older verions of Biopython only took a handle). </li> <li> format - string describing the file format. </li> <li> alphabet - optional Alphabet object, useful when the sequence type cannot be automatically inferred from the file itself (e.g. format="fasta" or "tab") </li> </ul> <p>This function is for use parsing sequence files containing exactly one record. For example, reading a GenBank file:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>record = SeqIO.read(<span class="py-string">"GenBank/arab1.gb"</span>, <span class="py-string">"genbank"</span>) <span class="py-prompt">>>> </span><span class="py-keyword">print</span> <span class="py-string">"ID"</span>, record.id <span class="py-output">ID AC007323.5</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> <span class="py-string">"Sequence length"</span>, len(record) <span class="py-output">Sequence length 86436</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> <span class="py-string">"Sequence alphabet"</span>, record.seq.alphabet <span class="py-output">Sequence alphabet IUPACAmbiguousDNA()</span></pre> <p>If the handle contains no records, or more than one record, an exception is raised. For example:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>record = SeqIO.read(<span class="py-string">"GenBank/cor6_6.gb"</span>, <span class="py-string">"genbank"</span>) <span class="py-except">Traceback (most recent call last):</span> <span class="py-except"> ...</span> <span class="py-except">ValueError: More than one record found in handle</span></pre> <p>If however you want the first record from a file containing multiple records this function would raise an exception (as shown in the example above). Instead use:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>record = SeqIO.parse(<span class="py-string">"GenBank/cor6_6.gb"</span>, <span class="py-string">"genbank"</span>).next() <span class="py-prompt">>>> </span><span class="py-keyword">print</span> <span class="py-string">"First record's ID"</span>, record.id <span class="py-output">First record's ID X55053.1</span></pre> <p>Use the Bio.SeqIO.parse(handle, format) function if you want to read multiple records from the handle.</p> <dl class="fields"> </dl> </td></tr></table> </div> <a name="to_dict"></a> <div> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">to_dict</span>(<span class="sig-arg">sequences</span>, <span class="sig-arg">key_function</span>=<span class="sig-default">None</span>)</span> </h3> </td><td align="right" valign="top" ><span class="codelink"><a href="Bio.SeqIO-pysrc.html#to_dict">source code</a></span> </td> </tr></table> <p>Turns a sequence iterator or list into a dictionary.</p> <ul> <li> sequences - An iterator that returns SeqRecord objects, or simply a list of SeqRecord objects. </li> <li> key_function - Optional callback function which when given a SeqRecord should return a unique key for the dictionary. </li> </ul> <p>e.g. key_function = lambda rec : rec.name or, key_function = lambda rec : rec.description.split()[0]</p> <p>If key_function is ommitted then record.id is used, on the assumption that the records objects returned are SeqRecords with a unique id.</p> <p>If there are duplicate keys, an error is raised.</p> <p>Example usage, defaulting to using the record.id as key:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>filename = <span class="py-string">"GenBank/cor6_6.gb"</span> <span class="py-prompt">>>> </span>format = <span class="py-string">"genbank"</span> <span class="py-prompt">>>> </span>id_dict = SeqIO.to_dict(SeqIO.parse(filename, format)) <span class="py-prompt">>>> </span><span class="py-keyword">print</span> sorted(id_dict) <span class="py-output">['AF297471.1', 'AJ237582.1', 'L31939.1', 'M81224.1', 'X55053.1', 'X62281.1']</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> id_dict[<span class="py-string">"L31939.1"</span>].description <span class="py-output">Brassica rapa (clone bif72) kin mRNA, complete cds.</span></pre> <p>A more complex example, using the key_function argument in order to use a sequence checksum as the dictionary key:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio.SeqUtils.CheckSum <span class="py-keyword">import</span> seguid <span class="py-prompt">>>> </span>filename = <span class="py-string">"GenBank/cor6_6.gb"</span> <span class="py-prompt">>>> </span>format = <span class="py-string">"genbank"</span> <span class="py-prompt">>>> </span>seguid_dict = SeqIO.to_dict(SeqIO.parse(filename, format), <span class="py-more">... </span> key_function = <span class="py-keyword">lambda</span> rec : seguid(rec.seq)) <span class="py-prompt">>>> </span><span class="py-keyword">for</span> key, record <span class="py-keyword">in</span> sorted(seguid_dict.iteritems()): <span class="py-more">... </span> <span class="py-keyword">print</span> key, record.id <span class="py-output">/wQvmrl87QWcm9llO4/efg23Vgg AJ237582.1</span> <span class="py-output">BUg6YxXSKWEcFFH0L08JzaLGhQs L31939.1</span> <span class="py-output">SabZaA4V2eLE9/2Fm5FnyYy07J4 X55053.1</span> <span class="py-output">TtWsXo45S3ZclIBy4X/WJc39+CY M81224.1</span> <span class="py-output">l7gjJFE6W/S1jJn5+1ASrUKW/FA X62281.1</span> <span class="py-output">uVEYeAQSV5EDQOnFoeMmVea+Oow AF297471.1</span></pre> <p>This approach is not suitable for very large sets of sequences, as all the SeqRecord objects are held in memory. Instead, consider using the Bio.SeqIO.index() function (if it supports your particular file format).</p> <dl class="fields"> </dl> </td></tr></table> </div> <a name="index"></a> <div> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">index</span>(<span class="sig-arg">filename</span>, <span class="sig-arg">format</span>, <span class="sig-arg">alphabet</span>=<span class="sig-default">None</span>, <span class="sig-arg">key_function</span>=<span class="sig-default">None</span>)</span> </h3> </td><td align="right" valign="top" ><span class="codelink"><a href="Bio.SeqIO-pysrc.html#index">source code</a></span> </td> </tr></table> <p>Indexes a sequence file and returns a dictionary like object.</p> <ul> <li> filename - string giving name of file to be indexed </li> <li> format - lower case string describing the file format </li> <li> alphabet - optional Alphabet object, useful when the sequence type cannot be automatically inferred from the file itself (e.g. format="fasta" or "tab") </li> <li> key_function - Optional callback function which when given a SeqRecord identifier string should return a unique key for the dictionary. </li> </ul> <p>This indexing function will return a dictionary like object, giving the SeqRecord objects as values:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>records = SeqIO.index(<span class="py-string">"Quality/example.fastq"</span>, <span class="py-string">"fastq"</span>) <span class="py-prompt">>>> </span>len(records) <span class="py-output">3</span> <span class="py-output"></span><span class="py-prompt">>>> </span>sorted(records) <span class="py-output">['EAS54_6_R1_2_1_413_324', 'EAS54_6_R1_2_1_443_348', 'EAS54_6_R1_2_1_540_792']</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> records[<span class="py-string">"EAS54_6_R1_2_1_540_792"</span>].format(<span class="py-string">"fasta"</span>) <span class="py-output">>EAS54_6_R1_2_1_540_792</span> <span class="py-output">TTGGCAGGCCAAGGCCGATGGATCA</span> <span class="py-output"><BLANKLINE></span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-string">"EAS54_6_R1_2_1_540_792"</span> <span class="py-keyword">in</span> records <span class="py-output">True</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> records.get(<span class="py-string">"Missing"</span>, None) <span class="py-output">None</span></pre> <p>Note that this psuedo dictionary will not support all the methods of a true Python dictionary, for example values() is not defined since this would require loading all of the records into memory at once.</p> <p>When you call the index function, it will scan through the file, noting the location of each record. When you access a particular record via the dictionary methods, the code will jump to the appropriate part of the file and then parse that section into a SeqRecord.</p> <p>Note that not all the input formats supported by Bio.SeqIO can be used with this index function. It is designed to work only with sequential file formats (e.g. "fasta", "gb", "fastq") and is not suitable for any interlaced file format (e.g. alignment formats such as "clustal").</p> <p>For small files, it may be more efficient to use an in memory Python dictionary, e.g.</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>records = SeqIO.to_dict(SeqIO.parse(open(<span class="py-string">"Quality/example.fastq"</span>), <span class="py-string">"fastq"</span>)) <span class="py-prompt">>>> </span>len(records) <span class="py-output">3</span> <span class="py-output"></span><span class="py-prompt">>>> </span>sorted(records) <span class="py-output">['EAS54_6_R1_2_1_413_324', 'EAS54_6_R1_2_1_443_348', 'EAS54_6_R1_2_1_540_792']</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> records[<span class="py-string">"EAS54_6_R1_2_1_540_792"</span>].format(<span class="py-string">"fasta"</span>) <span class="py-output">>EAS54_6_R1_2_1_540_792</span> <span class="py-output">TTGGCAGGCCAAGGCCGATGGATCA</span> <span class="py-output"><BLANKLINE></span></pre> <p>As with the to_dict() function, by default the id string of each record is used as the key. You can specify a callback function to transform this (the record identifier string) into your prefered key. For example:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span><span class="py-keyword">def</span> <span class="py-defname">make_tuple</span>(identifier): <span class="py-more">... </span> parts = identifier.split(<span class="py-string">"_"</span>) <span class="py-more">... </span> return int(parts[-2]), int(parts[-1]) <span class="py-prompt">>>> </span>records = SeqIO.index(<span class="py-string">"Quality/example.fastq"</span>, <span class="py-string">"fastq"</span>, <span class="py-more">... </span> key_function=make_tuple) <span class="py-prompt">>>> </span>len(records) <span class="py-output">3</span> <span class="py-output"></span><span class="py-prompt">>>> </span>sorted(records) <span class="py-output">[(413, 324), (443, 348), (540, 792)]</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> records[(540, 792)].format(<span class="py-string">"fasta"</span>) <span class="py-output">>EAS54_6_R1_2_1_540_792</span> <span class="py-output">TTGGCAGGCCAAGGCCGATGGATCA</span> <span class="py-output"><BLANKLINE></span> <span class="py-output"></span><span class="py-prompt">>>> </span>(540, 792) <span class="py-keyword">in</span> records <span class="py-output">True</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-string">"EAS54_6_R1_2_1_540_792"</span> <span class="py-keyword">in</span> records <span class="py-output">False</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> records.get(<span class="py-string">"Missing"</span>, None) <span class="py-output">None</span></pre> <p>Another common use case would be indexing an NCBI style FASTA file, where you might want to extract the GI number from the FASTA identifer to use as the dictionary key.</p> <p>Notice that unlike the to_dict() function, here the key_function does not get given the full SeqRecord to use to generate the key. Doing so would impose a severe performance penalty as it would require the file to be completely parsed while building the index. Right now this is usually avoided.</p> <p>See also: Bio.SeqIO.index_db() and Bio.SeqIO.to_dict()</p> <dl class="fields"> </dl> </td></tr></table> </div> <a name="index_db"></a> <div> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">index_db</span>(<span class="sig-arg">index_filename</span>, <span class="sig-arg">filenames</span>=<span class="sig-default">None</span>, <span class="sig-arg">format</span>=<span class="sig-default">None</span>, <span class="sig-arg">alphabet</span>=<span class="sig-default">None</span>, <span class="sig-arg">key_function</span>=<span class="sig-default">None</span>)</span> </h3> </td><td align="right" valign="top" ><span class="codelink"><a href="Bio.SeqIO-pysrc.html#index_db">source code</a></span> </td> </tr></table> <p>Index several sequence files and return a dictionary like object.</p> <p>The index is stored in an SQLite database rather than in memory (as in the Bio.SeqIO.index(...) function).</p> <ul> <li> index_filename - Where to store the SQLite index </li> <li> filenames - list of strings specifying file(s) to be indexed, or when indexing a single file this can be given as a string. (optional if reloading an existing index, but must match) </li> <li> format - lower case string describing the file format (optional if reloading an existing index, but must match) </li> <li> alphabet - optional Alphabet object, useful when the sequence type cannot be automatically inferred from the file itself (e.g. format="fasta" or "tab") </li> <li> key_function - Optional callback function which when given a SeqRecord identifier string should return a unique key for the dictionary. </li> </ul> <p>This indexing function will return a dictionary like object, giving the SeqRecord objects as values:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio.Alphabet <span class="py-keyword">import</span> generic_protein <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span>files = [<span class="py-string">"GenBank/NC_000932.faa"</span>, <span class="py-string">"GenBank/NC_005816.faa"</span>] <span class="py-prompt">>>> </span><span class="py-keyword">def</span> <span class="py-defname">get_gi</span>(name): <span class="py-more">... </span> parts = name.split(<span class="py-string">"|"</span>) <span class="py-more">... </span> i = parts.index(<span class="py-string">"gi"</span>) <span class="py-more">... </span> assert i != -1 <span class="py-more">... </span> return parts[i+1] <span class="py-prompt">>>> </span>idx_name = <span class="py-string">":memory:"</span> <span class="py-comment">#use an in memory SQLite DB for this test</span> <span class="py-prompt">>>> </span>records = SeqIO.index_db(idx_name, files, <span class="py-string">"fasta"</span>, generic_protein, get_gi) <span class="py-prompt">>>> </span>len(records) <span class="py-output">95</span> <span class="py-output"></span><span class="py-prompt">>>> </span>records[<span class="py-string">"7525076"</span>].description <span class="py-output">'gi|7525076|ref|NP_051101.1| Ycf2 [Arabidopsis thaliana]'</span> <span class="py-output"></span><span class="py-prompt">>>> </span>records[<span class="py-string">"45478717"</span>].description <span class="py-output">'gi|45478717|ref|NP_995572.1| pesticin [Yersinia pestis biovar Microtus str. 91001]'</span></pre> <p>In this example the two files contain 85 and 10 records respectively.</p> <p>See also: Bio.SeqIO.index() and Bio.SeqIO.to_dict()</p> <dl class="fields"> </dl> </td></tr></table> </div> <a name="convert"></a> <div> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">convert</span>(<span class="sig-arg">in_file</span>, <span class="sig-arg">in_format</span>, <span class="sig-arg">out_file</span>, <span class="sig-arg">out_format</span>, <span class="sig-arg">alphabet</span>=<span class="sig-default">None</span>)</span> </h3> </td><td align="right" valign="top" ><span class="codelink"><a href="Bio.SeqIO-pysrc.html#convert">source code</a></span> </td> </tr></table> <p>Convert between two sequence file formats, return number of records.</p> <ul> <li> in_file - an input handle or filename </li> <li> in_format - input file format, lower case string </li> <li> out_file - an output handle or filename </li> <li> out_format - output file format, lower case string </li> <li> alphabet - optional alphabet to assume </li> </ul> <p>NOTE - If you provide an output filename, it will be opened which will overwrite any existing file without warning. This may happen if even the conversion is aborted (e.g. an invalid out_format name is given).</p> <p>For example, going from a filename to a handle:</p> <pre class="py-doctest"> <span class="py-prompt">>>> </span><span class="py-keyword">from</span> Bio <span class="py-keyword">import</span> SeqIO <span class="py-prompt">>>> </span><span class="py-keyword">from</span> StringIO <span class="py-keyword">import</span> StringIO <span class="py-prompt">>>> </span>handle = StringIO(<span class="py-string">""</span>) <span class="py-prompt">>>> </span>SeqIO.convert(<span class="py-string">"Quality/example.fastq"</span>, <span class="py-string">"fastq"</span>, handle, <span class="py-string">"fasta"</span>) <span class="py-output">3</span> <span class="py-output"></span><span class="py-prompt">>>> </span><span class="py-keyword">print</span> handle.getvalue() <span class="py-output">>EAS54_6_R1_2_1_413_324</span> <span class="py-output">CCCTTCTTGTCTTCAGCGTTTCTCC</span> <span class="py-output">>EAS54_6_R1_2_1_540_792</span> <span class="py-output">TTGGCAGGCCAAGGCCGATGGATCA</span> <span class="py-output">>EAS54_6_R1_2_1_443_348</span> <span class="py-output">GTTGCTTCTGGCGTGGGTGGGGGGG</span> <span class="py-output"><BLANKLINE></span></pre> <dl class="fields"> </dl> </td></tr></table> </div> <a name="_test"></a> <div class="private"> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr valign="top"><td> <h3 class="epydoc"><span class="sig"><span class="sig-name">_test</span>()</span> </h3> </td><td align="right" valign="top" ><span class="codelink"><a href="Bio.SeqIO-pysrc.html#_test">source code</a></span> </td> </tr></table> <p>Run the Bio.SeqIO module's doctests.</p> <p>This will try and locate the unit tests directory, and run the doctests from there in order that the relative paths used in the examples work.</p> <dl class="fields"> </dl> </td></tr></table> </div> <br /> <!-- ==================== VARIABLES DETAILS ==================== --> <a name="section-VariablesDetails"></a> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr bgcolor="#70b0f0" class="table-header"> <td colspan="2" class="table-header"> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr valign="top"> <td align="left"><span class="table-header">Variables Details</span></td> <td align="right" valign="top" ><span class="options">[<a href="#section-VariablesDetails" class="privatelink" onclick="toggle_private();" >hide private</a>]</span></td> </tr> </table> </td> </tr> </table> <a name="_FormatToIterator"></a> <div class="private"> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <h3 class="epydoc">_FormatToIterator</h3> <dl class="fields"> </dl> <dl class="fields"> <dt>Value:</dt> <dd><table><tr><td><pre class="variable"> {"fasta": FastaIO.FastaIterator, "gb": InsdcIO.GenBankIterator, "genba<span class="variable-linewrap"><img src="crarr.png" alt="\" /></span> nk": InsdcIO.GenBankIterator, "genbank-cds": InsdcIO.GenBankCdsFeature<span class="variable-linewrap"><img src="crarr.png" alt="\" /></span> Iterator, "embl": InsdcIO.EmblIterator, "embl-cds": InsdcIO.EmblCdsFea<span class="variable-linewrap"><img src="crarr.png" alt="\" /></span> tureIterator, "imgt": InsdcIO.ImgtIterator, "ig": IgIO.IgIterator, "sw<span class="variable-linewrap"><img src="crarr.png" alt="\" /></span> iss": SwissIO.SwissIterator, "phd": PhdIO.PhdIterator, "ace": AceIO.Ac<span class="variable-linewrap"><img src="crarr.png" alt="\" /></span> eIterator, "tab": TabIO.TabIterator, "pir": PirIO.PirIterator, "fastq"<span class="variable-linewrap"><img src="crarr.png" alt="\" /></span> : QualityIO.FastqPhredIterator, "fastq-sanger": QualityIO.FastqPhredIt<span class="variable-linewrap"><img src="crarr.png" alt="\" /></span> erator, "fastq-solexa": QualityIO.FastqSolexaIterator, "fastq-illumina<span class="variable-linewrap"><img src="crarr.png" alt="\" /></span> <code class="variable-ellipsis">...</code> </pre></td></tr></table> </dd> </dl> </td></tr></table> </div> <a name="_FormatToWriter"></a> <div class="private"> <table class="details" border="1" cellpadding="3" cellspacing="0" width="100%" bgcolor="white"> <tr><td> <h3 class="epydoc">_FormatToWriter</h3> <dl class="fields"> </dl> <dl class="fields"> <dt>Value:</dt> <dd><table><tr><td><pre class="variable"> <code class="variable-group">{</code><code class="variable-quote">'</code><code class="variable-string">embl</code><code class="variable-quote">'</code><code class="variable-op">: </code><class 'Bio.SeqIO.InsdcIO.EmblWriter'><code class="variable-op">,</code> <code class="variable-quote">'</code><code class="variable-string">fasta</code><code class="variable-quote">'</code><code class="variable-op">: </code><class 'Bio.SeqIO.FastaIO.FastaWriter'><code class="variable-op">,</code> <code class="variable-quote">'</code><code class="variable-string">fastq</code><code class="variable-quote">'</code><code class="variable-op">: </code><class 'Bio.SeqIO.QualityIO.FastqPhredWriter'><code class="variable-op">,</code> <code class="variable-quote">'</code><code class="variable-string">fastq-illumina</code><code class="variable-quote">'</code><code class="variable-op">: </code><class 'Bio.SeqIO.QualityIO.FastqIlluminaWriter'><code class="variable-op">,</code> <code class="variable-quote">'</code><code class="variable-string">fastq-sanger</code><code class="variable-quote">'</code><code class="variable-op">: </code><class 'Bio.SeqIO.QualityIO.FastqPhredWriter'><code class="variable-op">,</code> <code class="variable-quote">'</code><code class="variable-string">fastq-solexa</code><code class="variable-quote">'</code><code class="variable-op">: </code><class 'Bio.SeqIO.QualityIO.FastqSolexaWriter'><code class="variable-op">,</code> <code class="variable-quote">'</code><code class="variable-string">gb</code><code class="variable-quote">'</code><code class="variable-op">: </code><class 'Bio.SeqIO.InsdcIO.GenBankWriter'><code class="variable-op">,</code> <code class="variable-quote">'</code><code class="variable-string">genbank</code><code class="variable-quote">'</code><code class="variable-op">: </code><class 'Bio.SeqIO.InsdcIO.GenBankWriter'><code class="variable-op">,</code> <code class="variable-ellipsis">...</code> </pre></td></tr></table> </dd> </dl> </td></tr></table> </div> <br /> <!-- ==================== NAVIGATION BAR ==================== --> <table class="navbar" border="0" width="100%" cellpadding="0" bgcolor="#a0c0ff" cellspacing="0"> <tr valign="middle"> <!-- Tree link --> <th> <a href="module-tree.html">Trees</a> </th> <!-- Index link --> <th> <a href="identifier-index.html">Indices</a> </th> <!-- Help link --> <th> <a href="help.html">Help</a> </th> <th class="navbar" width="100%"></th> </tr> </table> <table border="0" cellpadding="0" cellspacing="0" width="100%%"> <tr> <td align="left" class="footer"> Generated by Epydoc 3.0.1 on Thu Aug 18 18:19:22 2011 </td> <td align="right" class="footer"> <a target="mainFrame" href="http://epydoc.sourceforge.net" >http://epydoc.sourceforge.net</a> </td> </tr> </table> <script type="text/javascript"> <!-- // Private objects are initially displayed (because if // javascript is turned off then we want them to be // visible); but by default, we want to hide them. So hide // them unless we have a cookie that says to show them. checkCookie(); // --> </script> </body> </html>