美文网首页生信分析流程我自己的生信百宝箱
超简单python脚本实现从resfinder+blast下机数

超简单python脚本实现从resfinder+blast下机数

作者: 瓶瓶瓶平平 | 来源:发表于2019-12-10 14:25 被阅读0次

    今天还是在做张阿姨的项目,还是那100多个菌,她说要blast一下抗性基因,那就比咯,最出名与权威的的细菌抗性基因库自然肯定是resfinder了。

    for i in $(cat list); do echo i; mkdir ${i}_re; python resfinder.py -i ${i}_new.fa -o ${i}_re -p resfinder  -t 0.95 -l 0.90 > ${i}_out; done
    

    写个循环,刷刷弄完真开心。

    问题来了!


    图片.png

    生成的json结果的命名全是一样的!都是data_resfinder.json
    只有生成的目录文件名不一样,噢不。怎么办。


    图片.png
    机智的我们来看看resfinder的py脚本。噢一千多行,放弃!好吧其实我还是找到了他的输出指令。想想算了我要做矩阵它json我还是要处理的。一步到位吧,直接用我们">"出来的result文件处理好了!
    应该是输出文件指令吧。。。

    我们来看看输出的result是什么样子的:


    图片.png
    前面一千多行的废话。要的只是这几个字符 图片.png
    那就easy了!
    #resfinder转csv
    import os 
    from collections import OrderedDict
    import csv
    
    forall = OrderedDict()
    
    yourdir = "C:/Users/Administrator/Desktop/output/"#input("dir:")#输入文件夹名字
    mylist = os.listdir(yourdir)#批量读取文件名
    

    首先先读读包,引入一下文件。
    然后创建一个筛选器。

    #提取函数
    def select(file):
        for c in file.readlines():
            #c = c.strip()
            if len(c) > 70:
                if c.find("No hit found")  == -1:
                    if c.find("run_info")  == -1:
                        if c[70] != " ":
                            c = c.strip()
                            c = c.replace("{","")
                            c = c.replace("'","")
                            c = c.replace('"',"")
                            temp = c.find("HSP_length")
                            c = c[:temp-2]
                            c = c.split()
                            last = c[-1]
                            forall[i].append(last)
    

    再要一个判断器判断该耐药基因在该个体中是否存在,超简单!

    #判断存在函数
    def exist():
        for k in forall[i]:
            if j == k:
                temp = 1
                break
            else:temp = 0
        return temp
    

    最后主程序以及写到csv中。

    for i in mylist:
        file=open(yourdir+i,'r')
        forall[i] =[]
        select(file)
        #print(forall)
        file.close()
    
    forall_name = []
    for i in forall.keys():
    
        forall_name.append(i)
    outgo = forall[forall_name[0]]
    for j in range(len(forall_name)):
        outgo = list(set(outgo).union(forall[forall_name[j]]))
        #print(outgo)
    
    outmat = OrderedDict()
    data = []
    for i in forall.keys():
        outmat[i] = []
        for j in outgo:
            ex = exist()
            outmat[i].append(ex)
        outmat[i].append(i)
        data.append(outmat[i])
       
         ##print(data)
    with open('test.csv', 'w', newline='') as t_file:
        csv_writer = csv.writer(t_file)
        csv_writer.writerow(outgo)
        for l in data:
           csv_writer.writerow(l)
        
    

    生成矩阵用EXCEL打开如下嘿嘿嘿,又可以画图了嘿嘿嘿。

    图片.png
    好吧原本刚开始是想引入json进行处理的,但后来发现好像又不是标准的json格式,咱手动搞吧,看需求嘛。
    图片.png
    毕竟我大杰尼龟都说好。
    resfinder教程(https://www.jianshu.com/p/136e56b16a5a

    相关文章

      网友评论

        本文标题:超简单python脚本实现从resfinder+blast下机数

        本文链接:https://www.haomeiwen.com/subject/focggctx.html