python3 從gff文件提取指定基因信息


 

1、測試數據下載:ftp://ftp.ensemblgenomes.org/pub/plants/release-44/gff3/arabidopsis_thaliana/Arabidopsis_thaliana.TAIR10.44.chromosome.1.gff3.gz

 

 

 

2、測試:

root@PC1:/home/test# wc -l *
  221871 a.txt
      13 test.py
  221884 total
root@PC1:/home/test# tail -n 3 a.txt ## gff文件 1       araport11       exon    30424758        30425192        .       +       .       Parent=transcript:AT1G80990.1;Name=AT1G80990.1.exon2;constitutive=1;ensembl_end_phase=0;ensembl_phase=0;exon_id=AT1G80990.1.exon2;rank=2
1       araport11       CDS     30424758        30425192        .       +       0       ID=CDS:AT1G80990.1;Parent=transcript:AT1G80990.1;protein_id=AT1G80990.1
###
root@PC1:/home/test# cat test.py
fr=open("a.txt", "r")
fw=open("output.txt", "w")
for line in fr:
    line = line.strip()     ## 刪除換行符
    if line.startswith("#"):   ## 過濾掉#
        continue
    tmp = line.split("\t")  ## 依據制表符對每一行進行拆分
    if int(tmp[0]) == 1 and tmp[2] == "gene" and int(tmp[3]) > 100000 and int(tmp[4]) < 500000:  ## 過濾
        gene = tmp[8].split(";")[0].split("=")[1]    ## gene列單獨篩選
        final = tmp[0] + "\t" +  tmp[3] + "\t" + tmp[4] + "\t" + gene   ## 字符串拼接
        fw.write(final + "\n")    ## 寫入文件
fr.close()
fw.close()
root@PC1:/home/test# python3 test.py
root@PC1:/home/test# ls
a.txt  output.txt  test.py
root@PC1:/home/test# wc -l *
  221871 a.txt
     128 output.txt       ## 結果文件
      13 test.py
  222012 total
root@PC1:/home/test# head -n 2 output.txt ## 查看結果 1       104440  105330  gene:AT1G01250
1       108946  111699  gene:AT1G01260

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM