Re: [Python] Iterare una sequenza
ciao giorgio, temo che a breve dovro' studiarmelo grazie per la segnalazione On Sun, Nov 26, 2017 at 11:34 AM, Giorgio Zoppiwrote: > Io userei el automa di aho corasick. > In Bioinformatics si usa tanto. > http://carshen.github.io/data-structures/algorithms/2014/04/07/aho-corasick-implementation-in-python.html > > > 2017-11-26 10:20 GMT+01:00 Giuseppe Costanzi : >> >> salve a tutti, >> >> ho una sequenza del tipo >> >> ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT >> >> scorrendola devo trovare una sequenza target GAATTC >> >> ACTGATCGATTACGTATAGTA "GAATTC" TATCATACATATATATCGATGCGTTCAT >> >> >> quindi dividere la sequenza da G, la prima lettera della sequenza target, >> e calcolarmi la lunghezza dei due frammenti risultanti >> >> ACTGATCGATTACGTATAGTAG >> >> e di questa >> >> GAATTCTATCATACATATATATCGATGCGTTCAT >> >> io avrei fatto >> >> seq = "ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT" >> target = "GAATTCT" >> >> s = [] >> >> for i,base in enumerate(seq): >> >> s.append(base) >> >> if len(s)==8: >> >> s.pop(0) >> >> if ''.join(s) == target: >> print i-5 >> print len(seq)-(i-5) >> break >> >> funziona ma non e' che ne sia proprio convinto, >> avete suggerimenti su come iterare la sequenza? >> >> saluti >> beppe >> >> >> p.s. >> >> e' un esercizio che ho trovato su p4b, python for biologist >> >> >> "Let's start this exercise by solving the problem manually. If we look >> through the >> DNA sequence we can spot the EcoRI site at position 21. Here's the >> sequence with >> the base positions labelled above and the EcoRI motif in bold: >> >> in bold sarebbe questa GAATTCT >> >> 0123456789012345678901234567890123456789012345678901234 >> ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT >> Since the EcoRI enzyme cuts the DNA between the G and first A, we can >> figure out >> that the first fragment will run from position 0 to position 21, and the >> second >> fragment from position 22 to the last position, 54. Therefore the >> lengths of the two >> fragments are 22 and 33." >> ___ >> Python mailing list >> Python@lists.python.it >> https://lists.python.it/mailman/listinfo/python > > > > ___ > Python mailing list > Python@lists.python.it > https://lists.python.it/mailman/listinfo/python > ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python
Re: [Python] Iterare una sequenza
ciao Christian, che dire... spettacolare; On Sun, Nov 26, 2017 at 11:25 AM, Christian Barrawrote: > In pseudo code > > seq = "ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT" > ...: > ...: def finder(seq, target): > ...: len_target = len(target) > ...: i = 0 > ...: while i+len_target < len(seq): > ...: yield seq[i:i+len_target], i > ...: i += 1 > Il check per == lo puoi inserire all’interno del finder o fuori. > > —— > Christian Barra > Python Freelancer // Consultant // Trainer > Board member of the EuroPython Society > www.chrisbarra.xyz > > On 26 Nov 2017, at 10:42, Giuseppe Costanzi > wrote: > > ciao carlo, > > si, interessante ma vorrei iterare la sequenza mano a mano, > > stavo pensando a qualcosa tipo > > next(iterator, default) > > grazie comunque > > > > On Sun, Nov 26, 2017 at 10:30 AM, Carlo Miron wrote: > > On Sun, Nov 26, 2017 at 10:20 AM, Giuseppe Costanzi > wrote: > > ho una sequenza del tipo > ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT > scorrendola devo trovare una sequenza target GAATTC > ACTGATCGATTACGTATAGTA "GAATTC" TATCATACATATATATCGATGCGTTCAT > quindi dividere la sequenza da G, la prima lettera della sequenza target, > e calcolarmi la lunghezza dei due frammenti risultanti > ACTGATCGATTACGTATAGTAG > e di questa > GAATTCTATCATACATATATATCGATGCGTTCAT > > > qualcosa tipo > > seq = "ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT" > target = "GAATTCT" > first, second = seq.split(target, 1) > first += target[1] > second = target[1:] + second > len(first), len(second) > > (22, 33) > > ? > ㎝ > > -- > |:**THE -WARE LICENSE** *(Revision ㊷)*: > | > wrote this mail. As long as you > retain this > | notice you can do whatever you want with this stuff. > | If we meet some day, and you think this stuff is worth it, > | you can buy me a in return. —㎝ > ___ > Python mailing list > Python@lists.python.it > https://lists.python.it/mailman/listinfo/python > > ___ > Python mailing list > Python@lists.python.it > https://lists.python.it/mailman/listinfo/python > > > > ___ > Python mailing list > Python@lists.python.it > https://lists.python.it/mailman/listinfo/python > > ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python
Re: [Python] Iterare una sequenza
Io userei el automa di aho corasick. In Bioinformatics si usa tanto. http://carshen.github.io/data-structures/algorithms/2014/04/07/aho-corasick-implementation-in-python.html 2017-11-26 10:20 GMT+01:00 Giuseppe Costanzi: > salve a tutti, > > ho una sequenza del tipo > > ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT > > scorrendola devo trovare una sequenza target GAATTC > > ACTGATCGATTACGTATAGTA "GAATTC" TATCATACATATATATCGATGCGTTCAT > > > quindi dividere la sequenza da G, la prima lettera della sequenza target, > e calcolarmi la lunghezza dei due frammenti risultanti > > ACTGATCGATTACGTATAGTAG > > e di questa > > GAATTCTATCATACATATATATCGATGCGTTCAT > > io avrei fatto > > seq = "ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT" > target = "GAATTCT" > > s = [] > > for i,base in enumerate(seq): > > s.append(base) > > if len(s)==8: > > s.pop(0) > > if ''.join(s) == target: > print i-5 > print len(seq)-(i-5) > break > > funziona ma non e' che ne sia proprio convinto, > avete suggerimenti su come iterare la sequenza? > > saluti > beppe > > > p.s. > > e' un esercizio che ho trovato su p4b, python for biologist > > > "Let's start this exercise by solving the problem manually. If we look > through the > DNA sequence we can spot the EcoRI site at position 21. Here's the > sequence with > the base positions labelled above and the EcoRI motif in bold: > > in bold sarebbe questa GAATTCT > > 0123456789012345678901234567890123456789012345678901234 > ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT > Since the EcoRI enzyme cuts the DNA between the G and first A, we can > figure out > that the first fragment will run from position 0 to position 21, and the > second > fragment from position 22 to the last position, 54. Therefore the > lengths of the two > fragments are 22 and 33." > ___ > Python mailing list > Python@lists.python.it > https://lists.python.it/mailman/listinfo/python > ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python
Re: [Python] Iterare una sequenza
In pseudo code seq = "ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT" ...: ...: def finder(seq, target): ...: len_target = len(target) ...: i = 0 ...: while i+len_target < len(seq): ...: yield seq[i:i+len_target], i ...: i += 1 Il check per == lo puoi inserire all’interno del finder o fuori. —— Christian Barra Python Freelancer // Consultant // Trainer Board member of the EuroPython Society www.chrisbarra.xyz > On 26 Nov 2017, at 10:42, Giuseppe Costanzi> wrote: > > ciao carlo, > > si, interessante ma vorrei iterare la sequenza mano a mano, > > stavo pensando a qualcosa tipo > > next(iterator, default) > > grazie comunque > > > > On Sun, Nov 26, 2017 at 10:30 AM, Carlo Miron wrote: >> On Sun, Nov 26, 2017 at 10:20 AM, Giuseppe Costanzi >> wrote: >> >>> ho una sequenza del tipo >>> ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT >>> scorrendola devo trovare una sequenza target GAATTC >>> ACTGATCGATTACGTATAGTA "GAATTC" TATCATACATATATATCGATGCGTTCAT >>> quindi dividere la sequenza da G, la prima lettera della sequenza target, >>> e calcolarmi la lunghezza dei due frammenti risultanti >>> ACTGATCGATTACGTATAGTAG >>> e di questa >>> GAATTCTATCATACATATATATCGATGCGTTCAT >> >> qualcosa tipo >> > seq = "ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT" > target = "GAATTCT" > first, second = seq.split(target, 1) > first += target[1] > second = target[1:] + second > len(first), len(second) >> (22, 33) >> >> ? >> ㎝ >> >> -- >> |:**THE -WARE LICENSE** *(Revision ㊷)*: >> | wrote this mail. As long as you retain this >> | notice you can do whatever you want with this stuff. >> | If we meet some day, and you think this stuff is worth it, >> | you can buy me a in return. —㎝ >> ___ >> Python mailing list >> Python@lists.python.it >> https://lists.python.it/mailman/listinfo/python > ___ > Python mailing list > Python@lists.python.it > https://lists.python.it/mailman/listinfo/python ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python
Re: [Python] Iterare una sequenza
ciao federico, il secondo metodo mi piace, tra l' altro ho notato che itera solo 22 volte mentre il mio 28. in pratica vai a blocchi di lunghezza uguale al target invece di andare una base alla volta. bello, mi piace questa soluzione. grazie 2017-11-26 11:06 GMT+01:00 Federico Cerchiari: > Ciao Giuseppe, > > per trovare la posizione di un oggetto in una sequenza (lista, stringa o > altro) puoi usare il metodo '.index' delle liste: > > dna_seq = 'ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT' > target = 'GAATTCT' > target_position = dna_seq.index(target) + 1 > print('lunghezza pre-target: %d, post-target %d' % (target_position, > len(dna_seq)-target_position)) > > Se vuoi iterare sulla sequenza per farci altro fai bene a usare > 'enumerate', ma invece di costruirti la stringa di comparazione di volta in > volta puoi prelevarla dalla stringa originale ad ogni ciclo: > > for position, gene in enumerate(dna_seq): > if dna_seq[position:position+len(target)] == target: > position += 1 # Aggiungiamo 1 perchè enumerate è 0-based > print('lunghezza pre-target: %d, post-target %d' % (position, > len(dna_seq)-position)) > > Federico > > 2017-11-26 10:42 GMT+01:00 Giuseppe Costanzi : > >> ciao carlo, >> >> si, interessante ma vorrei iterare la sequenza mano a mano, >> >> stavo pensando a qualcosa tipo >> >> next(iterator, default) >> >> grazie comunque >> >> >> >> On Sun, Nov 26, 2017 at 10:30 AM, Carlo Miron wrote: >> > On Sun, Nov 26, 2017 at 10:20 AM, Giuseppe Costanzi >> > wrote: >> > >> >> ho una sequenza del tipo >> >> ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT >> >> scorrendola devo trovare una sequenza target GAATTC >> >> ACTGATCGATTACGTATAGTA "GAATTC" TATCATACATATATATCGATGCGTTCAT >> >> quindi dividere la sequenza da G, la prima lettera della sequenza >> target, >> >> e calcolarmi la lunghezza dei due frammenti risultanti >> >> ACTGATCGATTACGTATAGTAG >> >> e di questa >> >> GAATTCTATCATACATATATATCGATGCGTTCAT >> > >> > qualcosa tipo >> > >> seq = "ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT" >> target = "GAATTCT" >> first, second = seq.split(target, 1) >> first += target[1] >> second = target[1:] + second >> len(first), len(second) >> > (22, 33) >> > >> > ? >> > ㎝ >> > >> > -- >> > |:**THE -WARE LICENSE** *(Revision ㊷)*: >> > | wrote this mail. As long as you retain this >> > | notice you can do whatever you want with this stuff. >> > | If we meet some day, and you think this stuff is worth it, >> > | you can buy me a in return. —㎝ >> > ___ >> > Python mailing list >> > Python@lists.python.it >> > https://lists.python.it/mailman/listinfo/python >> ___ >> Python mailing list >> Python@lists.python.it >> https://lists.python.it/mailman/listinfo/python >> > > > ___ > Python mailing list > Python@lists.python.it > https://lists.python.it/mailman/listinfo/python > > ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python
Re: [Python] Iterare una sequenza
Ciao Giuseppe, per trovare la posizione di un oggetto in una sequenza (lista, stringa o altro) puoi usare il metodo '.index' delle liste: dna_seq = 'ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT' target = 'GAATTCT' target_position = dna_seq.index(target) + 1 print('lunghezza pre-target: %d, post-target %d' % (target_position, len(dna_seq)-target_position)) Se vuoi iterare sulla sequenza per farci altro fai bene a usare 'enumerate', ma invece di costruirti la stringa di comparazione di volta in volta puoi prelevarla dalla stringa originale ad ogni ciclo: for position, gene in enumerate(dna_seq): if dna_seq[position:position+len(target)] == target: position += 1 # Aggiungiamo 1 perchè enumerate è 0-based print('lunghezza pre-target: %d, post-target %d' % (position, len(dna_seq)-position)) Federico 2017-11-26 10:42 GMT+01:00 Giuseppe Costanzi: > ciao carlo, > > si, interessante ma vorrei iterare la sequenza mano a mano, > > stavo pensando a qualcosa tipo > > next(iterator, default) > > grazie comunque > > > > On Sun, Nov 26, 2017 at 10:30 AM, Carlo Miron wrote: > > On Sun, Nov 26, 2017 at 10:20 AM, Giuseppe Costanzi > > wrote: > > > >> ho una sequenza del tipo > >> ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT > >> scorrendola devo trovare una sequenza target GAATTC > >> ACTGATCGATTACGTATAGTA "GAATTC" TATCATACATATATATCGATGCGTTCAT > >> quindi dividere la sequenza da G, la prima lettera della sequenza > target, > >> e calcolarmi la lunghezza dei due frammenti risultanti > >> ACTGATCGATTACGTATAGTAG > >> e di questa > >> GAATTCTATCATACATATATATCGATGCGTTCAT > > > > qualcosa tipo > > > seq = "ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT" > target = "GAATTCT" > first, second = seq.split(target, 1) > first += target[1] > second = target[1:] + second > len(first), len(second) > > (22, 33) > > > > ? > > ㎝ > > > > -- > > |:**THE -WARE LICENSE** *(Revision ㊷)*: > > | wrote this mail. As long as you retain this > > | notice you can do whatever you want with this stuff. > > | If we meet some day, and you think this stuff is worth it, > > | you can buy me a in return. —㎝ > > ___ > > Python mailing list > > Python@lists.python.it > > https://lists.python.it/mailman/listinfo/python > ___ > Python mailing list > Python@lists.python.it > https://lists.python.it/mailman/listinfo/python > ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python
Re: [Python] Iterare una sequenza
ciao carlo, si, interessante ma vorrei iterare la sequenza mano a mano, stavo pensando a qualcosa tipo next(iterator, default) grazie comunque On Sun, Nov 26, 2017 at 10:30 AM, Carlo Mironwrote: > On Sun, Nov 26, 2017 at 10:20 AM, Giuseppe Costanzi > wrote: > >> ho una sequenza del tipo >> ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT >> scorrendola devo trovare una sequenza target GAATTC >> ACTGATCGATTACGTATAGTA "GAATTC" TATCATACATATATATCGATGCGTTCAT >> quindi dividere la sequenza da G, la prima lettera della sequenza target, >> e calcolarmi la lunghezza dei due frammenti risultanti >> ACTGATCGATTACGTATAGTAG >> e di questa >> GAATTCTATCATACATATATATCGATGCGTTCAT > > qualcosa tipo > seq = "ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT" target = "GAATTCT" first, second = seq.split(target, 1) first += target[1] second = target[1:] + second len(first), len(second) > (22, 33) > > ? > ㎝ > > -- > |:**THE -WARE LICENSE** *(Revision ㊷)*: > | wrote this mail. As long as you retain this > | notice you can do whatever you want with this stuff. > | If we meet some day, and you think this stuff is worth it, > | you can buy me a in return. —㎝ > ___ > Python mailing list > Python@lists.python.it > https://lists.python.it/mailman/listinfo/python ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python
Re: [Python] Iterare una sequenza
On Sun, Nov 26, 2017 at 10:20 AM, Giuseppe Costanziwrote: > ho una sequenza del tipo > ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT > scorrendola devo trovare una sequenza target GAATTC > ACTGATCGATTACGTATAGTA "GAATTC" TATCATACATATATATCGATGCGTTCAT > quindi dividere la sequenza da G, la prima lettera della sequenza target, > e calcolarmi la lunghezza dei due frammenti risultanti > ACTGATCGATTACGTATAGTAG > e di questa > GAATTCTATCATACATATATATCGATGCGTTCAT qualcosa tipo >>> seq = "ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCAT" >>> target = "GAATTCT" >>> first, second = seq.split(target, 1) >>> first += target[1] >>> second = target[1:] + second >>> len(first), len(second) (22, 33) ? ㎝ -- |:**THE -WARE LICENSE** *(Revision ㊷)*: | wrote this mail. As long as you retain this | notice you can do whatever you want with this stuff. | If we meet some day, and you think this stuff is worth it, | you can buy me a in return. —㎝ ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python