[Biopython] error writing fasta file using SeqIO

Wibowo Arindrarto w.arindrarto at gmail.com
Fri Jul 8 04:46:11 UTC 2011


Hi Hugo,

I think the problem is you tried to concatenate a SeqRecord object and a
string object. Do this in 'salva_fasta' instead:

SeqIO.write([obj_SeqRecord], obj_SeqRecord.id + '.fasta', 'fasta')

And just as an additional input, in the 'processar_lote' method, you can use
this to generate a list of absolute file name paths (import the os and glob
module beforehand).

files = [os.path.abspath(x) for x in glob.glob('*.ab1')]

os.path.abspath() returns the absolute file path for a given file, and
glob.glob() returns a list of names that matches the given pattern.

Hope that helps!
---
Wibowo Arindrarto (bow)
http://bow.web.id



On Fri, Jul 8, 2011 at 06:19, A M Torres, Hugo <
mnemonico at posthocergopropterhoc.net> wrote:

> Hi. Can someone spot why I can't create a fasta file here? I tried
> following
> the cookbook tutorial but something goes wrong when I try to write  the
> sequence from a SeqRecord object to a fasta file:
>
>  Lodge It
>
>   - New <http://paste.pound-python.org/>
>   - All <http://paste.pound-python.org/all/>
>   - About <http://paste.pound-python.org/about/>
>   - ? <http://paste.pound-python.org/help/>
>
>  Paste #9205
> Paste Details
>
> reply <http://paste.pound-python.org/?reply_to=9205> |
> raw<http://paste.pound-python.org/raw/9205/>
>
> posted on Jul 8, 2011 4:12:16 AM
>
>   - reply to this paste <http://paste.pound-python.org/?reply_to=9205>
>   - download paste <http://paste.pound-python.org/raw/9205/>
>   - compare with paste
>   - select different colorscheme  Autumn Borland Bw Colorful Default Emacs
>   Friendly Fruity Manni Monokai Murphy Native Pastie Perldoc Tango Trac Vs
>   - toggle line numbers<
> http://paste.pound-python.org/show/9205/?linenos=no>
>
>   1
>  2
>  3
>  4
>  5
>  6
>  7
>  8
>  9
> 10
> 11
> 12
> 13
> 14
> 15
> 16
> 17
> 18
> 19
> 20
> 21
> 22
> 23
> 24
> 25
> 26
> 27
> 28
> 29
> 30
> 31
> 32
> 33
> 34
> 35
> 36
> 37
> 38
> 39
> 40
> 41
> 42
> 43
> 44
> 45
> 46
> 47
> 48
> 49
> 50
> 51
> 52
> 53
> 54
> 55
> 56
> 57
> 58
> 59
> 60
> 61
> 62
> 63
> 64
> 65
> 66
> 67
> 68
> 69
> 70
> 71
> 72
> 73
> 74
> 75
> 76
> 77
> 78
>
> import abifpy
> from Bio.Seq import Seq
> from Bio.SeqRecord import SeqRecord
> from Bio.Emboss.Applications import NeedleCommandline
> import os #uso a funcao listdir
> from Bio import SeqIO
>
> def acessa_ab1(arquivo,trim=True): #generalizar depois
>    """acessa um arquivo ab1 e retorna um objeto SeqRecord"""
>    dado = abifpy.Trace(arquivo)
>    if trim:
>        cortado = dado.trim(dado.seq(ambig=True))
>        return SeqRecord(cortado, id=arquivo, description='dado cortado')
>    else:
>        return dado.seqrecord()
>
> def abre_ref(arquivo):
>    """acessa um arquivo contendo uma sequencia de referencia
>      retorna um objeto SeqRecord"""
>    with open(arquivo, 'rUb') as dado:
>        referencia = SeqIO.read(dado, 'genbank')
>    return referencia
>
> def salva_fasta(obj_SeqRecord):
>    """Pega um objeto SeqRecord e cria um fasta com a sua sequencia"""
>
>    SeqIO.write([obj_SeqRecord], obj_SeqRecord + '.fasta','fasta')
>
> def processar_lote(diretorio, ref):
>    """abre os arquivos ab1 de uma pasta, apara, salva em fasta, faz o
> alinhamento com
>    o fasta de referencia e salva o alinhamento em um arquivo para analise
>    posterior.
>    diretorio --> uma string representando o caminho da pasta contendo
> os arquivos
>    ref --> uma string representando o caminho absoluto + genbank com a
>    sequencia de referencia.
>    """
>
>    referencia = abre_ref(ref)
>    referencia.id = 'sequencia de referencia'
>    salva_fasta(referencia)
>    ab1files = [x for x in os.listdir(diretorio) if x.endswith('.ab1')]
>    for file in ab1files:
>        dado = acessa_ab1(diretorio + file)
>        salva_fasta(dado)
>        needle_cline = NeedleCommandline(asequence='referencia.fasta',
>                                        bsequence= file + '.fasta',
> gapopen=10, gapextend=0.5,
>                                        outfile=file + "_aligned.txt")
>    stdout, stderr = needle_cline()
>
>
>
>
> #pasta = '/home/mercutio22/Dropbox/My scripts/Fabi/vs/Seq_placa273
> analisada/'
> #referencia = '/home/mercutio22/Dropbox/My
> scripts/Fabi/vs/Seq_placa273 analisada/BRCA1 (total) - Frag 3450.gb'
>
> #processar_lote(pasta, referencia)
>
> dado = acessa_ab1('/home/mercutio22/Dropbox/My
> scripts/Fabi/vs/Seq_placa273 analisada/1174411_3450F_A01.ab1')
> print type(dado)
> salva_fasta(dado)
>
>
> ===============================error msg==============
> <class 'Bio.SeqRecord.SeqRecord'>
> Traceback (most recent call last):
>  File "louise.py", line 59, in <module>
>    salva_fasta(dado)
>  File "louise.py", line 26, in salva_fasta
>    SeqIO.write([obj_SeqRecord], obj_SeqRecord + '.fasta','fasta')
>  File "/usr/lib/pymodules/python2.6/Bio/SeqIO/__init__.py", line 412, in
> write
>    count = writer_class(handle).write_file(sequences)
>  File "/usr/lib/pymodules/python2.6/Bio/SeqIO/Interfaces.py", line
> 271, in write_file
>    count = self.write_records(records)
>  File "/usr/lib/pymodules/python2.6/Bio/SeqIO/Interfaces.py", line
> 256, in write_records
>    self.write_record(record)
>  File "/usr/lib/pymodules/python2.6/Bio/SeqIO/FastaIO.py", line 134,
> in write_record
>    self.handle.write(">%s\n" % title)
> AttributeError: 'SeqRecord' object has no attribute 'write'
> _______________________________________________
> Biopython mailing list  -  Biopython at lists.open-bio.org
> http://lists.open-bio.org/mailman/listinfo/biopython
>



More information about the Biopython mailing list