Last active
August 15, 2016 17:17
-
-
Save sbarratt/03b3b04f6b779c0570b0b28958d1a860 to your computer and use it in GitHub Desktop.
Implementation of the 3-Base Periodicity Property: https://en.wikipedia.org/wiki/3-Base_Periodicity_Property
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
from numpy.fft import fft | |
from numpy.fft import fftshift | |
import numpy as np | |
import random | |
dictionary = ['A','C','G','T'] | |
def generate_random_sequence(N): | |
return [dictionary[random.randint(0,len(dictionary)-1)] for _ in range(N)] | |
def char_to_num_seq(s): | |
N = len(s) | |
u_a, u_c, u_g, u_t = np.zeros(N), np.zeros(N), np.zeros(N), np.zeros(N) | |
for i in range(len(s)): | |
if s[i] == 'A': | |
u_a[i] = 1 | |
elif s[i] == 'C': | |
u_c[i] = 1 | |
elif s[i] == 'G': | |
u_g[i] = 1 | |
elif s[i] == 'T': | |
u_t[i] = 1 | |
return u_a, u_c, u_g, u_t | |
def dft(x): | |
N = x.shape[0] | |
return np.linspace(-np.pi, np.pi, N), fftshift(fft(x)) | |
rand_seq = generate_random_sequence(300) | |
u_a, u_c, u_g, u_t = char_to_num_seq(rand_seq) | |
print reduce(lambda a,b: a+b, rand_seq)[:50] | |
plt.stem(u_a[:50]) | |
plt.show() | |
plt.stem(u_c[:50]) | |
plt.show() | |
plt.stem(u_g[:50]) | |
plt.show() | |
plt.stem(u_t[:50]) | |
plt.show() | |
u_a -= np.mean(u_a) | |
u_c -= np.mean(u_c) | |
u_g -= np.mean(u_g) | |
u_t -= np.mean(u_t) | |
f, U_a = dft(u_a) | |
f, U_c = dft(u_c) | |
f, U_g = dft(u_t) | |
f, U_t = dft(u_t) | |
S = np.abs(U_a)**2 + np.abs(U_c)**2 + np.abs(U_g)**2 + np.abs(U_t)**2 | |
plt.plot(f, S) | |
print "power at N/3", S[int(N*1.0/2+N*1.0/6)] | |
print "average power", np.sum(S)/N |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment