def export_tsv(variants, target, samples_selection, fields): """Write the selected fields of each variants on a line, with a header. :param target: a writable object - an HTTPResponse in our case. :param fields: list of field names (from Variant data model). """ for f in EXCEPT_FIELDS: if f in fields: fields.remove(f) sample_names = [s.name for s in samples_selection if s.active] writer = csv.writer(target, delimiter='\t') writer.writerow([COL_NAMES.get(f, capitalize(f)) for f in fields]) gts_map = {(0,0):'0/0', (1,0):'1/0', (0,1):'0/1', (1,1):'1/1', (None,None):'./.'} db = variants.db exp = [expose_variant_full(v, samples_selection) for v in variants] exp = annotate_variants(exp, db) for v in exp: info = [] for field in fields: val = v[field] if field == 'genotypes_index': assert len(sample_names) == len(val), "{} samples but {} genotypes".format(len(sample_names), len(val)) couples = ['{}:{}'.format(s,gts_map[tuple(g)]) for s,g in zip(sample_names, val)] info.append(','.join(c for c in couples)) elif isinstance(val, (list, tuple)): info.append(','.join(val)) else: info.append(val) writer.writerow(info)
def export_tsv(variants, target, samples_selection, fields): """Write the selected fields of each variants on a line, with a header. :param target: a writable object - an HTTPResponse in our case. :param fields: list of field names (from Variant data model). """ for f in EXCEPT_FIELDS: if f in fields: fields.remove(f) sample_names = [s.name for s in samples_selection if s.active] writer = csv.writer(target, delimiter='\t') writer.writerow([COL_NAMES.get(f, capitalize(f)) for f in fields]) gts_map = { (0, 0): '0/0', (1, 0): '1/0', (0, 1): '0/1', (1, 1): '1/1', (None, None): './.' } db = variants.db exp = [expose_variant_full(v, samples_selection) for v in variants] exp = annotate_variants(exp, db) for v in exp: info = [] for field in fields: if field not in v: continue else: val = v[field] if field == 'genotypes_index': assert len(sample_names) == len( val), "{} samples but {} genotypes".format( len(sample_names), len(val)) couples = [ '{}:{}'.format(s, gts_map[tuple(g)]) for s, g in zip(sample_names, val) ] info.append(','.join(c for c in couples)) elif isinstance(val, (list, tuple)): info.append(','.join(val)) else: info.append(val) writer.writerow(info)
def expose(self): """Return a dict exposing variants, filters, stats etc. to be sent to the view.""" t1 = time() filter_result = self.apply_all_filters() t2 = time() var = filter_result.variants stat = self.stats.make_stats(filter_result.ids) t3 = time() var = [expose_variant_full(v, self.ss) for v in var] t4 = time() var = annotate_variants(var, self.db) t5 = time() logging.info("Apply/Stats/Expose/Annotate: {:.3f}s {:.3f}s {:.3f}s {:.3f}s".format(t2-t1, t3-t2, t4-t3, t5-t4)) # TODO? How can one sort wrt. these fields? #if self.sort.key is not None and self.sort.key not in VARIANT_FIELDS: # self.sort.sort_dict(var, inplace=True) response = {'variants': var} response["filters"] = [str(x) for x in self.fc.list] response["nfound"] = stat.total_count response["stats"] = stat.expose() return response
def expose(self): """Return a dict exposing variants, filters, stats etc. to be sent to the view.""" t1 = time() filter_result = self.apply_all_filters() t2 = time() var = filter_result.variants stat = self.stats.make_stats(filter_result.ids) t3 = time() var = [expose_variant_full(v, self.ss) for v in var] t4 = time() var = annotate_variants(var, self.db) t5 = time() logging.info( "Apply/Stats/Expose/Annotate: {:.3f}s {:.3f}s {:.3f}s {:.3f}s". format(t2 - t1, t3 - t2, t4 - t3, t5 - t4)) # TODO? How can one sort wrt. these fields? #if self.sort.key is not None and self.sort.key not in VARIANT_FIELDS: # self.sort.sort_dict(var, inplace=True) response = {'variants': var} response["filters"] = [str(x) for x in self.fc.list] response["nfound"] = stat.total_count response["stats"] = stat.expose() return response