At some point I went through a mental exercise to think about working with DNA in Python.  I imagined what generating DNA strings would look like and then bringing two parents together to create a child.  Here is the output of that thought process.

Note: This is probably inaccurate in the real world, but a nifty thing to think about on a rainy Saturday.

I uploaded this to my Github account to help out.

``````import random

# Generates two parents and then mashes them together based on a random number
# this will print out a new DNA sequence
# based on the two parent's random binary string
# Purely just for fun playing with random psuedo DNA sequences.

def get_dna():
""" Generate a random DNS sequence """
dna = ['A', 'G', 'C', 'T']
random_sequence = ''
for i in range(0, 81):
random_sequence += random.choice(dna)
# print(random_sequence)
return random_sequence

def main():
# Generate the parents
parent1 = get_dna()
print(f'DNA Sequence Parent 1: {parent1}')
parent2 = get_dna()
print(f'DNA Sequence Parent 2: {parent2}')

# Choose random number up to 27 digits when converted to binary
random_number = random.randint(0, 134217727)

# Example: Hard set the random number for testing
# random_number = 21123

# Convert to binary 1's and 0's
rand_binary = str(bin(random_number))[2:].zfill(27)
# Setup our sequence
# DNA has 3 characters
first_number = 0
second_number = 3
count = 0
# Setup and  fill our parent lists
p0 = [parent1[0:3]]
p1 = [parent2[0:3]]

while count <= 25:
first_number += 3
second_number += 3
p0.append(parent1[first_number:second_number])
p1.append(parent2[first_number:second_number])
count += 1

new_child_array = []
# Setup the child based on the 1's and 0's of the random binary above
for digit, index in enumerate(rand_binary):
if int(index) > 0:
new_child_array.append(p1[digit])
else:
new_child_array.append(p0[digit])
# Make child
new_child = ''.join(map(str, new_child_array))

print('Parent 1 DNA Sequence broken out: {}'.format(p0))
print('----')

print('Parent 2 DNA Sequence broken out: {}'.format(p1))
print('----')

print('The new child DNA from these parents: {}'.format(new_child))
# return the child
return new_child

if __name__ == '__main__':
main()
``````

My blog posts tagged with "Python Patterns" are designed to be a quick look reference for some Python code snippets I use a lot.  They are written to be a quick starting point for future projects so I do not need to type as much.