Advertisement
andruhovski

ltp-a2

Sep 15th, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 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.  
  14. def is_longer(dna1, dna2):
  15.     """ (str, str) -> bool
  16.  
  17.    Return True if and only if DNA sequence dna1 is longer than DNA sequence
  18.    dna2.
  19.  
  20.    >>> is_longer('ATCG', 'AT')
  21.    True
  22.    >>> is_longer('ATCG', 'ATCGGA')
  23.    False
  24.    """
  25.     return len(dna1)>len(dna2)
  26.  
  27.  
  28. def count_nucleotides(dna, nucleotide):
  29.     """ (str, str) -> int
  30.  
  31.    Return the number of occurrences of nucleotide in the DNA sequence dna.
  32.  
  33.    >>> count_nucleotides('ATCGGC', 'G')
  34.    2
  35.    >>> count_nucleotides('ATCTA', 'G')
  36.    0
  37.    """
  38.     return dna.count(nucleotide)
  39.  
  40.  
  41. def contains_sequence(dna1, dna2):
  42.     """ (str, str) -> bool
  43.  
  44.    Return True if and only if DNA sequence dna2 occurs in the DNA sequence
  45.    dna1.
  46.  
  47.    >>> contains_sequence('ATCGGC', 'GG')
  48.    True
  49.    >>> contains_sequence('ATCGGC', 'GT')
  50.    False
  51.  
  52.    """
  53.     return dna2 in dna1
  54.  
  55. def is_valid_sequence(dna):
  56.            
  57.         nucleotide = 'ATCG'
  58.         for d in dna:
  59.             if d not in nucleotide:
  60.                 return False
  61.         return True
  62.  
  63. def insert_sequence(dna1, dna2, index):
  64.     """ (str, str, int) -> str
  65.  
  66.   Return the DNA sequence obtained by inserting the second DNA sequence into the
  67.   first DNA sequence at the given index.
  68.  
  69.   >>>insert_sequence('CCGG', 'AT', 2)
  70.   CCATGG
  71.   >>>insert_sequence('CON', 'PO', 2)
  72.   COPON
  73.  
  74.   """
  75.    
  76.     return dna1[:index] + dna2 + dna1[index:]
  77.  
  78.  
  79.  
  80. def get_complement(dna):
  81.     """ (str) -> str
  82.  
  83.   Return the nucleotide's complement ('A', 'T', 'C', 'G')
  84.  
  85.   >>>get_complement('PATA')
  86.   'TAT'
  87.   >>>get_complement('CGCG')
  88.   'GCGC'
  89.  
  90.   """
  91.  
  92.     nucletoide = ''
  93.  
  94.    
  95.     for char in dna:
  96.         if char == 'A':
  97.             nucletoide = nucletoide + 'T'
  98.         elif char == 'T':
  99.             nucletoide = nucletoide + 'A'
  100.         elif char == 'C':
  101.             nucletoide = nucletoide + 'G'
  102.         elif char == 'G':
  103.             nucletoide = nucletoide + 'C'
  104.    
  105.     return nucletoide
  106.  
  107. def get_complementary_sequence(dna):
  108.     """ (str) -> str
  109.  
  110.   Return the DNA sequence that is complementary to the given DNA
  111.   sequence.
  112.  
  113.   >>>get_complementary_sequence('AT')
  114.   'TA'
  115.   >>>get_complementary_sequence('GC')
  116.   'CG'
  117.  
  118.   """
  119.  
  120.     nucletoide = ''
  121.    
  122.     for char in dna:
  123.         if char == 'A':
  124.             nucletoide = nucletoide + 'T'
  125.         elif char == 'T':
  126.             nucletoide = nucletoide + 'A'
  127.         elif char == 'C':
  128.             nucletoide = nucletoide + 'G'
  129.         elif char == 'G':
  130.             nucletoide = nucletoide + 'C'
  131.    
  132.     return nucletoide
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement