Biopython之gbff转gff格式 2020-04-27

  • gbff是NCBI基因组数据库常见的基因组genebank格式文件,在实际分析中,常常需要gff格式或者gtf格式,所以就存在gbff转换gff格式的需求


    Biopython之gbff转gff格式 2020-04-27_第1张图片
    image.png

  • 方法请参考

先安装Biopython,见前篇随笔
再安装 bcbio-gff
pip install bcbio-gff

$ pip install bcbio-gff
Collecting bcbio-gff
  Downloading bcbio-gff-0.6.6.tar.gz (19 kB)
Requirement already satisfied: six in /ldfssz1/MS_OP/USER/lifan/software/bin/miniconda3/lib/python3.7/site-packages (from bcbio-gff) (1.12.0)
Installing collected packages: bcbio-gff
    Running setup.py install for bcbio-gff ... done
Successfully installed bcbio-gff-0.6.6
  • 转换代码 Converting other formats to GFF3
from BCBio import GFF
from Bio import SeqIO

in_file = "your_file.gb"
out_file = "your_file.gff"
in_handle = open(in_file)
out_handle = open(out_file, "w")

GFF.write(SeqIO.parse(in_handle, "genbank"), out_handle)

in_handle.close()
out_handle.close()

实践案例一

  1. 将代码中your_file.gb 改成自己需要转换的文件 GCA_010614865.1_ASM1061486v1_genomic.gbff.gz
  2. 将代码中your_file.gff改成自己想要生成的gff文件名,如GCA_010614865.1_ASM1061486v1_genomic.gbff.gff
  3. 保存改好的代码并命名为python gbff2gff3.py
  4. 运行这个脚本python gbff2gff3.py

报错情况1

$ python gbff2gff3.py
  File "gbff2gff3.py", line 28
    out_handle.close()from BCBio import GFF
                         ^
SyntaxError: invalid syntax

该报错中提示无效语法out_handle.close()from BCBio import GFF,重新审视发现多粘贴了开头代码段from BCBio import GFF到结尾,删除该结尾处的from BCBio import GFF可解决

报错情况2

$ python gbff2gff3.py
File "*/miniconda3/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

该报错中提示UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

Google是法宝,直接搜索UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte,得到如下解,见 stackoverflow

Biopython之gbff转gff格式 2020-04-27_第2张图片
image.png

表示文件是压缩文件,需要先解压再作为输入文件才可

于是开始解压文件 gunzip -c GCA_010614865.1_ASM1061486v1_genomic.gbff.gz > Sd_genomic.gbff

Biopython之gbff转gff格式 2020-04-27_第3张图片
image.png

修改新的对应文件名后再运行脚本

##Converting other formats to GFF3

from BCBio import GFF
from Bio import SeqIO

in_file = "*/GCA_010614865.1_ASM1061486v1/Sd_genomic.gbff"
out_file = "*/GCA_010614865.1_ASM1061486v1/Sd_genomic.gbff.gff"
in_handle = open(in_file)
out_handle = open(out_file, "w")

GFF.write(SeqIO.parse(in_handle, "genbank"), out_handle)

in_handle.close()
out_handle.close()

python gbff2gff3.py
于是得到以下

Biopython之gbff转gff格式 2020-04-27_第4张图片
image.png

你可能感兴趣的:(Biopython之gbff转gff格式 2020-04-27)