Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.8 kB
2
Indexable
Never
import random 

class DNA:

  def __init__(self, n):
    '''intialzies a DNA object with length n and a randomly generated strand'''

    self.length = n
    self.strand = ""
    for i in range(n):
      self.strand += random.choice(['A', 'T', 'C', 'G'])


  def __str__(self):

    return self.strand + " of length " + str(self.length)

  def is_complement(self, other):
    complement_dict = {
      'A': 'T',
      'T': 'A', 
      'C': 'G',
      'G': 'C'        
      }

    if self.length != other.length:
      return False
    
    for base_1, base_2 in zip(self.strand, other.strand):
      if complement_dict[base_1] != base_2:
        return False
    return True      


def is_reverse(self, other):
  return self.strand == other.strand[::-1]

def main():

    DNA_objects = []

    while True: 
      try:
        
        n = int(input("Length of DNA strand? "))
        r = int(input("Number of strands to test? "))
        
        if n <= 0 or r <= 0:    
          raise ValueError("\nNumbers must be positive. Please Try again")

        break

      except ValueError as e:
        print(f"\n{e}")

      except Exeption as e:
        print("\nInvalid Input, please try again.")


    # use "_" as variable since it's not being used
    for _ in range(r):
      dna = DNA(n)
      print(f'Creating strand: {dna}')
      DNA_objects.append(dna)

    print("\nComplement test:")

    for strand in DNA_objects:
      for strand2 in DNA_objects:
        if strand.is_complement(strand2):
          print(f"{strand.strand} is the complement of {strand2.strand}")

    print("Done")

    for test in DNA_objects:
      for test2 in DNA_objects:
        if test.is_redisverse(test2):
          print(f"{test.strand} is the complement of {test2.strand}")
      
    
      

if __name__ == "__main__":
    main()