Rosalind Profile and Consensus: Writing long strings to one line in Python (Formatting)
Question: I'm trying to tackle a problem on Rosalind where, given a FASTA file of at
most 10 sequences at 1kb, I need to give the consensus sequence and profile
(how many of each base do all the sequences have in common at each
nucleotide). In the context of formatting my response, what I have as my code
works for small sequences (verified).

However, I have issues in formatting my response when it comes to large
sequences. What I expect to return, regardless of length, is:

    
    
    "consensus sequence"
    "A: one line string of numbers without commas"
    "C: one line string """" "
    "G: one line string """" "
    "T: one line string """" "
    

All aligned with each other and on their own respective lines, or at least
some formatting that allows me to carry this formatting as a unit onward to
maintain the integrity of aligning.

but when I run my code for a large sequence, I get each separate string below
the consensus sequence broken up by a newline, presumably because the string
itself is too long. I've been struggling to think of ways to circumvent the
issue, but my searches have been fruitless. I'm thinking about some iterative
writing algorithm that can just write the entirety of the above expectation
but in chunks Any help would be greatly appreciated. I have attached the
entirety of my code below for the sake of completeness, with block comments as
needed, though the main section.

    
    
    def cons(file):
    #returns consensus sequence and profile of a FASTA file
        import os
        path = os.path.abspath(os.path.expanduser(file))
    
        with open(path,"r") as D:
            F=D.readlines()
    
    #initialize list of sequences, list of all strings, and a temporary storage
    #list, respectively
        SEQS=[]
        mystrings=[]
        temp_seq=[]
    
    #get a list of strings from the file, stripping the newline character
        for x in F:
            mystrings.append(x.strip("\n"))
    
    #if the string in question is a nucleotide sequence (without ">")
    #i'll store that string into a temporary variable until I run into a string
    #with a ">", in which case I'll join all the strings in my temporary
    #sequence list and append to my list of sequences SEQS    
        for i in range(1,len(mystrings)):
            if ">" not in mystrings[i]:
                temp_seq.append(mystrings[i])
            else:
                SEQS.append(("").join(temp_seq))
                temp_seq=[]
        SEQS.append(("").join(temp_seq))
    
    #set up list of nucleotide counts for A,C,G and T, in that order
        ACGT=      [[0 for i in range(0,len(SEQS[0]))],
                    [0 for i in range(0,len(SEQS[0]))],
                    [0 for i in range(0,len(SEQS[0]))],
                    [0 for i in range(0,len(SEQS[0]))]]
    
    #assumed to be equal length sequences. Counting amount of shared nucleotides
    #in each column
        for i in range(0,len(SEQS[0])-1):
            for j in range(0, len(SEQS)):
                if SEQS[j][i]=="A":
                    ACGT[0][i]+=1
                elif SEQS[j][i]=="C":
                    ACGT[1][i]+=1
                elif SEQS[j][i]=="G":
                    ACGT[2][i]+=1
                elif SEQS[j][i]=="T":
                    ACGT[3][i]+=1
    
        ancstr=""
        TR_ACGT=list(zip(*ACGT))
        acgt=["A: ","C: ","G: ","T: "]
        for i in range(0,len(TR_ACGT)-1):
            comp=TR_ACGT[i]
            if comp.index(max(comp))==0:
                ancstr+=("A")
            elif comp.index(max(comp))==1:
                ancstr+=("C")
            elif comp.index(max(comp))==2:
                ancstr+=("G")
            elif comp.index(max(comp))==3:
                ancstr+=("T")
    
    '''
    writing to file... trying to get it to write as
    consensus sequence
    A: blah(1line)
    C: blah(1line)
    G: blah(1line)
    T: blah(line)
    which works for small sequences. but for larger sequences
    python keeps adding newlines if the string in question is very long...
    '''
    
    
        myfile="myconsensus.txt"
        writing_strings=[acgt[i]+' '.join(str(n) for n in ACGT[i] for i in      range(0,len(ACGT))) for i in range(0,len(acgt))]
        with open(myfile,'w') as D:
            D.writelines(ancstr)
            D.writelines("\n")
            for i in range(0,len(writing_strings)):
                D.writelines(writing_strings[i])
                D.writelines("\n")
    

cons("rosalind_cons.txt")


Answer: Your code is totally fine except for this line:

    
    
    writing_strings=[acgt[i]+' '.join(str(n) for n in ACGT[i] for i in      range(0,len(ACGT))) for i in range(0,len(acgt))]
    

You accidentally replicate your data. Try replacing it with:

    
    
    writing_strings=[ACGT[i] + str(ACGT[i]) for i in range(0,len(ACGT))]
    

and then write it to your output file as follows:

    
    
    D.write(writing_strings[i][1:-1])
    

That's a lazy way to get rid of the brackets from your list.

