Callen Bragdon Callen Bragdon - 1 year ago 101
Python Question

Rosalind Profile and Consensus: Writing long strings to one line in Python (Formatting)

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:

#initialize list of sequences, list of all strings, and a temporary storage
#list, respectively

#get a list of strings from the file, stripping the newline character
for x in F:

#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]:

#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":
elif SEQS[j][i]=="C":
elif SEQS[j][i]=="G":
elif SEQS[j][i]=="T":

acgt=["A: ","C: ","G: ","T: "]
for i in range(0,len(TR_ACGT)-1):
if comp.index(max(comp))==0:
elif comp.index(max(comp))==1:
elif comp.index(max(comp))==2:
elif comp.index(max(comp))==3:

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...

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:
for i in range(0,len(writing_strings)):


Answer Source

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:


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