def test_partition():
    assert functional.partition(lambda e: e % 3 == 0,
                                functional.range(1, 11)) == \
           [ [ 3, 6, 9 ], [ 1, 2, 4, 5, 7, 8, 10 ] ]
    assert functional.partition(lambda e: e % 3 != 0,
                                functional.range(1, 11)) == \
           [ [ 1, 2, 4, 5, 7, 8, 10 ], [ 3, 6, 9 ] ]
    assert functional.partition(lambda e: e == 0,
                                functional.range(1, 11)) == \
           [ [], [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] ]
    assert functional.partition(lambda e: len(e) > 1,
                                [ [ 1 ], [ 1, 2 ], [ 1, 2, 3 ] ]) == \
           [ [ [ 1, 2 ], [ 1, 2, 3 ] ], [ [ 1 ] ] ]
from sequence import rev_complement
from fastamasta import FastaReader
from functional import partition

if __name__ == "__main__":
    seq = [i for i in FastaReader("data/44.fas")][0][1]

    rev_palindromes = []
    for i in range(4, 13):
        parts = [''.join(x) for x in partition(seq, i, list)]
        for j in range(len(parts)):
            if parts[j] == rev_complement(parts[j]):
                rev_palindromes.append((j + 1, i))

    for tup in rev_palindromes:
        print tup[0], tup[1]
from fastamasta import FastaReader
from functional import partition

if __name__ == "__main__":
    data = []

    for datum in FastaReader("data/21.fas"):
        data.append(datum[1])

    shortest = ' ' * 1001
    for i in data:
        if len(i) < len(shortest):
            shortest = i

    data.remove(shortest)
    print data

    for i in range(2, len(shortest) + 1)[::-1]:
        parts = [i for i in partition(shortest, i, str)]
        for part in parts:
            in_all = True
            for datum in data:
                if part not in datum:
                    in_all = False
                    break
            if in_all:
                print part
Beispiel #4
0
from functional import partition, frequencies
from fastamasta import FastaReader

def alphabet(order, length, string='', arr=None):
    if arr is None:
        arr=[]
    if length == 0:
        arr.append(string)
    else:
        for char in order:
            alphabet(order, length-1, string + char, arr)
    return arr

if __name__ == "__main__":
    with open("data/13.dat", 'r') as data_file:
        reader = FastaReader('data/14.dat')
        data = reader.readnext()

        order, size = ['A', 'C', 'G', 'T'], 4
        alph = alphabet(order, size)

        fourmer_count = frequencies(partition(data[1], size, str))

        output = ''
        for word in alph:
            output += str(fourmer_count.get(word, 0)) + ' '

        print output
from functional import partition, frequencies
import operator

if __name__ == '__main__':
    with open('data/4.ini', 'r') as data_file:
        data = data_file.readline()
        k, L, t = data_file.readline().split(' ')
        k, L, t = int(k), int(L), int(t)

        kmers = [
            i for i in partition(list(data), k,
                                 lambda x: reduce(operator.add, x))
        ]
        kmers_count = frequencies(kmers)
        viable_kmers = set(key for key, val in kmers_count.items()
                           if (val >= t))

        viable_kmer_idxs = {}
        for i in range(len(kmers)):
            if kmers[i] in viable_kmers:
                temp_arr = viable_kmer_idxs.get(kmers[i], [])
                temp_arr.append(i)
                viable_kmer_idxs[kmers[i]] = temp_arr

        L_t_clumps = set()
        for (key, val) in viable_kmer_idxs.items():
            for i in range(len(val) - (t - 1)):
                if ((val[i + t - 1] - val[i]) <= (L - t)):
                    L_t_clumps.add(key)

        print ' '.join(map(str, L_t_clumps))