当前位置: 首页>>代码示例>>Python>>正文


Python DictReader.get方法代码示例

本文整理汇总了Python中csv.DictReader.get方法的典型用法代码示例。如果您正苦于以下问题:Python DictReader.get方法的具体用法?Python DictReader.get怎么用?Python DictReader.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在csv.DictReader的用法示例。


在下文中一共展示了DictReader.get方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: action

# 需要导入模块: from csv import DictReader [as 别名]
# 或者: from csv.DictReader import get [as 别名]
def action(args):
    ### format format blast data and add additional available information
    fieldnames = None if args.has_header else sequtils.BLAST_HEADER_DEFAULT
    blast_results = DictReader(args.blast_file, fieldnames = fieldnames)
    blast_results = list(blast_results)

    sseqids = set(s['sseqid'] for s in blast_results)
    qseqids = set(s['qseqid'] for s in blast_results)

    # load seq_info and map file
    mapfile = DictReader(args.map, fieldnames = ['name', 'specimen'])
    mapfile = {m['name']:m['specimen'] for m in mapfile if m['name'] in qseqids}

    seq_info = DictReader(args.seq_info)
    seq_info = {s['seqname']:s for s in seq_info if s['seqname'] in sseqids}

    # pident
    def pident(b):
        return dict(b, pident = float(b['pident'])) if b['sseqid'] else b

    blast_results = (pident(b) for b in blast_results)

    # coverage
    def cov(b):
        if b['sseqid'] and b['qcovs']:
            b['coverage'] = float(b['qcovs'])
            return b
        elif b['sseqid']:
            c = coverage(b['qstart'], b['qend'], b['qlen'])
            return dict(b, coverage = c)
        else:
            return b

    blast_results = (cov(b) for b in blast_results)

    # seq info
    def info(b):
        return dict(seq_info[b['sseqid']], **b) if b['sseqid'] else b

    blast_results = (info(b) for b in blast_results)

    # tax info
    def tax_info(b):
        return dict(args.taxonomy[b['tax_id']], **b) if b['sseqid'] else b

    blast_results = (tax_info(b) for b in blast_results)

    ### output file headers
    fieldnames = ['specimen', 'max_percent', 'min_percent', 'max_coverage',
                  'min_coverage', 'assignment_id', 'assignment']

    if args.weights:
        weights = DictReader(args.weights, fieldnames = ['name', 'weight'])
        weights = {d['name']:d['weight'] for d in weights if d['name'] in qseqids}
        fieldnames += ['clusters', 'reads', 'pct_reads']
    else:
        weights = {}

    if args.copy_numbers:
        copy_numbers = DictReader(args.copy_numbers)
        copy_numbers = {d['tax_id']:float(d['median']) for d in copy_numbers}
        fieldnames += ['corrected', 'pct_corrected']
    else:
        copy_numbers = {}

    # TODO: take out target_rank, hi, low and provide in pipeline using csvmod
    # TODO: option to include tax_ids (default no)
    fieldnames += ['target_rank', 'hi', 'low', 'tax_ids']

    ### Columns
    out = DictWriter(args.out,
            extrasaction = 'ignore',
            fieldnames = fieldnames)
    out.writeheader()

    if args.out_detail:
        args.out_detail.writeheader()

    def blast_hit(hit, args):
        return hit['sseqid'] and \
               hit[args.target_rank] and \
               hit['coverage'] >= args.coverage and \
               float(weights.get(hit['qseqid'], 1)) >= args.min_cluster_size and \
               hit[args.target_rank] not in args.exclude_by_taxid and \
               hit['qseqid'] != hit['sseqid'] and \
               int(hit['ambig_count']) <= args.max_ambiguous

    ### Rows
    etc = '[no blast result]' # This row will hold all unmatched

    # groups have list position prioritization
    groups = [
        ('> {}%'.format(args.max_identity),
            lambda h: blast_hit(h, args) and h['pident'] > args.max_identity),
        (None,
            lambda h: blast_hit(h, args) and args.max_identity >= h['pident'] > args.min_identity),
        ('<= {}%'.format(args.min_identity),
            lambda h: blast_hit(h, args) and h['pident'] <= args.min_identity),
    ]

#.........这里部分代码省略.........
开发者ID:crosenth,项目名称:bioy,代码行数:103,代码来源:classify.py


注:本文中的csv.DictReader.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。