Sunday, 21 October 2012

PYTHON FUNCTION FUNCTION FOR DNA SEQUENCES

def is_valid_sequence(dna):
    '''(str) -> bool

    Return True if dna sequence is valid

    >>>is_valid_sequence('AAAAA')
    True
    >>>is_valid_sequence('TTTTT')
    True
    >>>is_valid_sequence('CCCCC')
    True
    >>>is_valid_sequence('GGGGG')
    True
    >>>is_valid_sequence('ATCGKHKD')
    False
    '''
    for char in dna:
        if char in 'ATCG':
            r=True
        else :
            return False
    return r

def insert_sequence(dna1,dna2,index):
    '''(str, str, int) -> str

    Return the DNA sequence obtained by inserting the second DNA sequence
    into the first DNA sequence at the given index

    >>>insert_sequence('CCGG', 'AT',2)
    CCATGG
    >>>insert_sequence('CCGT', 'CGA',3)
    '''
    return dna1[:index] + dna2 + dna1[index:]

def get_complement(nucleotide):
    '''(str)->str

    Return Complement of Given Nucleotide

    >>>get_complement('A')
    T
    >>>get_complement('C')
    G
    '''
    if nucleotide=='A':
        return 'T'
    elif nucleotide=='T':
        return 'A'
    elif nucleotide=='C':
        return 'G'
    elif nucleotide=='G':
        return 'C'
    else :
        return 'Invalid Sequence'


def get_complementary_sequence(sequence):
    '''(str)->str
   
    Return the DNA sequence that is complementary to the given DNA sequence.

    >>>get_complementary_sequence('AT')
    TA
    '''
    seq=''
    for char in sequence:
        seq=seq+get_complement(char)
    return seq

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel