def build_totals_dict(args, handle): references = tuple(collect_references(args, handle)) structure = build_key_struct(args, handle) totals = {} for (sm_key, libraries) in structure.iteritems(): for lb_key in libraries: if len(references) == 1: key = references[0] counts = collections.defaultdict(int) totals[(sm_key, lb_key, key)] = counts totals[(sm_key, lb_key, '*')] = counts else: build_new_dicts(totals, sm_key, lb_key, references) if len(libraries) == 1: key = list(libraries)[0] reuse_dicts(totals, sm_key, '*', sm_key, key, references) else: build_new_dicts(totals, sm_key, '*', references) if len(structure) == 1: key = list(structure)[0] reuse_dicts(totals, '*', '*', key, '*', references) else: build_new_dicts(totals, '*', '*', references) return totals
def print_table(handle, args, totals): lengths = collect_references(args, handle) if args.outfile == "-": output_handle = sys.stdout else: output_handle = open(args.outfile, "w") with output_handle: rows = build_table(args.target_name, totals, lengths) output_handle.write(_HEADER % datetime.datetime.now().isoformat()) output_handle.write("\n") for line in padded_table(rows): output_handle.write(line) output_handle.write("\n")
def build_table(args, handle, counts): table = {} for (key, readgroup) in collect_readgroups(args, handle).iteritems(): sample = readgroup["SM"] library = readgroup["LB"] for (reference, size) in collect_references(args, handle).iteritems(): subtable_key = (args.target_name, sample, library, reference) subtable = create_or_get_subtable(table, subtable_key, size) statistics = counts.get(reference, {}).get(key, {}) for (stat, value) in statistics.iteritems(): subtable[stat] += value return table