I know for the reverse procedure, there are lots of methods available. But how can I change my fasta file with sequences like:

    >gi|189094002
    MDDAELNAIRQARLAELQRNAAGGGSSTNPSSSSSGGAQDSAQENMTITILNRVLTNEARERLSRQQTKITFNRKNIAADDDEDDDDFFD
    >gi|68485955
    MDDAELNAIRQARLAELQRNAAGGGSSTNPSSSSSGGAQDSAQENMTITILNRVLTNEARERLSRVKIVRRDSQQKQQTKITFNRKNIAGDDEDDDDFFD

to like:




    >gi|189094002
    MDDAELNAIRQARLAELQRNAAGGGSSTNPSSSSSGGAQDSAQENMTITILNRVLTNEARERLSR
    QQTKITFNRKNIAADDDEDDDDFFD
    >gi|68485955
    MDDAELNAIRQARLAELQRNAAGGGSSTNPSSSSSGGAQDSAQENMTITILNRVLTNEARERLSR
    VKIVRRDSQQKQQTKITFNRKNIAGDDEDDDDFFD

I know its just about inserting line breaks in each sequence, but  is there any way that I can check whether the fasta file is single line fasta or not, and if is, add line breaks in each sequence..

Ps. I am using windows..

Thanks for your consideration







Here's a quick one in python:


    import sys
    
    inputfile = sys.argv[1]
    length = int(sys.argv[2])
    
    print '\nConverting ' + inputfile + ' to ' + str(length) + ' chars per line...'
    
    with open(inputfile, 'r') as f:
    
         h = []                                             #headers list
         s = []                                             #sequence list
         for line in f:
               if line.startswith(">"):                      #grab headers
                   h.append(line.strip().split(">")[1])
              else:
                   s.append(line.strip())                   #grab sequences

    myseqs = dict(zip(h,s))                                 #map to dictionary, header:sequence
    
    outfile = open(inputfile.split(".fasta")[0] + '_multi-line.fasta', 'w') #open outfile for writing
    
    def print_seqs(header, sequence):
            print >> outfile, '>' + header
            while len(sequence) > 0:
                    print >> outfile, sequence[:length]
                    sequence = sequence[length:]
    
    for i in myseqs:
            print_seqs(i, myseqs[i])
    print 'Converted ' + str(len(myseqs)) + ' sequences.'
      print 'Done.\n'

    outfile.close()

Run as '*python your_input_fasta Nchars'*

**EDIT**

A cleaner, more memory efficient solution, per WouterDeCoster comment:

    import sys
    
    inputfile = sys.argv[1]
    length = int(sys.argv[2])
    
    outfile = open(inputfile.split(".fasta")[0] + '_multi-line.fasta', 'w') #open outfile for writing
    
    with open(inputfile, 'r') as f:
         for line in f:
            if line.startswith(">"):
                    print >> outfile, line.strip()
            else:
                    sequence = line.strip()
                    while len(sequence) > 0:
                            print >>outfile, sequence[:length]
                            sequence = sequence[length:]