• python 读入test2.fa,把每条FASTA序列连成一行然后输出


    001、方法1

    (base) root@PC1:/home/test2# ls
    a.fasta  test.py
    (base) root@PC1:/home/test2# cat a.fasta              ## 测试fasta文件
    >gene1 myc
    AGCTGCCTAAGC
    GGCATAGCTAATCG
    >gene2 jun
    ACCGAATCGGAGCGATG
    GGCATTAAAGATCTAGCT
    >gene3 malat1
    AGGCTAGCGAG
    GCGCGAG
    GATTAGGCG
    (base) root@PC1:/home/test2# cat test.py                ## 测试程序
    #!/usr/bin/python
    
    in_file = open("a.fasta", "r")
    
    idx = 0
    for i in in_file:
        i = i.strip()
        if i[0] == ">":
            if idx > 0:
                print("")
            idx += 1
            print(i.split(" ")[0].split(">")[1])
        else:
            print(i, end = "")
    print("")
    
    in_file.close()
    (base) root@PC1:/home/test2# python test.py           ## 执行程序
    gene1
    AGCTGCCTAAGCGGCATAGCTAATCG
    gene2
    ACCGAATCGGAGCGATGGGCATTAAAGATCTAGCT
    gene3
    AGGCTAGCGAGGCGCGAGGATTAGGCG

    002、方法2

    (base) root@PC1:/home/test2# ls
    a.fasta  test.py
    (base) root@PC1:/home/test2# cat a.fasta          ## 测试fasta文件
    >gene1 myc
    AGCTGCCTAAGC
    GGCATAGCTAATCG
    >gene2 jun
    ACCGAATCGGAGCGATG
    GGCATTAAAGATCTAGCT
    >gene3 malat1
    AGGCTAGCGAG
    GCGCGAG
    GATTAGGCG
    (base) root@PC1:/home/test2# cat test.py           ## 测试程序
    #!/usr/bin/python
    
    in_file = open("a.fasta", "r")
    dict1 = {}
    for i in in_file:
        i = i.strip()
        if i[0] == ">":
            key = i.split(" ")[0].split(">")[1]
            dict1[key] = ""
        else:
            dict1[key] += i
    for i,j in dict1.items():
        print(i)
        print(j)
    in_file.close()
    (base) root@PC1:/home/test2# python test.py       ## 执行程序
    gene1
    AGCTGCCTAAGCGGCATAGCTAATCG
    gene2
    ACCGAATCGGAGCGATGGGCATTAAAGATCTAGCT
    gene3
    AGGCTAGCGAGGCGCGAGGATTAGGCG

    参考:https://www.jianshu.com/p/403a23fdd7bb

  • 相关阅读:
    创建精灵--九宫格
    精灵灰化
    根据点中坐标,调整怪物动作方向
    从tableview中拖动某个精灵
    CCardSlip
    CCImage
    cocos2d-x---CCLabelTTF加载字体库
    CCAction、CCFiniteTimeAction、CCSpeed、CCFollow
    CCObject
    html中charset和content-language的区别
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/16583756.html
Copyright © 2020-2023  润新知