python - 基因组中寻找特定要求序列,求一个计算速度快的方法
巴扎黑
巴扎黑 2017-04-18 09:36:14
[Python讨论组]
巴扎黑
巴扎黑

全部回复(5)
大家讲道理

1

阿神

这个问题蛮有意思, 贴上我的代码(用到了生成器以及多线程)

from multiprocessing import cpu_count
from multiprocessing.dummy import Pool


def generate_index(n, step=5):
    for i in range(n):
        if i + step < n:
            yield (i, i + step)
        else:
            yield (i, None)
            break


def pick_up(st):
    for item in (st[i: j] for i, j in generate_index(len(st), 9) if j is not None):
        if st.count(item) == 3:
            result.add(item)


with open('gene.txt', 'r') as fh:
    src_lst = fh.readline().strip()
    sep_lst = [src_lst[i: j] for i, j in generate_index(len(src_lst), 500)]

result = set()
pool = Pool(cpu_count())
pool.map(pick_up, sep_lst)
pool.close()
pool.join()
print(result)

随便捏了点数据, 居然真找到这么几个:

{'TCGATCGAT', 'CGATCGATC', 'ATCGATCGA', 'GATCGATCG'}
伊谢尔伦

目测可以使用collections 中的 counter

阿神

每九个序列一读和之前的两组组九个序列做比较,如果不同就丢弃掉之前的那一组。

ans = []
s = list(s)
first, s = s[:9], s[9:]
second, s = s[:9], s[9:]
third, s = s[:9], s[9:]
while len(s) > 0:
    if first == second and second == third:
        ans.append(first)
    else:
        first, second, third, s = second, third, s[:9], s[9:]
大家讲道理

julia有个基因处理方面的库
也可以试试。

https://github.com/OpenGene/O...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号