\chapter{Sequence Input/Output}
\label{chapter:Bio.SeqIO}

In this chapter we'll discuss in more detail the \verb|Bio.SeqIO| module, which was briefly introduced in Chapter~\ref{chapter:quick-start} and also used in Chapter~\ref{chapter:SeqRecord}. This aims to provide a simple interface for working with assorted sequence file formats in a uniform way.
See also the \verb|Bio.SeqIO| wiki page (\url{http://biopython.org/wiki/SeqIO}), and the built in documentation (also \href{http://biopython.org/DIST/docs/api/Bio.SeqIO-module.html}{online}):

\begin{verbatim}
>>> from Bio import SeqIO
>>> help(SeqIO)
...
\end{verbatim}

The ``catch'' is that you have to work with \verb|SeqRecord| objects (see Chapter~\ref{chapter:SeqRecord}), which contain a \verb|Seq| object (see Chapter~\ref{chapter:Bio.Seq}) plus annotation like an identifier and description.

\section{Parsing or Reading Sequences}
\label{sec:Bio.SeqIO-input}

The workhorse function \verb|Bio.SeqIO.parse()| is used to read in sequence data as SeqRecord objects.  This function expects two arguments:

\begin{enumerate}
\item The first argument is a {\it handle} to read the data from, or a filename. A handle is typically a file opened for reading, but could be the output from a command line program, or data downloaded from the internet (see Section~\ref{sec:SeqIO_Online}).  See Section~\ref{sec:appendix-handles} for more about handles.
\item The second argument is a lower case string specifying sequence format -- we don't try and guess the file format for you!  See \url{http://biopython.org/wiki/SeqIO} for a full listing of supported formats.
\end{enumerate}

\noindent There is an optional argument \verb|alphabet| to specify the alphabet to be used.  This is useful for file formats like FASTA where otherwise \verb|Bio.SeqIO| will default to a generic alphabet.

The \verb|Bio.SeqIO.parse()| function returns an {\it iterator} which gives \verb|SeqRecord| objects.  Iterators are typically used in a for loop as shown below.

Sometimes you'll find yourself dealing with files which contain only a single record.  For this situation use the function \verb|Bio.SeqIO.read()| which takes the same arguments.  Provided there is one and only one record in the file, this is returned as a \verb|SeqRecord| object.  Otherwise an exception is raised.

\subsection{Reading Sequence Files}

In general \verb|Bio.SeqIO.parse()| is used to read in sequence files as \verb|SeqRecord| objects, and is typically used with a for loop like this:

\begin{verbatim}
from Bio import SeqIO
for seq_record in SeqIO.parse("ls_orchid.fasta", "fasta"):
    print(seq_record.id)
    print(repr(seq_record.seq))
    print(len(seq_record))
\end{verbatim}

The above example is repeated from the introduction in Section~\ref{sec:sequence-parsing}, and will load the orchid DNA sequences in the FASTA format file \href{https://raw.githubusercontent.com/biopython/biopython/master/Doc/examples/ls_orchid.fasta}{ls\_orchid.fasta}.  If instead you wanted to load a GenBank format file like \href{https://raw.githubusercontent.com/biopython/biopython/master/Doc/examples/ls_orchid.gbk}{ls\_orchid.gbk} then all you need to do is change the filename and the format string:

\begin{verbatim}
from Bio import SeqIO
for seq_record in SeqIO.parse("ls_orchid.gbk", "genbank"):
    print(seq_record.id)
    print(seq_record.seq)
    print(len(seq_record))
\end{verbatim}

Similarly, if you wanted to read in a file in another file format, then assuming \verb|Bio.SeqIO.parse()| supports it you would just need to change the format string as appropriate, for example ``swiss'' for SwissProt files or ``embl'' for EMBL text files. There is a full listing on the wiki page (\url{http://biopython.org/wiki/SeqIO}) and in the built in documentation (also \href{http://biopython.org/DIST/docs/api/Bio.SeqIO-module.html}{online}).

Another very common way to use a Python iterator is within a list comprehension (or
a generator expression).  For example, if all you wanted to extract from the file was
a list of the record identifiers we can easily do this with the following list comprehension:

\begin{verbatim}
>>> from Bio import SeqIO
>>> identifiers = [seq_record.id for seq_record in SeqIO.parse("ls_orchid.gbk", "genbank")]
>>> identifiers
['Z78533.1', 'Z78532.1', 'Z78531.1', 'Z78530.1', 'Z78529.1', 'Z78527.1', ..., 'Z78439.1']
\end{verbatim}

\noindent There are more examples using \verb|SeqIO.parse()| in a list
comprehension like this in Section~\ref{seq:sequence-parsing-plus-pylab}
(e.g. for plotting sequence lengths or GC\%).

\subsection{Iterating over the records in a sequence file}

In the above examples, we have usually used a for loop to iterate over all the records one by one.  You can use the for loop with all sorts of Python objects (including lists, tuples and strings) which support the iteration interface.

The object returned by \verb|Bio.SeqIO| is actually an iterator which returns \verb|SeqRecord| objects.  You get to see each record in turn, but once and only once.  The plus point is that an iterator can save you memory when dealing with large files.

Instead of using a for loop, can also use the \verb|next()| function on an iterator to step through the entries, like this:

\begin{verbatim}
from Bio import SeqIO
record_iterator = SeqIO.parse("ls_orchid.fasta", "fasta")

first_record = next(record_iterator)
print(first_record.id)
print(first_record.description)

second_record = next(record_iterator)
print(second_record.id)
print(second_record.description)
\end{verbatim}

Note that if you try to use \verb|next()| and there are no more results, you'll get the special \verb|StopIteration| exception.

One special case to consider is when your sequence files have multiple records, but you only want the first one.  In this situation the following code is very concise:

\begin{verbatim}
from Bio import SeqIO
first_record = next(SeqIO.parse("ls_orchid.gbk", "genbank"))
\end{verbatim}

A word of warning here -- using the \verb|next()| function like this will silently ignore any additional records in the file.
If your files have {\it one and only one} record, like some of the online examples later in this chapter, or a GenBank file for a single chromosome, then use the new \verb|Bio.SeqIO.read()| function instead.
This will check there are no extra unexpected records present.

\subsection{Getting a list of the records in a sequence file}

In the previous section we talked about the fact that \verb|Bio.SeqIO.parse()| gives you a \verb|SeqRecord| iterator, and that you get the records one by one.  Very often you need to be able to access the records in any order. The Python \verb|list| data type is perfect for this, and we can turn the record iterator into a list of \verb|SeqRecord| objects using the built-in Python function \verb|list()| like so:

\begin{verbatim}
from Bio import SeqIO
records = list(SeqIO.parse("ls_orchid.gbk", "genbank"))

print("Found %i records" % len(records))

print("The last record")
last_record = records[-1] #using Python's list tricks
print(last_record.id)
print(repr(last_record.seq))
print(len(last_record))

print("The first record")
first_record = records[0] #remember, Python counts from zero
print(first_record.id)
print(repr(first_record.seq))
print(len(first_record))
\end{verbatim}

\noindent Giving:

\begin{verbatim}
Found 94 records
The last record
Z78439.1
Seq('CATTGTTGAGATCACATAATAATTGATCGAGTTAATCTGGAGGATCTGTTTACT...GCC', IUPACAmbiguousDNA())
592
The first record
Z78533.1
Seq('CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGATGAGACCGTGG...CGC', IUPACAmbiguousDNA())
740
\end{verbatim}

You can of course still use a for loop with a list of \verb|SeqRecord| objects.  Using a list is much more flexible than an iterator (for example, you can determine the number of records from the length of the list), but does need more memory because it will hold all the records in memory at once.

\subsection{Extracting data}

The \verb|SeqRecord| object and its annotation structures are described more fully in
Chapter~\ref{chapter:SeqRecord}.  As an example of how annotations are stored, we'll look at the output from parsing the first record in the GenBank file \href{https://raw.githubusercontent.com/biopython/biopython/master/Doc/examples/ls_orchid.gbk}{ls\_orchid.gbk}.

\begin{verbatim}
from Bio import SeqIO
record_iterator = SeqIO.parse("ls_orchid.gbk", "genbank")
first_record = next(record_iterator)
print(first_record)
\end{verbatim}

\noindent That should give something like this:

\begin{verbatim}
ID: Z78533.1
Name: Z78533
Description: C.irapeanum 5.8S rRNA gene and ITS1 and ITS2 DNA.
Number of features: 5
/sequence_version=1
/source=Cypripedium irapeanum
/taxonomy=['Eukaryota', 'Viridiplantae', 'Streptophyta', ..., 'Cypripedium']
/keywords=['5.8S ribosomal RNA', '5.8S rRNA gene', ..., 'ITS1', 'ITS2']
/references=[...]
/accessions=['Z78533']
/data_file_division=PLN
/date=30-NOV-2006
/organism=Cypripedium irapeanum
/gi=2765658
Seq('CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGATGAGACCGTGG...CGC', IUPACAmbiguousDNA())
\end{verbatim}

This gives a human readable summary of most of the annotation data for the \verb|SeqRecord|.
For this example we're going to use the \verb|.annotations| attribute which is just a Python dictionary.
The contents of this annotations dictionary were shown when we printed the record above.
You can also print them out directly:
\begin{verbatim}
print(first_record.annotations)
\end{verbatim}
\noindent Like any Python dictionary, you can easily get a list of the keys:
\begin{verbatim}
print(first_record.annotations.keys())
\end{verbatim}
\noindent or values:
\begin{verbatim}
print(first_record.annotations.values())
\end{verbatim}

In general, the annotation values are strings, or lists of strings.  One special case is any references in the file get stored as reference objects.

Suppose you wanted to extract a list of the species from the \href{https://raw.githubusercontent.com/biopython/biopython/master/Doc/examples/ls_orchid.gbk}{ls\_orchid.gbk} GenBank file.  The information we want, \emph{Cypripedium irapeanum}, is held in the annotations dictionary under `source' and `organism', which we can access like this:

\begin{verbatim}
>>> print(first_record.annotations["source"])
Cypripedium irapeanum
\end{verbatim}

\noindent or:

\begin{verbatim}
>>> print(first_record.annotations["organism"])
Cypripedium irapeanum
\end{verbatim}

In general, `organism' is used for the scientific name (in Latin, e.g. \textit{Arabidopsis thaliana}),
while `source' will often be the common name (e.g. thale cress).  In this example, as is often the case,
the two fields are identical.

Now let's go through all the records, building up a list of the species each orchid sequence is from:

\begin{verbatim}
from Bio import SeqIO
all_species = []
for seq_record in SeqIO.parse("ls_orchid.gbk", "genbank"):
    all_species.append(seq_record.annotations["organism"])
print(all_species)
\end{verbatim}

Another way of writing this code is to use a list comprehension:

\begin{verbatim}
from Bio import SeqIO
all_species = [seq_record.annotations["organism"] for seq_record in \
               SeqIO.parse("ls_orchid.gbk", "genbank")]
print(all_species)
\end{verbatim}

\noindent In either case, the result is:

% Try and keep this example output line short enough to fit on one page of PDF output:
\begin{verbatim}
['Cypripedium irapeanum', 'Cypripedium californicum', ..., 'Paphiopedilum barbatum']
\end{verbatim}

Great.  That was pretty easy because GenBank files are annotated in a standardised way.

Now, let's suppose you wanted to extract a list of the species from a FASTA file, rather than the GenBank file.  The bad news is you will have to write some code to extract the data you want from the record's description line - if the information is in the file in the first place!  Our example FASTA format file \href{https://raw.githubusercontent.com/biopython/biopython/master/Doc/examples/ls_orchid.fasta}{ls\_orchid.fasta} starts like this:

\begin{verbatim}
>gi|2765658|emb|Z78533.1|CIZ78533 C.irapeanum 5.8S rRNA gene and ITS1 and ITS2 DNA
CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGATGAGACCGTGGAATAAACGATCGAGTG
AATCCGGAGGACCGGTGTACTCAGCTCACCGGGGGCATTGCTCCCGTGGTGACCCTGATTTGTTGTTGGG
...
\end{verbatim}

You can check by hand, but for every record the species name is in the description line as the second word.  This means if we break up each record's \verb|.description| at the spaces, then the species is there as field number one (field zero is the record identifier).  That means we can do this:

\begin{verbatim}
from Bio import SeqIO
all_species = []
for seq_record in SeqIO.parse("ls_orchid.fasta", "fasta"):
    all_species.append(seq_record.description.split()[1])
print(all_species)
\end{verbatim}

\noindent This gives:

\begin{verbatim}
['C.irapeanum', 'C.californicum', 'C.fasciculatum', 'C.margaritaceum', ..., 'P.barbatum']
\end{verbatim}

The concise alternative using list comprehensions would be:

\begin{verbatim}
from Bio import SeqIO
all_species == [seq_record.description.split()[1] for seq_record in \
                SeqIO.parse("ls_orchid.fasta", "fasta")]
print(all_species)
\end{verbatim}

In general, extracting information from the FASTA description line is not very nice.
If you can get your sequences in a well annotated file format like GenBank or EMBL,
then this sort of annotation information is much easier to deal with.

\section{Parsing sequences from compressed files}
\label{sec:SeqIO_compressed}
In the previous section, we looked at parsing sequence data from a file.
Instead of using a filename, you can give \verb|Bio.SeqIO| a handle
(see Section~\ref{sec:appendix-handles}), and in this section
we'll use handles to parse sequence from compressed files.

As you'll have seen above, we can use \verb|Bio.SeqIO.read()| or
\verb|Bio.SeqIO.parse()| with a filename - for instance this quick
example calculates the total length of the sequences in a multiple
record GenBank file using a generator expression:

%doctest examples
\begin{verbatim}
>>> from Bio import SeqIO
>>> print(sum(len(r) for r in SeqIO.parse("ls_orchid.gbk", "gb")))
67518
\end{verbatim}

\noindent
Here we use a file handle instead, using the \verb|with| statement
to close the handle automatically:

%doctest examples
\begin{verbatim}
>>> from Bio import SeqIO
>>> with open("ls_orchid.gbk") as handle:
...     print(sum(len(r) for r in SeqIO.parse(handle, "gb")))
67518
\end{verbatim}

\noindent
Or, the old fashioned way where you manually close the handle:

%doctest examples
\begin{verbatim}
>>> from Bio import SeqIO
>>> handle = open("ls_orchid.gbk")
>>> print(sum(len(r) for r in SeqIO.parse(handle, "gb")))
67518
>>> handle.close()
\end{verbatim}

Now, suppose we have a gzip compressed file instead? These are very
commonly used on Linux. We can use Python's \verb|gzip| module to open
the compressed file for reading - which gives us a handle object:

%This doctest fails on Python 3, http://bugs.python.org/issue13989
\begin{verbatim}
>>> import gzip
>>> from Bio import SeqIO
>>> handle = gzip.open("ls_orchid.gbk.gz", "r")
>>> print(sum(len(r) for r in SeqIO.parse(handle, "gb")))
67518
>>> handle.close()
\end{verbatim}

Similarly if we had a bzip2 compressed file (sadly the function name isn't
quite as consistent):

%This doctest fails on Python 3
\begin{verbatim}
>>> import bz2
>>> from Bio import SeqIO
>>> handle = bz2.BZ2File("ls_orchid.gbk.bz2", "r")
>>> print(sum(len(r) for r in SeqIO.parse(handle, "gb")))
67518
>>> handle.close()
\end{verbatim}

\noindent
If you are using Python 2.7 or later, the \verb|with|-version works for
gzip and bz2 as well. Unfortunately this is broken on older versions of
Python (\href{http://bugs.python.org/issue3860}{Issue 3860}) and you'd
get an \verb|AttributeError| about \verb|__exit__| being missing.

There is a gzip (GNU Zip) variant called BGZF (Blocked GNU Zip Format),
which can be treated like an ordinary gzip file for reading, but has
advantages for random access later which we'll talk about later in
Section~\ref{sec:SeqIO-index-bgzf}.

\section{Parsing sequences from the net}
\label{sec:SeqIO_Online}
In the previous sections, we looked at parsing sequence data from a file
(using a filename or handle), and from compressed files (using a handle).
Here we'll use \verb|Bio.SeqIO| with another type of handle, a network
connection, to download and parse sequences from the internet.

Note that just because you \emph{can} download sequence data and parse it into
a \verb|SeqRecord| object in one go doesn't mean this is a good idea.
In general, you should probably download sequences \emph{once} and save them to
a file for reuse.

\subsection{Parsing GenBank records from the net}
\label{sec:SeqIO_GenBank_Online}
Section~\ref{sec:efetch} talks about the Entrez EFetch interface in more detail,
but for now let's just connect to the NCBI and get a few \textit{Opuntia} (prickly-pear)
sequences from GenBank using their GI numbers.

First of all, let's fetch just one record.  If you don't care about the
annotations and features downloading a FASTA file is a good choice as these
are compact.  Now remember, when you expect the handle to contain one and
only one record, use the \verb|Bio.SeqIO.read()| function:

\begin{verbatim}
from Bio import Entrez
from Bio import SeqIO
Entrez.email = "A.N.Other@example.com"
handle = Entrez.efetch(db="nucleotide", rettype="fasta", retmode="text", id="6273291")
seq_record = SeqIO.read(handle, "fasta")
handle.close()
print("%s with %i features" % (seq_record.id, len(seq_record.features)))
\end{verbatim}

\noindent Expected output:

\begin{verbatim}
gi|6273291|gb|AF191665.1|AF191665 with 0 features
\end{verbatim}

The NCBI will also let you ask for the file in other formats, in particular as
a GenBank file. Until Easter 2009, the Entrez EFetch API let you use ``genbank''
as the return type, however the NCBI now insist on using the official
return types of ``gb'' (or ``gp'' for proteins) as described on
\href{http://www.ncbi.nlm.nih.gov/entrez/query/static/efetchseq_help.html}
{EFetch for Sequence and other Molecular Biology Databases}.
As a result, in Biopython 1.50 onwards, we support ``gb'' as an
alias for ``genbank'' in \verb|Bio.SeqIO|.

\begin{verbatim}
from Bio import Entrez
from Bio import SeqIO
Entrez.email = "A.N.Other@example.com"
handle = Entrez.efetch(db="nucleotide", rettype="gb", retmode="text", id="6273291")
seq_record = SeqIO.read(handle, "gb") #using "gb" as an alias for "genbank"
handle.close()
print("%s with %i features" % (seq_record.id, len(seq_record.features)))
\end{verbatim}

\noindent The expected output of this example is:

\begin{verbatim}
AF191665.1 with 3 features
\end{verbatim}

\noindent Notice this time we have three features.

Now let's fetch several records.  This time the handle contains multiple records,
so we must use the \verb|Bio.SeqIO.parse()| function:

\begin{verbatim}
from Bio import Entrez
from Bio import SeqIO
Entrez.email = "A.N.Other@example.com"
handle = Entrez.efetch(db="nucleotide", rettype="gb", retmode="text",
                       id="6273291,6273290,6273289")
for seq_record in SeqIO.parse(handle, "gb"):
    print("%s %s..." % (seq_record.id, seq_record.description[:50]))
    print("Sequence length %i, %i features, from: %s"
          % (len(seq_record), len(seq_record.features), seq_record.annotations["source"]))
handle.close()
\end{verbatim}

\noindent That should give the following output:

\begin{verbatim}
AF191665.1 Opuntia marenae rpl16 gene; chloroplast gene for c...
Sequence length 902, 3 features, from: chloroplast Opuntia marenae
AF191664.1 Opuntia clavata rpl16 gene; chloroplast gene for c...
Sequence length 899, 3 features, from: chloroplast Grusonia clavata
AF191663.1 Opuntia bradtiana rpl16 gene; chloroplast gene for...
Sequence length 899, 3 features, from: chloroplast Opuntia bradtianaa
\end{verbatim}

See Chapter~\ref{chapter:entrez} for more about the \verb|Bio.Entrez| module, and make sure to read about the NCBI guidelines for using Entrez (Section~\ref{sec:entrez-guidelines}).

\subsection{Parsing SwissProt sequences from the net}
\label{sec:SeqIO_ExPASy_and_SwissProt}
Now let's use a handle to download a SwissProt file from ExPASy,
something covered in more depth in Chapter~\ref{chapter:swiss_prot}.
As mentioned above, when you expect the handle to contain one and only one record,
use the \verb|Bio.SeqIO.read()| function:

\begin{verbatim}
from Bio import ExPASy
from Bio import SeqIO
handle = ExPASy.get_sprot_raw("O23729")
seq_record = SeqIO.read(handle, "swiss")
handle.close()
print(seq_record.id)
print(seq_record.name)
print(seq_record.description)
print(repr(seq_record.seq))
print("Length %i" % len(seq_record))
print(seq_record.annotations["keywords"])
\end{verbatim}

\noindent Assuming your network connection is OK, you should get back:

\begin{verbatim}
O23729
CHS3_BROFI
RecName: Full=Chalcone synthase 3; EC=2.3.1.74; AltName: Full=Naringenin-chalcone synthase 3;
Seq('MAPAMEEIRQAQRAEGPAAVLAIGTSTPPNALYQADYPDYYFRITKSEHLTELK...GAE', ProteinAlphabet())
Length 394
['Acyltransferase', 'Flavonoid biosynthesis', 'Transferase']
\end{verbatim}

\section{Sequence files as Dictionaries}

We're now going to introduce three related functions in the \verb|Bio.SeqIO|
module which allow dictionary like random access to a multi-sequence file.
There is a trade off here between flexibility and memory usage. In summary:
\begin{itemize}
\item \verb|Bio.SeqIO.to_dict()| is the most flexible but also the most
memory demanding option (see Section~\ref{SeqIO:to_dict}). This is basically
a helper function to build a normal Python \verb|dictionary| with each entry
held as a \verb|SeqRecord| object in memory, allowing you to modify the
records.
\item \verb|Bio.SeqIO.index()| is a useful middle ground, acting like a
read only dictionary and parsing sequences into \verb|SeqRecord| objects
on demand (see Section~\ref{sec:SeqIO-index}).
\item \verb|Bio.SeqIO.index_db()| also acts like a read only dictionary
but stores the identifiers and file offsets in a file on disk (as an
SQLite3 database), meaning it has very low memory requirements (see
Section~\ref{sec:SeqIO-index-db}), but will be a little bit slower.
\end{itemize}
See the discussion for an broad overview
(Section~\ref{sec:SeqIO-indexing-discussion}).

\subsection{Sequence files as Dictionaries -- In memory}
\label{SeqIO:to_dict}

The next thing that we'll do with our ubiquitous orchid files is to show how
to index them and access them like a database using the Python \verb|dictionary|
data type (like a hash in Perl). This is very useful for moderately large files
where you only need to access certain elements of the file, and makes for a nice
quick 'n dirty database. For dealing with larger files where memory becomes a
problem, see Section~\ref{sec:SeqIO-index} below.

You can use the function \verb|Bio.SeqIO.to_dict()| to make a SeqRecord dictionary
(in memory).  By default this will use each record's identifier (i.e. the \verb|.id|
attribute) as the key.  Let's try this using our GenBank file:

%doctest examples
\begin{verbatim}
>>> from Bio import SeqIO
>>> orchid_dict = SeqIO.to_dict(SeqIO.parse("ls_orchid.gbk", "genbank"))
\end{verbatim}

There is just one required argument for \verb|Bio.SeqIO.to_dict()|, a list or
generator giving \verb|SeqRecord| objects. Here we have just used the output
from the \verb|SeqIO.parse| function. As the name suggests, this returns a
Python dictionary.

Since this variable \verb|orchid_dict| is an ordinary Python dictionary,
we can look at all of the keys we have available:

%cont-doctest
\begin{verbatim}
>>> len(orchid_dict)
94
\end{verbatim}
%Can't use following for doctest due to abbreviation
\begin{verbatim}
>>> list(orchid_dict.keys())
['Z78484.1', 'Z78464.1', 'Z78455.1', 'Z78442.1', 'Z78532.1', 'Z78453.1', ..., 'Z78471.1']
\end{verbatim}

You can leave out the ``list(...)`` bit if you are still using Python 2.
Under Python 3 the dictionary methods like ``.keys()`` and ``.values()``
are iterators rather than lists.

If you really want to, you can even look at all the records at once:
\begin{verbatim}
>>> list(orchid_dict.values()) #lots of output!
...
\end{verbatim}

We can access a single \verb|SeqRecord| object via the keys and manipulate the object as normal:

%cont-doctest
\begin{verbatim}
>>> seq_record = orchid_dict["Z78475.1"]
>>> print(seq_record.description)
P.supardii 5.8S rRNA gene and ITS1 and ITS2 DNA.
>>> print(repr(seq_record.seq))
Seq('CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGTTGAGATCACAT...GGT', IUPACAmbiguousDNA())
\end{verbatim}

So, it is very easy to create an in memory ``database'' of our GenBank records.  Next we'll try this for the FASTA file instead.

Note that those of you with prior Python experience should all be able to construct a dictionary like this ``by hand''.  However, typical dictionary construction methods will not deal with the case of repeated keys very nicely.  Using the \verb|Bio.SeqIO.to_dict()| will explicitly check for duplicate keys, and raise an exception if any are found.

\subsubsection{Specifying the dictionary keys}
\label{seq:seqio-todict-functionkey}

Using the same code as above, but for the FASTA file instead:

\begin{verbatim}
from Bio import SeqIO
orchid_dict = SeqIO.to_dict(SeqIO.parse("ls_orchid.fasta", "fasta"))
print(orchid_dict.keys())
\end{verbatim}

\noindent This time the keys are:

\begin{verbatim}
['gi|2765596|emb|Z78471.1|PDZ78471', 'gi|2765646|emb|Z78521.1|CCZ78521', ...
 ..., 'gi|2765613|emb|Z78488.1|PTZ78488', 'gi|2765583|emb|Z78458.1|PHZ78458']
\end{verbatim}

You should recognise these strings from when we parsed the FASTA file earlier in Section~\ref{sec:fasta-parsing}.  Suppose you would rather have something else as the keys - like the accession numbers.  This brings us nicely to \verb|SeqIO.to_dict()|'s optional argument \verb|key_function|, which lets you define what to use as the dictionary key for your records.

First you must write your own function to return the key you want (as a string) when given a \verb|SeqRecord| object.  In general, the details of function will depend on the sort of input records you are dealing with.  But for our orchids, we can just split up the record's identifier using the ``pipe'' character (the vertical line) and return the fourth entry (field three):

\begin{verbatim}
def get_accession(record):
    """"Given a SeqRecord, return the accession number as a string.

    e.g. "gi|2765613|emb|Z78488.1|PTZ78488" -> "Z78488.1"
    """
    parts = record.id.split("|")
    assert len(parts) == 5 and parts[0] == "gi" and parts[2] == "emb"
    return parts[3]
\end{verbatim}

\noindent Then we can give this function to the \verb|SeqIO.to_dict()| function to use in building the dictionary:

\begin{verbatim}
from Bio import SeqIO
orchid_dict = SeqIO.to_dict(SeqIO.parse("ls_orchid.fasta", "fasta"), key_function=get_accession)
print(orchid_dict.keys())
\end{verbatim}

\noindent Finally, as desired, the new dictionary keys:

\begin{verbatim}
>>> print(orchid_dict.keys())
['Z78484.1', 'Z78464.1', 'Z78455.1', 'Z78442.1', 'Z78532.1', 'Z78453.1', ..., 'Z78471.1']
\end{verbatim}

\noindent Not too complicated, I hope!

\subsubsection{Indexing a dictionary using the SEGUID checksum}

To give another example of working with dictionaries of \verb|SeqRecord| objects, we'll use the SEGUID checksum function.  This is a relatively recent checksum, and collisions should be very rare (i.e. two different sequences with the same checksum), an improvement on the CRC64 checksum.

Once again, working with the orchids GenBank file:

\begin{verbatim}
from Bio import SeqIO
from Bio.SeqUtils.CheckSum import seguid
for record in SeqIO.parse("ls_orchid.gbk", "genbank"):
    print(record.id, seguid(record.seq))
\end{verbatim}

\noindent This should give:

\begin{verbatim}
Z78533.1 JUEoWn6DPhgZ9nAyowsgtoD9TTo
Z78532.1 MN/s0q9zDoCVEEc+k/IFwCNF2pY
...
Z78439.1 H+JfaShya/4yyAj7IbMqgNkxdxQ
\end{verbatim}

Now, recall the \verb|Bio.SeqIO.to_dict()| function's \verb|key_function| argument expects a function which turns a \verb|SeqRecord| into a string.  We can't use the \verb|seguid()| function directly because it expects to be given a \verb|Seq| object (or a string).  However, we can use Python's \verb|lambda| feature to create a ``one off'' function to give to \verb|Bio.SeqIO.to_dict()| instead:

%doctest examples
\begin{verbatim}
>>> from Bio import SeqIO
>>> from Bio.SeqUtils.CheckSum import seguid
>>> seguid_dict = SeqIO.to_dict(SeqIO.parse("ls_orchid.gbk", "genbank"),
...                             lambda rec : seguid(rec.seq))
>>> record = seguid_dict["MN/s0q9zDoCVEEc+k/IFwCNF2pY"]
>>> print(record.id)
Z78532.1
>>> print(record.description)
C.californicum 5.8S rRNA gene and ITS1 and ITS2 DNA.
\end{verbatim}

\noindent That should have retrieved the record {\tt Z78532.1}, the second entry in the file.

\subsection{Sequence files as Dictionaries -- Indexed files}
%\subsection{Indexing really large files}
\label{sec:SeqIO-index}

As the previous couple of examples tried to illustrate, using
\verb|Bio.SeqIO.to_dict()| is very flexible. However, because it holds
everything in memory, the size of file you can work with is limited by your
computer's RAM. In general, this will only work on small to medium files.

For larger files you should consider
\verb|Bio.SeqIO.index()|, which works a little differently. Although
it still returns a dictionary like object, this does \emph{not} keep
\emph{everything} in memory. Instead, it just records where each record
is within the file -- when you ask for a particular record, it then parses
it on demand.

As an example, let's use the same GenBank file as before:

%doctest examples
\begin{verbatim}
>>> from Bio import SeqIO
>>> orchid_dict = SeqIO.index("ls_orchid.gbk", "genbank")
>>> len(orchid_dict)
94
\end{verbatim}
%Following is abbr.
\begin{verbatim}
>>> orchid_dict.keys()
['Z78484.1', 'Z78464.1', 'Z78455.1', 'Z78442.1', 'Z78532.1', 'Z78453.1', ..., 'Z78471.1']
\end{verbatim}
%cont-doctest
\begin{verbatim}
>>> seq_record = orchid_dict["Z78475.1"]
>>> print(seq_record.description)
P.supardii 5.8S rRNA gene and ITS1 and ITS2 DNA.
>>> seq_record.seq
Seq('CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGTTGAGATCACAT...GGT', IUPACAmbiguousDNA())
>>> orchid_dict.close()
\end{verbatim}

\noindent Note that \verb|Bio.SeqIO.index()| won't take a handle,
but only a filename. There are good reasons for this, but it is a little
technical. The second argument is the file format (a lower case string as
used in the other \verb|Bio.SeqIO| functions). You can use many other
simple file formats, including FASTA and FASTQ files (see the example in
Section~\ref{sec:fastq-indexing}). However, alignment
formats like PHYLIP or Clustal are not supported. Finally as an optional
argument you can supply an alphabet, or a key function.

Here is the same example using the FASTA file - all we change is the
filename and the format name:

\begin{verbatim}
>>> from Bio import SeqIO
>>> orchid_dict = SeqIO.index("ls_orchid.fasta", "fasta")
>>> len(orchid_dict)
94
>>> orchid_dict.keys()
['gi|2765596|emb|Z78471.1|PDZ78471', 'gi|2765646|emb|Z78521.1|CCZ78521', ...
 ..., 'gi|2765613|emb|Z78488.1|PTZ78488', 'gi|2765583|emb|Z78458.1|PHZ78458']
\end{verbatim}

\subsubsection{Specifying the dictionary keys}
\label{seq:seqio-index-functionkey}

Suppose you want to use the same keys as before? Much like with the
\verb|Bio.SeqIO.to_dict()| example in Section~\ref{seq:seqio-todict-functionkey},
you'll need to write a tiny function to map from the FASTA identifier
(as a string) to the key you want:

\begin{verbatim}
def get_acc(identifier):
    """"Given a SeqRecord identifier string, return the accession number as a string.

    e.g. "gi|2765613|emb|Z78488.1|PTZ78488" -> "Z78488.1"
    """
    parts = identifier.split("|")
    assert len(parts) == 5 and parts[0] == "gi" and parts[2] == "emb"
    return parts[3]
\end{verbatim}

\noindent Then we can give this function to the \verb|Bio.SeqIO.index()|
function to use in building the dictionary:

\begin{verbatim}
>>> from Bio import SeqIO
>>> orchid_dict = SeqIO.index("ls_orchid.fasta", "fasta", key_function=get_acc)
>>> print(orchid_dict.keys())
['Z78484.1', 'Z78464.1', 'Z78455.1', 'Z78442.1', 'Z78532.1', 'Z78453.1', ..., 'Z78471.1']
\end{verbatim}

\noindent Easy when you know how?

\subsubsection{Getting the raw data for a record}
\label{sec:seqio-index-getraw}

The dictionary-like object from \verb|Bio.SeqIO.index()| gives you each
entry as a \verb|SeqRecord| object. However, it is sometimes useful to
be able to get the original raw data straight from the file. For this
use the \verb|get_raw()| method which takes a
single argument (the record identifier) and returns a bytes string
(extracted from the file without modification).

A motivating example is extracting a subset of a records from a large
file where either \verb|Bio.SeqIO.write()| does not (yet) support the
output file format (e.g. the plain text SwissProt file format) or
where you need to preserve the text exactly (e.g. GenBank or EMBL
output from Biopython does not yet preserve every last bit of
annotation).

Let's suppose you have download the whole of UniProt in the plain
text SwissPort file format from their FTP site
(\url{ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/complete/uniprot_sprot.dat.gz})
and uncompressed it as the file \verb|uniprot_sprot.dat|, and you
want to extract just a few records from it:

\begin{verbatim}
>>> from Bio import SeqIO
>>> uniprot = SeqIO.index("uniprot_sprot.dat", "swiss")
>>> handle = open("selected.dat", "wb")
>>> for acc in ["P33487", "P19801", "P13689", "Q8JZQ5", "Q9TRC7"]:
...     handle.write(uniprot.get_raw(acc))
>>> handle.close()
\end{verbatim}

Note with Python 3 onwards, we have to open the file for writing in
binary mode because the \verb|get_raw()| method returns bytes strings.

There is a longer example in Section~\ref{sec:SeqIO-sort} using the
\verb|SeqIO.index()| function to sort a large sequence file (without
loading everything into memory at once).

\subsection{Sequence files as Dictionaries -- Database indexed files}
\label{sec:SeqIO-index-db}

Biopython 1.57 introduced an alternative, \verb|Bio.SeqIO.index_db()|, which
can work on even extremely large files since it stores the record information
as a file on disk (using an SQLite3 database) rather than in memory. Also,
you can index multiple files together (providing all the record identifiers
are unique).

The \verb|Bio.SeqIO.index()| function takes three required arguments:
\begin{itemize}
\item Index filename, we suggest using something ending \texttt{.idx}.
This index file is actually an SQLite3 database.
\item List of sequence filenames to index (or a single filename)
\item File format (lower case string as used in the rest of the
\verb|SeqIO| module).
\end{itemize}

As an example, consider the GenBank flat file releases from the NCBI FTP site,
\url{ftp://ftp.ncbi.nih.gov/genbank/}, which are gzip compressed GenBank files.

As of GenBank release $210$, there are $38$ files making up the viral sequences,
\texttt{gbvrl1.seq}, \ldots, \texttt{gbvrl38.seq}, taking about 8GB on disk once
decompressed, and containing in total nearly two million records.

If you were interested in the viruses, you could download all the virus files
from the command line very easily with the \texttt{rsync} command, and then
decompress them with \texttt{gunzip}:

\begin{verbatim}
# For illustration only, see reduced example below
$ rsync -avP "ftp.ncbi.nih.gov::genbank/gbvrl*.seq.gz" .
$ gunzip gbvrl*.seq.gz
\end{verbatim}

Unless you care about viruses, that's a lot of data to download just for this
example - so let's download \emph{just} the first four chunks (about 25MB each
compressed), and decompress them (taking in all about 1GB of space):

\begin{verbatim}
# Reduced example, download only the first four chunks
$ curl -O ftp://ftp.ncbi.nih.gov/genbank/gbvrl1.seq.gz
$ curl -O ftp://ftp.ncbi.nih.gov/genbank/gbvrl2.seq.gz
$ curl -O ftp://ftp.ncbi.nih.gov/genbank/gbvrl3.seq.gz
$ curl -O ftp://ftp.ncbi.nih.gov/genbank/gbvrl4.seq.gz
$ gunzip gbvrl*.seq.gz
\end{verbatim}

Now, in Python, index these GenBank files as follows:

\begin{verbatim}
>>> import glob
>>> from Bio import SeqIO
>>> files = glob.glob("gbvrl*.seq")
>>> print("%i files to index" % len(files))
4
>>> gb_vrl = SeqIO.index_db("gbvrl.idx", files, "genbank")
>>> print("%i sequences indexed" % len(gb_vrl))
272960 sequences indexed
\end{verbatim}

Indexing the full set of virus GenBank files took about ten minutes on my machine,
just the first four files took about a minute or so.

However, once done, repeating this will reload the index file \verb|gbvrl.idx|
in a fraction of a second.

You can use the index as a read only Python dictionary - without having to worry
about which file the sequence comes from, e.g.

\begin{verbatim}
>>> print(gb_vrl[``AB811634.1''].description)
Equine encephalosis virus NS3 gene, complete cds, isolate: Kimron1.
\end{verbatim}

\subsubsection{Getting the raw data for a record}

Just as with the \verb|Bio.SeqIO.index()| function discussed above in
Section~\ref{sec:seqio-index-getraw}, the dictionary like object also lets you
get at the raw bytes of each record:

% TODO - Under Python 3 you'd get the bytes string representation with
% leading b single-quote, escaped newlines, and closing single-quote:
%
% >>> print(gb_vrl.get_raw(``GQ333173.1''))
% b'LOCUS       GQ333173                 459 bp    DNA     linear   VRL 21-OCT-2009\nDEFINITION...'
\begin{verbatim}
>>> print(gb_vrl.get_raw(``AB811634.1''))
LOCUS       AB811634                 723 bp    RNA     linear   VRL 17-JUN-2015
DEFINITION  Equine encephalosis virus NS3 gene, complete cds, isolate: Kimron1.
ACCESSION   AB811634
...
//
\end{verbatim}

\subsection{Indexing compressed files}
\label{sec:SeqIO-index-bgzf}

Very often when you are indexing a sequence file it can be quite large -- so
you may want to compress it on disk. Unfortunately efficient random access
is difficult with the more common file formats like gzip and bzip2. In this
setting, BGZF (Blocked GNU Zip Format) can be very helpful. This is a variant
of gzip (and can be decompressed using standard gzip tools) popularised by
the BAM file format, \href{http://samtools.sourceforge.net/}{samtools}, and
\href{http://samtools.sourceforge.net/tabix.shtml}{tabix}.

To create a BGZF compressed file you can use the command line tool \verb|bgzip|
which comes with samtools. In our examples we use a filename extension
\verb|*.bgz|, so they can be distinguished from normal gzipped files (named
\verb|*.gz|). You can also use the \verb|Bio.bgzf| module to read and write
BGZF files from within Python.

The \verb|Bio.SeqIO.index()| and \verb|Bio.SeqIO.index_db()| can both be
used with BGZF compressed files. For example, if you started with an
uncompressed GenBank file:

%doctest examples
\begin{verbatim}
>>> from Bio import SeqIO
>>> orchid_dict = SeqIO.index("ls_orchid.gbk", "genbank")
>>> len(orchid_dict)
94
>>> orchid_dict.close()
\end{verbatim}

You could compress this (while keeping the original file) at the command
line using the following command -- but don't worry, the compressed file
is already included with the other example files:

\begin{verbatim}
$ bgzip -c ls_orchid.gbk > ls_orchid.gbk.bgz
\end{verbatim}

You can use the compressed file in exactly the same way:

%doctest examples
\begin{verbatim}
>>> from Bio import SeqIO
>>> orchid_dict = SeqIO.index("ls_orchid.gbk.bgz", "genbank")
>>> len(orchid_dict)
94
>>> orchid_dict.close()
\end{verbatim}

\noindent
or:

%Don't use doctest as would have to clean up the *.idx file
\begin{verbatim}
>>> from Bio import SeqIO
>>> orchid_dict = SeqIO.index_db("ls_orchid.gbk.bgz.idx", "ls_orchid.gbk.bgz", "genbank")
>>> len(orchid_dict)
94
>>> orchid_dict.close()
\end{verbatim}

The \verb|SeqIO| indexing automatically detects the BGZF compression. Note
that you can't use the same index file for the uncompressed and compressed files.

\subsection{Discussion}
\label{sec:SeqIO-indexing-discussion}

So, which of these methods should you use and why? It depends on what you are
trying to do (and how much data you are dealing with). However, in general
picking \verb|Bio.SeqIO.index()| is a good starting point. If you are dealing
with millions of records, multiple files, or repeated analyses, then look at
\verb|Bio.SeqIO.index_db()|.

Reasons to choose \verb|Bio.SeqIO.to_dict()| over either
\verb|Bio.SeqIO.index()| or \verb|Bio.SeqIO.index_db()| boil down to a need
for flexibility despite its high memory needs. The advantage of storing the
\verb|SeqRecord| objects in memory is they can be changed, added to, or
removed at will. In addition to the downside of high memory consumption,
indexing can also take longer because all the records must be fully parsed.

Both \verb|Bio.SeqIO.index()| and \verb|Bio.SeqIO.index_db()| only parse
records on demand. When indexing, they scan the file once looking for the
start of each record and do as little work as possible to extract the
identifier.

Reasons to choose \verb|Bio.SeqIO.index()| over \verb|Bio.SeqIO.index_db()|
include:
\begin{itemize}
\item Faster to build the index (more noticeable in simple file formats)
\item Slightly faster access as SeqRecord objects (but the difference is only
really noticeable for simple to parse file formats).
\item Can use any immutable Python object as the dictionary keys (e.g. a
tuple of strings, or a frozen set) not just strings.
\item Don't need to worry about the index database being out of date if the
sequence file being indexed has changed.
\end{itemize}

Reasons to choose \verb|Bio.SeqIO.index_db()| over \verb|Bio.SeqIO.index()|
include:
\begin{itemize}
\item Not memory limited -- this is already important with files from second
generation sequencing where 10s of millions of sequences are common, and
using \verb|Bio.SeqIO.index()| can require more than 4GB of RAM and therefore
a 64bit version of Python.
\item Because the index is kept on disk, it can be reused. Although building
the index database file takes longer, if you have a script which will be
rerun on the same datafiles in future, this could save time in the long run.
\item Indexing multiple files together
\item The \verb|get_raw()| method can be much faster, since for most file
formats the length of each record is stored as well as its offset.
\end{itemize}

\section{Writing Sequence Files}

We've talked about using \verb|Bio.SeqIO.parse()| for sequence input (reading files), and now we'll look at \verb|Bio.SeqIO.write()| which is for sequence output (writing files).  This is a function taking three arguments: some \verb|SeqRecord| objects, a handle or filename to write to, and a sequence format.

Here is an example, where we start by creating a few \verb|SeqRecord| objects the hard way (by hand, rather than by loading them from a file):

\begin{verbatim}
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import generic_protein

rec1 = SeqRecord(Seq("MMYQQGCFAGGTVLRLAKDLAENNRGARVLVVCSEITAVTFRGPSETHLDSMVGQALFGD" \
                    +"GAGAVIVGSDPDLSVERPLYELVWTGATLLPDSEGAIDGHLREVGLTFHLLKDVPGLISK" \
                    +"NIEKSLKEAFTPLGISDWNSTFWIAHPGGPAILDQVEAKLGLKEEKMRATREVLSEYGNM" \
                    +"SSAC", generic_protein),
                 id="gi|14150838|gb|AAK54648.1|AF376133_1",
                 description="chalcone synthase [Cucumis sativus]")

rec2 = SeqRecord(Seq("YPDYYFRITNREHKAELKEKFQRMCDKSMIKKRYMYLTEEILKENPSMCEYMAPSLDARQ" \
                    +"DMVVVEIPKLGKEAAVKAIKEWGQ", generic_protein),
                 id="gi|13919613|gb|AAK33142.1|",
                 description="chalcone synthase [Fragaria vesca subsp. bracteata]")

rec3 = SeqRecord(Seq("MVTVEEFRRAQCAEGPATVMAIGTATPSNCVDQSTYPDYYFRITNSEHKVELKEKFKRMC" \
                    +"EKSMIKKRYMHLTEEILKENPNICAYMAPSLDARQDIVVVEVPKLGKEAAQKAIKEWGQP" \
                    +"KSKITHLVFCTTSGVDMPGCDYQLTKLLGLRPSVKRFMMYQQGCFAGGTVLRMAKDLAEN" \
                    +"NKGARVLVVCSEITAVTFRGPNDTHLDSLVGQALFGDGAAAVIIGSDPIPEVERPLFELV" \
                    +"SAAQTLLPDSEGAIDGHLREVGLTFHLLKDVPGLISKNIEKSLVEAFQPLGISDWNSLFW" \
                    +"IAHPGGPAILDQVELKLGLKQEKLKATRKVLSNYGNMSSACVLFILDEMRKASAKEGLGT" \
                    +"TGEGLEWGVLFGFGPGLTVETVVLHSVAT", generic_protein),
                 id="gi|13925890|gb|AAK49457.1|",
                 description="chalcone synthase [Nicotiana tabacum]")

my_records = [rec1, rec2, rec3]
\end{verbatim}

\noindent Now we have a list of \verb|SeqRecord| objects, we'll write them to a FASTA format file:

\begin{verbatim}
from Bio import SeqIO
SeqIO.write(my_records, "my_example.faa", "fasta")
\end{verbatim}

\noindent And if you open this file in your favourite text editor it should look like this:

\begin{verbatim}
>gi|14150838|gb|AAK54648.1|AF376133_1 chalcone synthase [Cucumis sativus]
MMYQQGCFAGGTVLRLAKDLAENNRGARVLVVCSEITAVTFRGPSETHLDSMVGQALFGD
GAGAVIVGSDPDLSVERPLYELVWTGATLLPDSEGAIDGHLREVGLTFHLLKDVPGLISK
NIEKSLKEAFTPLGISDWNSTFWIAHPGGPAILDQVEAKLGLKEEKMRATREVLSEYGNM
SSAC
>gi|13919613|gb|AAK33142.1| chalcone synthase [Fragaria vesca subsp. bracteata]
YPDYYFRITNREHKAELKEKFQRMCDKSMIKKRYMYLTEEILKENPSMCEYMAPSLDARQ
DMVVVEIPKLGKEAAVKAIKEWGQ
>gi|13925890|gb|AAK49457.1| chalcone synthase [Nicotiana tabacum]
MVTVEEFRRAQCAEGPATVMAIGTATPSNCVDQSTYPDYYFRITNSEHKVELKEKFKRMC
EKSMIKKRYMHLTEEILKENPNICAYMAPSLDARQDIVVVEVPKLGKEAAQKAIKEWGQP
KSKITHLVFCTTSGVDMPGCDYQLTKLLGLRPSVKRFMMYQQGCFAGGTVLRMAKDLAEN
NKGARVLVVCSEITAVTFRGPNDTHLDSLVGQALFGDGAAAVIIGSDPIPEVERPLFELV
SAAQTLLPDSEGAIDGHLREVGLTFHLLKDVPGLISKNIEKSLVEAFQPLGISDWNSLFW
IAHPGGPAILDQVELKLGLKQEKLKATRKVLSNYGNMSSACVLFILDEMRKASAKEGLGT
TGEGLEWGVLFGFGPGLTVETVVLHSVAT
\end{verbatim}

Suppose you wanted to know how many records the \verb|Bio.SeqIO.write()| function wrote to the handle?
If your records were in a list you could just use \verb|len(my_records)|, however you can't do that when your records come from a generator/iterator.  The \verb|Bio.SeqIO.write()| function returns the number of \verb|SeqRecord| objects written to the file.

\emph{Note} - If you tell the \verb|Bio.SeqIO.write()| function to write to a file that already exists, the old file will be overwritten without any warning.

\subsection{Round trips}

Some people like their parsers to be ``round-tripable'', meaning if you read in
a file and write it back out again it is unchanged. This requires that the parser
must extract enough information to reproduce the original file \emph{exactly}.
\verb|Bio.SeqIO| does \emph{not} aim to do this.

As a trivial example, any line wrapping of the sequence data in FASTA files is
allowed. An identical \verb|SeqRecord| would be given from parsing the following
two examples which differ only in their line breaks:

\begin{verbatim}
>YAL068C-7235.2170 Putative promoter sequence
TACGAGAATAATTTCTCATCATCCAGCTTTAACACAAAATTCGCACAGTTTTCGTTAAGA
GAACTTAACATTTTCTTATGACGTAAATGAAGTTTATATATAAATTTCCTTTTTATTGGA

>YAL068C-7235.2170 Putative promoter sequence
TACGAGAATAATTTCTCATCATCCAGCTTTAACACAAAATTCGCA
CAGTTTTCGTTAAGAGAACTTAACATTTTCTTATGACGTAAATGA
AGTTTATATATAAATTTCCTTTTTATTGGA
\end{verbatim}

To make a round-tripable FASTA parser you would need to keep track of where the
sequence line breaks occurred, and this extra information is usually pointless.
Instead Biopython uses a default line wrapping of $60$ characters on output.
The same problem with white space applies in many other file formats too.
Another issue in some cases is that Biopython does not (yet) preserve every
last bit of annotation (e.g. GenBank and EMBL).

Occasionally preserving the original layout (with any quirks it may have) is
important. See Section~\ref{sec:seqio-index-getraw} about the \verb|get_raw()|
method of the \verb|Bio.SeqIO.index()| dictionary-like object for one potential
solution.

\subsection{Converting between sequence file formats}
\label{sec:SeqIO-conversion}

In previous example we used a list of \verb|SeqRecord| objects as input to the \verb|Bio.SeqIO.write()| function, but it will also accept a \verb|SeqRecord| iterator like we get from \verb|Bio.SeqIO.parse()| -- this lets us do file conversion by combining these two functions.

For this example we'll read in the GenBank format file \href{https://raw.githubusercontent.com/biopython/biopython/master/Doc/examples/ls_orchid.gbk}{ls\_orchid.gbk} and write it out in FASTA format:

\begin{verbatim}
from Bio import SeqIO
records = SeqIO.parse("ls_orchid.gbk", "genbank")
count = SeqIO.write(records, "my_example.fasta", "fasta")
print("Converted %i records" % count)
\end{verbatim}

Still, that is a little bit complicated. So, because file conversion is such a
common task, there is a helper function letting you replace that with just:

\begin{verbatim}
from Bio import SeqIO
count = SeqIO.convert("ls_orchid.gbk", "genbank", "my_example.fasta", "fasta")
print("Converted %i records" % count)
\end{verbatim}

The \verb|Bio.SeqIO.convert()| function will take handles \emph{or} filenames.
Watch out though -- if the output file already exists, it will overwrite it!
To find out more, see the built in help:

\begin{verbatim}
>>> from Bio import SeqIO
>>> help(SeqIO.convert)
...
\end{verbatim}

In principle, just by changing the filenames and the format names, this code
could be used to convert between any file formats available in Biopython.
However, writing some formats requires information (e.g. quality scores) which
other files formats don't contain. For example, while you can turn a FASTQ
file into a FASTA file, you can't do the reverse. See also
Sections~\ref{sec:SeqIO-fastq-conversion} and~\ref{sec:SeqIO-fasta-qual-conversion}
in the cookbook chapter which looks at inter-converting between different FASTQ formats.

Finally, as an added incentive for using the \verb|Bio.SeqIO.convert()| function
(on top of the fact your code will be shorter), doing it this way may also be
faster! The reason for this is the convert function can take advantage of
several file format specific optimisations and tricks.

\subsection{Converting a file of sequences to their reverse complements}
\label{sec:SeqIO-reverse-complement}

Suppose you had a file of nucleotide sequences, and you wanted to turn it into a file containing their reverse complement sequences.  This time a little bit of work is required to transform the \verb|SeqRecord| objects we get from our input file into something suitable for saving to our output file.

To start with, we'll use \verb|Bio.SeqIO.parse()| to load some nucleotide
sequences from a file, then print out their reverse complements using
the \verb|Seq| object's built in \verb|.reverse_complement()| method (see Section~\ref{sec:seq-reverse-complement}):

\begin{verbatim}
>>> from Bio import SeqIO
>>> for record in SeqIO.parse("ls_orchid.gbk", "genbank"):
...     print(record.id)
...     print(record.seq.reverse_complement())
\end{verbatim}

Now, if we want to save these reverse complements to a file, we'll need to make \verb|SeqRecord| objects.
We can use  the \verb|SeqRecord| object's built in \verb|.reverse_complement()| method (see Section~\ref{sec:SeqRecord-reverse-complement}) but we must decide how to name our new records.

This is an excellent place to demonstrate the power of list comprehensions which make a list in memory:
%doctest examples
\begin{verbatim}
>>> from Bio import SeqIO
>>> records = [rec.reverse_complement(id="rc_"+rec.id, description = "reverse complement") \
...            for rec in SeqIO.parse("ls_orchid.fasta", "fasta")]
>>> len(records)
94
\end{verbatim}

\noindent Now list comprehensions have a nice trick up their sleeves, you can add a conditional statement:

%cont-doctest examples
\begin{verbatim}
>>> records = [rec.reverse_complement(id="rc_"+rec.id, description = "reverse complement") \
...            for rec in SeqIO.parse("ls_orchid.fasta", "fasta") if len(rec)<700]
>>> len(records)
18
\end{verbatim}

That would create an in memory list of reverse complement records where the sequence length was under 700 base pairs. However, we can do exactly the same with a generator expression - but with the advantage that this does not create a list of all the records in memory at once:

%cont-doctest examples
\begin{verbatim}
>>> records = (rec.reverse_complement(id="rc_"+rec.id, description = "reverse complement") \
...           for rec in SeqIO.parse("ls_orchid.fasta", "fasta") if len(rec)<700)
\end{verbatim}

As a complete example:

%not a doctest as would have to remove the output file
\begin{verbatim}
>>> from Bio import SeqIO
>>> records = (rec.reverse_complement(id="rc_"+rec.id, description = "reverse complement") \
...            for rec in SeqIO.parse("ls_orchid.fasta", "fasta") if len(rec)<700)
>>> SeqIO.write(records, "rev_comp.fasta", "fasta")
18
\end{verbatim}

There is a related example in Section~\ref{sec:SeqIO-translate}, translating each
record in a FASTA file from nucleotides to amino acids.

\subsection{Getting your SeqRecord objects as formatted strings}
\label{sec:Bio.SeqIO-and-StringIO}
Suppose that you don't really want to write your records to a file or handle -- instead you want a string containing the records in a particular file format.  The \verb|Bio.SeqIO| interface is based on handles, but Python has a useful built in module which provides a string based handle.

For an example of how you might use this, let's load in a bunch of \verb|SeqRecord| objects from our orchids GenBank file, and create a string containing the records in FASTA format:

\begin{verbatim}
from Bio import SeqIO
from StringIO import StringIO
records = SeqIO.parse("ls_orchid.gbk", "genbank")
out_handle = StringIO()
SeqIO.write(records, out_handle, "fasta")
fasta_data = out_handle.getvalue()
print(fasta_data)
\end{verbatim}

This isn't entirely straightforward the first time you see it!  On the bright side, for the special case where you would like a string containing a \emph{single} record in a particular file format, use the the \verb|SeqRecord| class' \verb|format()| method (see Section~\ref{sec:SeqRecord-format}).

Note that although we don't encourage it, you \emph{can} use the \verb|format()| method to write to a file, for example something like this:
\begin{verbatim}
from Bio import SeqIO
out_handle = open("ls_orchid_long.tab", "w")
for record in SeqIO.parse("ls_orchid.gbk", "genbank"):
    if len(record) > 100:
        out_handle.write(record.format("tab"))
out_handle.close()
\end{verbatim}
\noindent While this style of code will work for a simple sequential file format like FASTA or the simple tab separated format used here, it will \emph{not} work for more complex or interlaced file formats.  This is why we still recommend using \verb|Bio.SeqIO.write()|, as in the following example:
\begin{verbatim}
from Bio import SeqIO
records = (rec for rec in SeqIO.parse("ls_orchid.gbk", "genbank") if len(rec) > 100)
SeqIO.write(records, "ls_orchid.tab", "tab")
\end{verbatim}
\noindent Making a single call to \verb|SeqIO.write(...)| is also much quicker than
multiple calls to the \verb|SeqRecord.format(...)| method.

