生信猿我自己的生信百宝箱

超简单脚本python脚本实现批量多文件contig名称缩短

2019-12-05  本文已影响0人  瓶瓶瓶平平

今天复习中途顺便在帮张阿姨用prokka做原核contig注释,但是第一个contig的fa文件就出问题了!


图片.png

[03:47:20] Contig ID must <= 20 chars long: B12_L1.R1.clean_(paired)_contig_1
[03:47:20] Please rename your contigs or use --centre XXX to generate clean contig names.


作为一个码农的基本素质,观看报错,好的说是contig名字太长了。
查了半天没搞懂--centre怎么用。


图片.png

算了重命名吧哭哭!
先看看张阿姨的fa文件格式:


图片.png
也不算很长吧,不知道搞什么。但人家说不行就不行咯。
100多个文件,写个脚本来换吧!

先把你要缩短名字的fa文件放到一个目录下,新建一个目的目录。
然后利用以下脚本。

import os
#from collections import OrderedDict
mylist = os.listdir("/home/lfp/bacteria/AB/ABshunde/AB_fa/allfa")#批量读取文件名“”内是你的输入目录
#al = OrderedDict()
for i in mylist:
    file=open("/home/lfp/bacteria/AB/ABshunde/AB_fa/allfa/"+i,'r')
    naloc = i.find(".fa")
    new_file = i[:naloc] + "_new.fa"
    out_file=open("/home/lfp/bacteria/AB/ABshunde/AB_fa/new_fa/"+ new_file,"w") # “”内是你的输出目录
    for c in file.readlines():
        c = c.strip()
        if c.startswith(">"):
            location = c.find("ig")
            contigname = ">" + i[:naloc] + c[location+2:]
            print(contigname,file = out_file)
        else:
            print(c,file = out_file)
    file.close()
    out_file.close()
    print(i + "finish")


输入文件:


图片.png

输出文件:


图片.png
新contig名字
图片.png

再跑prokka。


图片.png
成功!开心!

ps:直接利用conda安装prokka为1.145版,依赖2.8+版本blast,但conda装的blast是2.71版的(最高!委屈脸),好吧作为一个社会主义接班人我才懒得编译呢,直接退回了1.1版本的prokka

上一篇 下一篇

猜你喜欢

热点阅读