HeidiHeff

DNA

Oct 17th, 2012
2,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. # DNA.py
  2. # Heidi Heffelfinger
  3. # October 17, 2012
  4. # Learn to Program - Coursera - University of Toronto
  5. # Week 4 Assignment
  6. # Python 3.2
  7.  
  8. # This program examines DNA sequences, checks for validity, and makes
  9. # modifications to DNA sequences.
  10.  
  11. def get_length(dna):
  12.     ''' (str) -> int
  13.  
  14.    Return the length of the DNA sequence dna.
  15.  
  16.    >>> get_length('ATCGAT')
  17.    6
  18.    >>> get_length('ATCG')
  19.    4
  20.    >>> get_length('CGATAGCT')
  21.    8
  22.    >>> get_length('CG')
  23.    2
  24.    '''
  25.     return len(dna)
  26.  
  27.  
  28. def is_longer(dna1, dna2):
  29.     ''' (str, str) -> bool
  30.  
  31.    Return True if and only if DNA sequence dna1 is longer than DNA sequence
  32.    dna2.
  33.  
  34.    >>> is_longer('ATCG', 'AT')
  35.    True
  36.    >>> is_longer('ATCG', 'ATCGGA')
  37.    False
  38.    >>> is_longer('ATCG', 'CGATAGCT')
  39.    False
  40.    >>> is_longer('ATCGAT', 'CGA')
  41.    True
  42.    '''
  43.     return get_length(dna1) > get_length(dna2)
  44.  
  45.  
  46. def count_nucleotides(dna, nucleotide):
  47.     ''' (str, str) -> int
  48.  
  49.    Return the number of occurrences of nucleotide in the DNA sequence dna.
  50.  
  51.    >>> count_nucleotides('ATCGGC', 'G')
  52.    2
  53.    >>> count_nucleotides('ATCTA', 'G')
  54.    0
  55.    >>> count_nucleotides('CGATAGCT', 'T')
  56.    2
  57.    >>> count_nucleotides('ATCGGA', 'C')
  58.    1
  59.    '''
  60.     return dna.count(nucleotide)
  61.  
  62.    
  63. def contains_sequence(dna1, dna2):
  64.     ''' (str, str) -> bool
  65.  
  66.    Return True if and only if DNA sequence dna2 occurs in the DNA sequence
  67.    dna1.
  68.  
  69.    >>> contains_sequence('ATCGGC', 'GG')
  70.    True
  71.    >>> contains_sequence('ATCGGC', 'GT')
  72.    False
  73.    >>> contains_sequence('ATCGGC', 'CG')
  74.    True
  75.    >>> contains_sequence('ATCGGC', 'AT')
  76.    True
  77.    >>> contains_sequence('ATCGGC', 'AG')
  78.    False
  79.    '''
  80.     return dna2 in dna1
  81.  
  82. def is_valid_sequence(dna):
  83.     ''' (str) -> bool
  84.  
  85.    Return True if dna sequence is valid, containing only nucleotide
  86.    characters: 'A', 'T', 'C' and 'G'.
  87.  
  88.    >>> is_valid_sequence('ATCGGC')
  89.    True
  90.    >>> is_valid_sequence('ATcGGC')
  91.    False
  92.    >>> is_valid_sequence('CGATAGCT')
  93.    True
  94.    >>> is_valid_sequence('ABCDEFG')
  95.    False
  96.    >>> is_valid_sequence('atcggc')
  97.    False
  98.    >>> is_valid_sequence('CGATAGCT')
  99.    True
  100.    >>> is_valid_sequence('A')
  101.    True
  102.    '''
  103.     dna_nucleotides = 'ATCG'
  104.     for nucleotide in dna:
  105.         if nucleotide not in dna_nucleotides:
  106.             return False
  107.     return True
  108.  
  109. def insert_sequence(dna1, dna2, insertion):
  110.     '''(str, str, int) -> str
  111.  
  112.    Returns DNA sequence obtained by inserting dna2 into dna1 at insertion point.
  113.  
  114.    >>> insert_sequence('CTGATAGCT', 'ATCGGC', 2)
  115.    CTATCGGCGATAGCT
  116.    >>> insert_sequence('ATCGGC', 'CG', 4)
  117.    ATCGCGGC
  118.    >>> insert_sequence('ATCGGC', 'TA', 1)
  119.    ATATCGGC
  120.    '''
  121.  
  122.     new_dna = dna1[:insertion] + dna2 + dna1[insertion:]
  123.     return new_dna
  124.  
  125. def get_complement(nucleotide):
  126.     '''(str) -> str
  127.  
  128.    Returns complement (A, T, C, G) of nucleotide (A, T, C, G) where A is
  129.    complement of T (& vice-versa) and C is complement of G (& vice versa).
  130.  
  131.    Precondition - nucleotide must be A, T, C, or G.
  132.    
  133.    >>> get_complement('A')
  134.    T
  135.    >>> get_complement('T')
  136.    A
  137.    >>> get_complement('C')
  138.    G
  139.    >>> get_complement('G')
  140.    C
  141.    '''
  142.     if nucleotide == 'A':
  143.         return 'T'
  144.     elif nucleotide == 'T':
  145.         return 'A'
  146.     elif nucleotide == 'C':
  147.         return 'G'
  148.     elif nucleotide == 'G':
  149.         return 'C'
  150.  
  151. def get_complementary_sequence(dna):
  152.     '''(str) -> str
  153.  
  154.    Returns DNA sequence that is complementary to given DNA sequence of dna.
  155.    
  156.    >>> get_complementary_sequence('ACGTACG')
  157.    TGCATGC
  158.    >>> get_complementary_sequence('CGATAGCT')
  159.    GCTATCGA
  160.    >>> get_complementary_sequence('ATCG')
  161.    TAGC
  162.    >>> get_complementary_sequence('ATCTA')
  163.    TAGAT
  164.    '''
  165.     complement = ''
  166.     for nucleotide in dna:
  167.         complement += get_complement(nucleotide)
  168.     return complement
Advertisement
Add Comment
Please, Sign In to add comment