美文网首页python模块
利用python中的pysam模块做一些简单的统计(3)

利用python中的pysam模块做一些简单的统计(3)

作者: 糕糕python | 来源:发表于2018-10-26 16:48 被阅读102次

在着手朋友的新需求前,发现了一些问题,比如说经常重复提取文本,我可不可以把提取后的序列单独保存起来,实现自动化提取bam文件序列?为了实现自己给自己提的需求,我顺便把文件操作复习一遍,毕竟还是以学习为主。

看了一下,实现序列自动化提取序列需要用到os,import之。

    def pysam_save(self,squence_number):  #用于提取序列的功能模块,输出TXT
        number =0
        compile = re.compile("/桌面/(.*).bam")
        filename = re.findall(compile,path_in)
        print(filename)
        for line in samfile:
            number += 1
            if number < squence_number:
                squence = (re.findall("[AGCT][AGCT][AGCT].*[AGCT][AGCT][AGCT]", str(line)))  # 提取目標序列
                str_squence = " ".join(squence)
                if len(str_squence) < 400:
                    with open("bam_squence", "a") as pysam_txt:
                        pysam_txt.write(str_squence)
                        pysam_txt.write("\n")
                        #print(os.path.abspath("bam_squence.txt"))
            else:
                ospath = "/home/charmflystar/Desktop/save_file_for_pycharm/gene_squence/bam_squence"
                ospath_new = "/home/charmflystar/Desktop/save_file_for_pycharm/gene_squence/%s"% filename
                os.rename(ospath, ospath_new)
                print("执行完毕")
                break

这块功能模块运行完之后,是这个样子的


智能化输出序列txt文本

有人可能会觉得,做这个有什么用呢,因为接下来如果要筛出符合要求的序列,那么就必须使用文件存储功能,方便后续比对序列,下面上的第二个功能:

    def AGCT_accuracy(self,squence_number): #用于统计A\G\C\T的碱基精准度,智能化输出到TXT
        number = 0
        for line in samfile:
            number += 1
            if number < squence_number:#控制输出结果条数,方便测试
                squence = re.findall("[AGCT][AGCT][AGCT].*[AGCT][AGCT][AGCT]",str(line)) #提取目標序列
                str_squence = " ".join(squence)#需要转化为str
                total_squence =len(str_squence) #統計序列長度
                if total_squence < 400: #這裏是因爲曬篩除則中有一些提取出問題的序列,原因不明
                    compile1 = re.compile("[A][A][A][A][A*]") #根据需求抓取含A的连续序列
                    A_squence = str(re.findall(compile1,str_squence))#同样需转化为str,以便以文本格式保存
                    compile2 = re.compile("/桌面/(.*).bam")#1.这段代码是用来智能化命名文本名称的
                    name = re.findall(compile2, path_in)#2.这段代码是用来智能化命名文本名称的
                    filename =" ".join(name)#3.这段代码是用来智能化命名文本名称的
                    #print(A_squence)
                    if "A" in A_squence:
                        with open("bam_squence", "a") as pysam_txt: #把筛出的符合序列写到记事本中
                            pysam_txt.write(str_squence)
                            pysam_txt.write("\n")
            else:
                ospath = "/home/charmflystar/Desktop/save_file_for_pycharm/gene_squence/bam_squence"
                ospath_new = "/home/charmflystar/Desktop/save_file_for_pycharm/gene_squence/%s" % filename #4.这段代码是用来智能化命名文本名称的
                os.rename(ospath, ospath_new)
                print("执行完毕")
                break

至此,需要的功能模块基本完成,剩下的就是和目标序列逐一比对,统计正确率


相关文章

网友评论

    本文标题:利用python中的pysam模块做一些简单的统计(3)

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