Genera tutte le permutazioni con al massimo d Mismatches
Stavo risolvendo un problema per il pattern matching con distanza di martellamento fino a d per una sequenza di DNA. Regex mi ha salvato lì. Ma ora ho riscontrato un problema diverso. Data una lunga sequenza di DNA, devo trovare k-mer non corrispondenti più frequenti con al massimo d mancate corrispondenze. Qui, k-mer si riferisce alla sotto-sequenza di lunghezza k.
Nota: la sequenza del DNA può essere rappresentata utilizzando solo quattro lettere: {A, C, G, T}
Per esempio,
DNA sequence= "AGGC"
k = 3
d = 1
Qui sono possibili solo due k-mers: "AGG", "GGC"
Ora posso permutarli individualmente con 1 mancata corrispondenza eseguendo il seguente codice per "AGG" e "GGC"
def permute_one_nucleotide(motif, alphabet={"A", "C", "G", "T"}):
import itertools
return list(
set(
itertools.chain.from_iterable(
[
[
motif[:pos] + nucleotide + motif[pos + 1 :]
for nucleotide in alphabet
]
for pos in range(len(motif))
]
)
)
)
"AGG" darà:
['TGG', 'ATG', 'AGG', 'GGG', 'AGT', 'CGG', 'AGC', 'AGA', 'ACG', 'AAG']
E "GCC" darà:
['GCC', 'GAC', 'GGT', 'GGA', 'AGC', 'GTC', 'TGC', 'CGC', 'GGG', 'GGC']
Quindi posso usare Counter
per trovare k-mer più frequenti. Ma funziona solo se d = 1
. Come generalizzare questo per qualcuno d <= k
?
Risposte
Questo è un metodo costoso dal punto di vista computazionale. Ma sì, dovrebbe recuperare desiderato. Quello che ho fatto qui è calcolare tutto il disadattamento con hamming dist 1. quindi calcolare il nuovo disadattamento con ham dist 1 dal disadattamento precedente e ricorrere fino a d.
import itertools
all_c=set('AGCT')
other = lambda x : list(all_c.difference(x))
def get_changed(sub, i):
return [sub[0:i]+c+sub[i+1:] for c in other(sub[i])]
def get_mismatch(d, setOfMmatch):
if d==0:
return setOfMmatch
newMmatches=[]
for sub in setOfMmatch:
newMmatches.extend(list(map(lambda x : ''.join(x), itertools.chain.from_iterable(([get_changed(sub, i) for i, c in enumerate(sub)])))))
setOfMmatch=setOfMmatch.union(newMmatches)
return get_mismatch(d-1, setOfMmatch)
dna='AGGC'
hamm_dist=1
length=3
list(itertools.chain.from_iterable([get_mismatch(hamm_dist, {dna[i:i+length]}) for i in range(len(dna)-length+1)]))
# without duplicates
# set(itertools.chain.from_iterable([get_mismatch(hamm_dist, {dna[i:i+length]}) for i in range(len(dna)-length+1)]))
ha trovato un codice con prestazioni migliori quasi 10-20 volte più veloce
%%time
import itertools, random
from cacheout import Cache
import time
all_c=set('AGCT')
get_other = lambda x : list(all_c.difference(x))
other={}
for c in all_c:
other[c]=get_other(c)
def get_changed(sub, i):
return [sub[0:i]+c+sub[i+1:] for c in other[sub[i]]]
mmatchHash=Cache(maxsize=256*256, ttl=0, timer=time.time, default=None)
def get_mismatch(d, setOfMmatch):
if d==0:
return setOfMmatch
newMmatches=[]
for sub in setOfMmatch:
newMmatches.extend(list(map(lambda x : ''.join(x), itertools.chain.from_iterable(([get_changed(sub, i) for i, c in enumerate(sub)])))))
setOfMmatch=setOfMmatch.union(newMmatches)
if not mmatchHash.get((d-1, str(setOfMmatch)), 0):
mmatchHash.set((d-1, str(setOfMmatch)), get_mismatch(d-1, setOfMmatch))
return mmatchHash.get((d-1, str(setOfMmatch)))
length_of_DNA=1000
dna=''.join(random.choices('AGCT', k=length_of_DNA))
hamm_dist=4
length=9
len(list(itertools.chain.from_iterable([get_mismatch(hamm_dist, {dna[i:i+length]}) for i in range(len(dna)-length+1)])))
# set(itertools.chain.from_iterable([get_mismatch(hamm_dist, {dna[i:i+length]}) for i in range(len(dna)-length+1)]))
Tempi CPU: utente 1 min 32 s, sys: 1,81 s, totale: 1 min 34 s Tempo wall: 1 min 34 s