Thank you for this resource!
Hi, I have a text file with a long list of DNA sequences.
I would like to convert them all to the same length, with that length being the longest sequences. "D's" should be added to those sequences that are shorter.
Is there anyway to do this in R or Biophython, some script like:
1) Read sequences and find longest sequence
2) Loop through each sequence adding "D"s to match the length of the longest sequence
I was looking through the APE package in R as I imagine something must exist already to accomplish this.
Any help with be appreciated.
2 answers
Hello, here is a quick and dirty solution in Python (for an input file named foo.fasta):
maxl = 0
for line in open('foo.fasta'):
if '>' not in line:
if len(line) > maxl:
maxl = len(line)
for line in open('foo.fasta'):
if '>' not in line:
print line.replace('\n','') + 'D'*(maxl - len(line))
else:
print line,
Hope it helps!
PS: the script assumes you have no other text than FASTA lines, and that your sequences are formatted as single lines.
See this post.
How to copy all fasta-seqs from fasta-files with the seq-lengths between minlen and maxlen
There are many helpful script vertions inside.
I am using lh3-script in Perl.
It's almost what you need, isn't it?
Log in to answer this question.
Is your file in fasta format? Not clear from your question.
No, the file is not yet in fasta format. Just a text document.