基因组生信专题全基因组分析相关

GTF与GFF文件格式的区别与转换

2019-01-07  本文已影响763人  鹿无为

GTF与GFF文件都是用于存储注释信息的文本类型,经常可以看到二者格式之间的相互转换。二者的名字相似,连内容都极为相似,那么二者的差异究竟在哪里呢?

GFF

GFF文件是一种用来描述基因组特征的文件,现在我们所使用的大部分都是第三版)(GFF3)。gff文件除gff1以外均由9列数据组成,前8列在gff的3个版本中信息都是相同的,只是名称不同。

第9列attributes的内容存在很大的版本特异性,9列信息(以gff3为例)分别是:

1 2 3 4 5 6 7 8 9
seqid annotation source feature type start end score strand phase attributes

GFF允许使用#作为注释符号,例如很多GFF文件都会使用如下的两行来表明其版本其创建日期:

## gff-version 2
## created 11/11/11 

GFF文件每一列所代表的含义前面表格中有,但请注意,它的第3列feature type是不受约束的,你可以使用任意的名称,但也不要太淘气~用一些适当的名称对于后面的分析会有很大的帮助。

我们需要注意的是GFF文件的第9列,从第二版开始(GFF2),所有的属性都以标签=值的方式呈现,各个属性之间以;作为分隔符

ID=geneAExon1;Name=geneA;Parent=geneA;Organism=human

在最新版本的GFF文件中(GFF3),有一些是已经预先定义的属性特征,并且这些特征往往还有特殊的含义:ID这个标签实在各行都要有的;另外有一个Parent的属性,它指明type所从属的上一级ID。

GTF

当前所广泛使用的GTF格式为第二版(GTF2),它主要是用来描述基因的注释。GTF格式有两个硬性标准:

1 2 3 4 5 6 7 8 9
seqname source feature start end score strand frame attributes

区别

gtf2的内容和gff3也是很相似的,区别只在其中的3列:

- GTF2 GFF3
feature type 根据软件名称注明 可以是任意名称
attributes 空格分隔 ‘=’分隔

格式转换

使用Cufflinks里面的工具"gffread":

#gff2gtf
gffread my.gff3 -T -o my.gtf
#gtf2gff
gffread merged.gtf -o- > merged.gff3

使用脚本进行格式转换

GTF to GFF

import sys

inFile = open(sys.argv[1],'r')

for line in inFile:
  #skip comment lines that start with the '#' character
  if line[0] != '#':
    #split line into columns by tab
    data = line.strip().split('\t')

    #parse the transcript/gene ID. I suck at using regex, so I usually just do a series of splits.
    #transcript_id和gene_ids是GTF第九列肯定存在的
    transcriptID = data[-1].split('transcript_id')[-1].split(';')[0].strip()[1:-1]
    geneID = data[-1].split('gene_id')[-1].split(';')[0].strip()[1:-1]

    #replace the last column with a GFF formatted attributes columns
    #I added a GID attribute just to conserve all the GTF data
    data[-1] = "ID=" + transcriptID + ";GID=" + geneID

    #print out this new GFF line
    print '\t'.join(data)

脚本运行

python myScript.py myFile.gtf > myFile.gff

GTF to GFF

import sys

inFile = open(sys.argv[1],'r')

for line in inFile:
  #skip comment lines that start with the '#' character
  if line[0] != '#':
    #split line into columns by tab
    data = line.strip().split('\t')

    ID = ''

    #if the feature is a gene 
    if data[2] == "gene":
      #get the id
      ID = data[-1].split('ID=')[-1].split(';')[0]

    #if the feature is anything else
    else:
      # get the parent as the ID
      ID = data[-1].split('Parent=')[-1].split(';')[0]
    
    #modify the last column
    data[-1] = 'gene_id "' + ID + '"; transcript_id "' + ID

    #print out this new GTF line
    print '\t'.join(data)

参考资料:

  1. Method: GFF/GTF conversion and differences
  2. NGS数据格式之gff/gtf
上一篇 下一篇

猜你喜欢

热点阅读