写入python 3.5中的gzipped fasta文件

时间:2016-12-08 23:50:44

标签: python python-3.5 biopython

我刚刚将我的工具中的所有模块从python 2迁移到python 3.我遇到了一个我无法解决的问题 - 我不知道如何写入gzipped fasta文件。

with gzip.open("sample.fasta.gz", "w") as file:
    print("writing...")
    for oid, seq in temp_data.items():
        # prepare row
        row = SeqRecord(Seq(seq), id=str(oid), description=temp_tax[oid])
        SeqIO.write(row, file, "fasta")

此代码适用于python 2,但它在python 3中不起作用,它引发了:

  

TypeError:memoryview:需要类似字节的对象,而不是' str'

我该如何解决问题?

2 个答案:

答案 0 :(得分:2)

gzip.open function与常规open函数不同,默认为二进制模式,而不是文本模式。您必须显式传递t作为模式字符串的一部分,以文本模式打开(接受/期望str):

with gzip.open("sample.fasta.gz", "wt") as file:

为避免行结束转换,您可能还需要pass newline=''(空字符串),以便在像Windows这样的系统上,它不会将\n转换为\r\n当它写的时候。

答案 1 :(得分:0)

尝试将从this.on("addedfile", function (file) { if (this.files[1] != null) { this.removeFile(this.files[0]); } else { file.status = 'queued'; } });返回的字符串转换为SeqRecord对象。

bytes

record.format('')将记录恢复为byte_row = record.format('').encode()

.encode()会将string转换为string个对象。