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
#!/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
#!/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

SOURCE CODE:
Pattern to Numeric Value
Numeric Value to Pattern

Main Index