\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kn}{from} \PYG{n+nn}{Bio} \PYG{k+kn}{import} \PYG{n}{SeqIO}


\PYG{k}{def} \PYG{n+nf}{trim\PYGZus{}adaptors}\PYG{p}{(}\PYG{n}{records}\PYG{p}{,} \PYG{n}{adaptor}\PYG{p}{):}
    \PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}Trims perfect adaptor sequences.}

\PYG{l+s+sd}{    This is a generator function, the records argument should}
\PYG{l+s+sd}{    be a list or iterator returning SeqRecord objects.}
\PYG{l+s+sd}{    \PYGZdq{}\PYGZdq{}\PYGZdq{}}
    \PYG{n}{len\PYGZus{}adaptor} \PYG{o}{=} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{adaptor}\PYG{p}{)}  \PYG{c+c1}{\PYGZsh{} cache this for later}
    \PYG{k}{for} \PYG{n}{record} \PYG{o+ow}{in} \PYG{n}{records}\PYG{p}{:}
        \PYG{n}{index} \PYG{o}{=} \PYG{n}{record}\PYG{o}{.}\PYG{n}{seq}\PYG{o}{.}\PYG{n}{find}\PYG{p}{(}\PYG{n}{adaptor}\PYG{p}{)}
        \PYG{k}{if} \PYG{n}{index} \PYG{o}{==} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{:}
            \PYG{c+c1}{\PYGZsh{} adaptor not found, so won\PYGZsq{}t trim}
            \PYG{k}{yield} \PYG{n}{record}
        \PYG{k}{else}\PYG{p}{:}
            \PYG{c+c1}{\PYGZsh{} trim off the adaptor}
            \PYG{k}{yield} \PYG{n}{record}\PYG{p}{[}\PYG{n}{index} \PYG{o}{+} \PYG{n}{len\PYGZus{}adaptor} \PYG{p}{:]}


\PYG{n}{original\PYGZus{}reads} \PYG{o}{=} \PYG{n}{SeqIO}\PYG{o}{.}\PYG{n}{parse}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}SRR020192.fastq\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}fastq\PYGZdq{}}\PYG{p}{)}
\PYG{n}{trimmed\PYGZus{}reads} \PYG{o}{=} \PYG{n}{trim\PYGZus{}adaptors}\PYG{p}{(}\PYG{n}{original\PYGZus{}reads}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}GATGACGGTGT\PYGZdq{}}\PYG{p}{)}
\PYG{n}{count} \PYG{o}{=} \PYG{n}{SeqIO}\PYG{o}{.}\PYG{n}{write}\PYG{p}{(}\PYG{n}{trimmed\PYGZus{}reads}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}trimmed.fastq\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}fastq\PYGZdq{}}\PYG{p}{)}
\PYG{k}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}Saved }\PYG{l+s+si}{\PYGZpc{}i}\PYG{l+s+s2}{ reads\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{count}\PYG{p}{)}
\end{Verbatim}
