I am the author of this package that turns DNA sequences into two dimensional visualizations. DNA, for the unaware, consists of four letters (A, T, G, and C). To convert the sequence, I am using this code:
import numpy as np
def transform(sequence):
running_value = 0
x, y = np.linspace(0, len(sequence), 2 * len(sequence) + 1), [0]
for character in sequence:
if character == "A":
y.extend([running_value + 0.5, running_value])
elif character == "C":
y.extend([running_value - 0.5, running_value])
elif character == "T":
y.extend([running_value - 0.5, running_value - 1])
running_value -= 1
elif character == "G":
y.extend([running_value + 0.5, running_value + 1])
running_value += 1
else:
y.extend([running_value] * 2)
return list(x), y
Note that seq
may be a very long sequence, and this transformation process may be done (hundreds of) thousands of time.
What are some things I could do to improve performance?
Edit:
The
else
clause is the because some DNA sequences have ambiguity (i.e. non ATGC characters) which must be accounted for as horizontal lines.sequence
may safely assumed to be a string. For example, "GATTACA" is a valid DNA sequence that should return([0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0], [0, 0.5, 1, 1.5, 1, 0.5, 0, -0.5, -1, -0.5, -1, -1.5, -1, -0.5, -1])
.By "long", I mean anywhere from thousands to billions. That being said, the most common use case likely will involve fewer than ten thousand characters, although this process may be done to thousands of such sequences. For context, I am currently running this function over 400,000 sequences with a median length of 800 characters.
else
? Is it ever used? \$\endgroup\$X
, or one of a few characters? \$\endgroup\$