Advertisement
tom_enos

a2

Sep 25th, 2013
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. def get_length(dna):
  2.     """ (str) -> int
  3.  
  4.    Return the length of the DNA sequence dna.
  5.  
  6.    >>> get_length('ATCGAT')
  7.    6
  8.    >>> get_length('ATCG')
  9.    4
  10.    """
  11.     return len(dna)
  12.  
  13. def is_longer(dna1, dna2):
  14.     """ (str, str) -> bool
  15.  
  16.    Return True if and only if DNA sequence dna1 is longer than DNA sequence
  17.    dna2.
  18.  
  19.    >>> is_longer('ATCG', 'AT')
  20.    True
  21.    >>> is_longer('ATCG', 'ATCGGA')
  22.    False
  23.    """
  24.     return len(dna1) > len(dna2)
  25.  
  26. def count_nucleotides(dna, nucleotide):
  27.     """ (str, str) -> int
  28.  
  29.    Return the number of occurrences of nucleotide in the DNA sequence dna.
  30.  
  31.    >>> count_nucleotides('ATCGGC', 'G')
  32.    2
  33.    >>> count_nucleotides('ATCTA', 'G')
  34.    0
  35.    """
  36.     return dna.count(nucleotide)
  37.  
  38. def contains_sequence(dna1, dna2):
  39.     """ (str, str) -> bool
  40.  
  41.    Return True if and only if DNA sequence dna2 occurs in the DNA sequence
  42.    dna1.
  43.  
  44.    >>> contains_sequence('ATCGGC', 'GG')
  45.    True
  46.    >>> contains_sequence('ATCGGC', 'GT')
  47.    False
  48.  
  49.    """
  50.     return dna1.count(dna2) > 0
  51.  
  52. def is_valid_sequence(dna):
  53.     """
  54.    (str) -> bool
  55.    
  56.    Return True if and only if the DNA sequence is valid
  57.    
  58.    >>> is_valid_sequence('ACCGT')
  59.    True
  60.    >>> is_valid_sequence('ACCDT')
  61.    Fale
  62.    
  63.    """
  64.     for nucleotide in dna:
  65.         if nucleotide not in "ATCG":
  66.             return False
  67.     return True
  68.  
  69. def insert_sequence(dna1, dna2, loc):
  70.     """
  71.    (str, str, int) -> str
  72.    
  73.    Return the DNA sequence obtained by inserting the second DNA sequence into
  74.    the first DNA sequence at the given index.
  75.    precondition: You can assume that the index is valid.
  76.    
  77.    >>> insert_sequence('ATGC', 'GG', 2)
  78.    'ATGGGC'
  79.    >>> insert_sequence('ATGCCGTA', 'CCGGAA', 5)
  80.    'ATGCCCCGGAAGTA'
  81.    
  82.    """
  83.     return dna1[0:loc] + dna2 + dna1[loc:]
  84.  
  85. def get_complement(nucleotide):
  86.     """
  87.    (str) -> str
  88.  
  89.    Return the nucleotide's complement.
  90.    
  91.    >>> get_complement('A')
  92.    'T'
  93.    >>> get_complement('a')
  94.    'a'
  95.    
  96.    """
  97.     if nucleotide == 'A':
  98.       return 'T'
  99.     elif nucleotide == 'T':
  100.       return 'A'
  101.     elif nucleotide == 'G':
  102.       return 'C'
  103.     elif nucleotide == 'C':
  104.       return 'G'
  105.     return nucleotide
  106.  
  107. def get_complementary_sequence(dna):
  108.     """
  109.    (str) -> str
  110.    
  111.    Return the DNA sequence that is complementary to the given DNA sequence.
  112.    
  113.    >>> get_complementary_sequence('TACG')
  114.    'ATGC'
  115.    >>> get_complementary_sequence('AtGD')
  116.    'TtCD'
  117.    
  118.    """
  119.     sequence = ''
  120.     for nucleotide in dna:
  121.         sequence += get_complement(nucleotide)
  122.     return sequence
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement