Exemple #1
0
def test_order():
    o1 = order([132, 99, 22], ordering=[99, 22, 44, 132])
    o2 = order([{'thing': 'bathrobe'}, {'thing': 'toga'}],
               ['toga', 'bathrobe'], key='thing')
    o3 = order([-2, 2, 4, 1, 4],
               [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16], key=lambda x: x * x)

    asserts.eq_(o1, [99, 22, 132])
    asserts.eq_(o2, [{'thing': 'toga'}, {'thing': 'bathrobe'}])
    asserts.eq_(o3, [1, -2, 2, 4, 4])
def genotypes_to_records(genotypes, reader, extant_columns):
    """Return a list of vcf.model._Record, converted from a list of genotype
    relations, taken from the database.

    This is used to write to a .vcf file with vcf.parser.Writer.

    Args:
        genotypes: a list of genotype relations from the database.
        reader: a template vcf.Reader.
        extant_columns: a list of columns which have values for these genotypes.
    """
    info_fields, format_fields = _fields_from_columns(extant_columns)
    CallData = vcf.model.make_calldata_tuple(format_fields)
    grouped_genotypes = itertools.groupby(genotypes, _call_key)
    sample_ordering = reader.samples
    records = []
    for _, gts in grouped_genotypes:
        samples = []
        gts = list(gts)
        gts = order(gts, sample_ordering, 'sample_name')
        for gt in gts:
            data = CallData(*[vcf_format(gt['sample:' + d]) for d in format_fields])
            call = vcf.model._Call(None, gt['sample_name'], data)
            samples.append(call)
        record = _make_record_from_gt(gts[0], info_fields, format_fields, samples)
        records.append(record)
    return records