Bioinformatics Algorithms
Representing Sequences as Numbers
Back to Index
The two Python scripts displayed on this page perform the tasks of
converting a nucleotide sequence to a numeric value and converting a
numeric value to a nucleotide sequence.
P2N.py Script Outline
- P2N(p) - Recursive function which calls utility functions
- LastSymbol(p) - Utility function which returns the last character in a string
- Prefix(p) - Utility function which returns a string minus the last character
- S2N(s) - Utility function which converts a symbol (A,C,G,T) to a number
#!/usr/bin/python
def P2N(p):
if p == "":
return 0
symbol = LastSymbol(p)
prefix = Prefix(p)
return 4* P2N(prefix) + S2N(symbol)
def LastSymbol(p):
index = len(p)-1
return p[index]
def Prefix(p):
index = len(p)-1
return p[:index]
def S2N(s):
if s=="A":
return 0
elif s=="C":
return 1
elif s=="G":
return 2
else:
return 3
seq = raw_input("ENTER STRING OF A, C, G, T: ")
num = P2N(seq)
print num
N2P.py Script Outline
- N2P(i,k) - Recursive function which calls utility functions
- Q(i,d) - Utility function which performs integer division
- REM(i,d) - Utility function which performs modulus division
- N2S(s) - Utility function which converts a number to a symbol
#!/usr/bin/python
def N2P(i,k):
if k==1:
return N2S(i)
pref = Q(i,4)
r = REM(i,4)
symbol = N2S(r)
pP = N2P(pref,k-1)
return pP + symbol
def Q(i,d):
return i//d
def REM(i,d):
return i%d
def N2S(r):
if r==0:
return "A"
elif r==1:
return "C"
elif r==2:
return "G"
else:
return "T"
nums = raw_input("ENTER SEQ VALUE: ")
lens = raw_input("ENTER SEQ LENGTH: ")
nums = int(nums)
lens = int(lens)
seq = N2P(nums,lens)
print seq
Main Index