How to use .split

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bb64
    New Member
    • Sep 2010
    • 7

    How to use .split

    I need to write a Python script to convert two sequences to interleaved FASTA format with 10 characters per line.
    so if the input is

    >human
    ACACCGGTACCAGAT ATGATATACCGAGA
    >mouse
    ACCAGAGGGGGTTTT AAACCACAGCG

    (saved as dna.txt)
    the output should be
    >human
    ACACCGGTAC
    CAGATATGAT
    ATACCGAGA
    >mouse
    ACCAGAGGGG
    GTTTTAAACC
    ACAGCG


    i have no idea what to do.
    this is all i have so far
    Code:
    def read_data(filename):
        with open("p:/dna.txt", "r") as myfile:
            data = myfile.readlines()
            myfile.close()
        for i in range(0, len(data)):
            data[i] = data[i].rstrip("\n")
    
        return data
    
    seq1 = data[1]
    seq2 = data[3]
    Last edited by bvdet; Nov 8 '10, 12:32 AM. Reason: Add code tags, fix indentation
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Maybe this will help:
    Code:
    >>> import textwrap
    >>> textwrap.wrap("ACCAGAGGGGGTTTTAAACCACAGCG", 10)
    ['ACCAGAGGGG', 'GTTTTAAACC', 'ACAGCG']
    >>> data = [">human", "ACACCGGTACCAGATATGATATACCGAGA", ">mouse", "ACCAGAGGGGGTTTTAAACCACAGCG"]
    >>> for item in data:
    ... 	print "\n".join([s for s in textwrap.wrap(item, 10)])
    ... 	
    >human
    ACACCGGTAC
    CAGATATGAT
    ATACCGAGA
    >mouse
    ACCAGAGGGG
    GTTTTAAACC
    ACAGCG
    >>>

    Comment

    Working...