zoukankan      html  css  js  c++  java
  • 从字符串中或文件中获取序列信息

    #从长度不是特别长的fasta_string中获取序列,去除序列中的空格并全部转为大写,序列以(序列名,序列)的形式,保存到列表中
    def get_sequences_from_fasta_string(fasta_string):
    assert isinstance(fasta_string,str)
    sequences=[]
    for item in fasta_string.split('>')[1:]:
    seq_name=item.split(' ')[0]
    seq=''.join(''.join(item.split(' ')[1:]).split()).upper()
    sequences.append((seq_name,seq))
    return sequences

    # 将一个文件夹内的序列文件提取生成到一个fasta文件里
    import glob
    extend_names=['txt','seq','fasta']
    s=''
    for extend_name in extend_names:
    for filename in glob.glob(r'c:usersselndesktopsequences*.{}'.format(extend_name)):
    with open(filename,'r')as f:
    seq=''.join(list(filter(lambda char:char.isalpha(),
    f.read())))
    s+=">%s %s " % (filename.split('\')[-1],seq)


    #利用生成器,从序列数非常巨大的文件里读取序列
    def get_items_from_file(filename,testfun=None):
    with open(filename) as file:
    return get_items(file,testfun)

    def get_items(src,testfun=None):
    return [item for item in item_generator(src) if not testfun or testfun(item)]

    def item_generator(src):
    skip_intro(src)
    des=[]
    seq=''
    for line in src:
    if line[0]=='>':
    if des:
    yield des,seq
    des=line[1:-1].split('|')
    seq=''
    else:
    seq+=line[:-1]
    yield des,seq

    def skip_intro(src):
    line=src.readline()
    while line[0]!='A':
    line=src.readline()

    def find_item_in_file(filename,testfun=None):
    with open(filename)as file:
    return find_item(file,testfun)

    def find_item(src,testfun=None):
    gen=item_generator(src) #生成器函数一定要赋给一个变量后使用!!!
    item=next(gen)
    if not testfun:
    return item
    else:
    try:
    while not testfun(item):
    item=next(gen)
    return item
  • 相关阅读:
    转: android emulator 命令详解
    ubuntu 环境变量PATH的修改
    ubuntu创建、删除文件及文件夹,强制清空回收站方法
    51单片机的P0口工作原理详细讲解
    smartconfig配置模式
    (网卡)混杂模式
    Scrapy shell使用
    Splash 笔记
    Lua一般都用来干什么,有什么优点
    python dict转json并保存文件
  • 原文地址:https://www.cnblogs.com/liaoxuewen/p/10384769.html
Copyright © 2011-2022 走看看