• python 中将fastq文件保存为字典


    001、保存为字典

    (base) root@PC1:/home/test2# ls
    a.fastq  test.py
    (base) root@PC1:/home/test2# cat a.fastq                        ## 测试fastq文件
    @SRR1596091.1 HISEQ:62:C35RDACXX:2:1101:1546:1996/1
    GGAAAATAAACATCAGTTTCTTCCAATGAAATGTCTCTCATTCTATCAAC
    +
    BBBFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIIIFIIF
    @SRR1596091.2 HISEQ:62:C35RDACXX:2:1101:1511:1997/1
    CAAGCCACATCTAACAACAATATTAACTATCCAGAAAGGAGCCAGTTATC
    +
    BBBFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
    @SRR1596091.3 HISEQ:62:C35RDACXX:2:1101:2357:1925/1
    NACGGAGCTGAGCTGGATTTCAGGAGACCTCCTGCCCAGCAGTCAGGAAG
    +
    #0<FFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
    (base) root@PC1:/home/test2# cat test.py                          ## 测试脚本
    #!/usr/bin/python
    in_file = open("a.fastq", "r")
    
    dict1 = dict()
    n  = 0
    for i in in_file:
        n += 1
        i = i.strip()
        if n % 4 == 1:
            key = i
            dict1[key] = ""
        if n % 4 == 2:
            dict1[key] += i
    print(dict1)
    
    in_file.close()
    (base) root@PC1:/home/test2# python test.py                        ## 程序执行结果
    {'@SRR1596091.1 HISEQ:62:C35RDACXX:2:1101:1546:1996/1': 'GGAAAATAAACATCAGTTTCTTCCAATGAAATGTCTCTCATTCTATCAAC', '@SRR1596091.2 HISEQ:62:C35RDACXX:2:1101:1511:1997/1': 'CAAGCCACATCTAACAACAATATTAACTATCCAGAAAGGAGCCAGTTATC', '@SRR1596091.3 HISEQ:62:C35RDACXX:2:1101:2357:1925/1': 'NACGGAGCTGAGCTGGATTTCAGGAGACCTCCTGCCCAGCAGTCAGGAAG'}

    002、保存为文件

    (base) root@PC1:/home/test2# ls
    a.fastq  test.py
    (base) root@PC1:/home/test2# cat a.fastq             ## 测试fastq文件
    @SRR1596091.1 HISEQ:62:C35RDACXX:2:1101:1546:1996/1
    GGAAAATAAACATCAGTTTCTTCCAATGAAATGTCTCTCATTCTATCAAC
    +
    BBBFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIIIFIIF
    @SRR1596091.2 HISEQ:62:C35RDACXX:2:1101:1511:1997/1
    CAAGCCACATCTAACAACAATATTAACTATCCAGAAAGGAGCCAGTTATC
    +
    BBBFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
    @SRR1596091.3 HISEQ:62:C35RDACXX:2:1101:2357:1925/1
    NACGGAGCTGAGCTGGATTTCAGGAGACCTCCTGCCCAGCAGTCAGGAAG
    +
    #0<FFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
    (base) root@PC1:/home/test2# cat test.py            ## 测试程序
    #!/usr/bin/python
    in_file = open("a.fastq", "r")
    out_file = open("result.txt", "w")
    n = 0
    
    for i in in_file:
        n += 1
        if n % 4 == 1:
            out_file.write(i)
        if n % 4 == 2:
            out_file.write(i)
    
    in_file.close()
    out_file.close()
    (base) root@PC1:/home/test2# python test.py          ## 执行程序
    (base) root@PC1:/home/test2# ls
    a.fastq  result.txt  test.py
    (base) root@PC1:/home/test2# cat result.txt          ## 程序运行结果
    @SRR1596091.1 HISEQ:62:C35RDACXX:2:1101:1546:1996/1
    GGAAAATAAACATCAGTTTCTTCCAATGAAATGTCTCTCATTCTATCAAC
    @SRR1596091.2 HISEQ:62:C35RDACXX:2:1101:1511:1997/1
    CAAGCCACATCTAACAACAATATTAACTATCCAGAAAGGAGCCAGTTATC
    @SRR1596091.3 HISEQ:62:C35RDACXX:2:1101:2357:1925/1
    NACGGAGCTGAGCTGGATTTCAGGAGACCTCCTGCCCAGCAGTCAGGAAG

    参考:https://mp.weixin.qq.com/s?__biz=MzkyMTI1MTYxNA==&mid=2247493739&idx=1&sn=f690c93761307e6ec9bb77cca2eb4619&chksm=c184d21af6f35b0cda1d964ed896adee1091e1f615b7f6be0caf2508105275ca3ae66889c58e&mpshare=1&scene=23&srcid=0811LY0ghlyV0yNXki8WcW6m&sharer_sharetime=1660215059305&sharer_shareid=50b75c6a886e09824b582fb782a7678b#rd

  • 相关阅读:
    从关系型数据库到非关系型数据库
    2016某知名互联网公司PHP面试题及答案
    企业网站核心关键词如何去选择
    写Seo网站标题应该注意什么
    什么样的外链才是优质外链
    什么是网站物理链接结构
    需要分析竞争对手的网站哪些SEO数据
    做外链的时候应该需要注意什么
    描述标签对关键词排名有影响吗
    网站外链对排名的影响有哪些
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/16577791.html
Copyright © 2020-2023  润新知