def bed_writer(parser, keep, extract, args):
    """Writes BED/BIM/FAM files."""
    # The output bed and bim file
    bim_fn = args.output + ".bim"
    with open(bim_fn, "w") as bim, PyPlink(args.output, "w") as bed:
        # Getting the samples
        samples = np.array(parser.get_samples(), dtype=str)
        k = _get_sample_select(samples=samples, keep=keep)

        # Writing the FAM file
        with open(args.output + ".fam", "w") as fam:
            for sample in samples[k]:
                print(sample, sample, "0", "0", "0", "-1", sep=" ", file=fam)

        # Getting the data generator
        generator = _get_generator(parser=parser, extract=extract, keep=k,
                                   check_maf=args.maf)

        # The number of markers extracted
        nb_extracted = 0

        for data in generator:
            # Keeping only the required genotypes, changing NaN to -1 and
            # rounding to get a hard call
            genotypes = data.genotypes
            genotypes[np.isnan(genotypes)] = -1
            genotypes = np.round(genotypes, 0)

            # Writing the genotypes and the BIM file
            bed.write_genotypes(genotypes)
            print(
                _PLINK_CHROM_ENCODE.get(str(data.variant.chrom),
                                        data.variant.chrom),
                data.variant.name, "0", data.variant.pos, data.coded,
                data.reference, sep="\t", file=bim,
            )
            nb_extracted += 1

        if nb_extracted == 0:
            logger.warning("No markers matched the extract list")
def bed_writer ( parser , keep , extract , args ) : bim_fn = args . output + ".bim" with open ( bim_fn , "w" ) as bim , PyPlink ( args . output , "w" ) as bed : samples = np . array ( parser . get_samples ( ) , dtype = str ) k = _get_sample_select ( samples = samples , keep = keep ) with open ( args . output + ".fam" , "w" ) as fam : for sample in samples [ k ] : print ( sample , sample , "0" , "0" , "0" , "-1" , sep = " " , file = fam ) generator = _get_generator ( parser = parser , extract = extract , keep = k , check_maf = args . maf ) nb_extracted = 0 for data in generator : genotypes = data . genotypes genotypes [ np . isnan ( genotypes ) ] = - 1 genotypes = np . round ( genotypes , 0 ) bed . write_genotypes ( genotypes ) print ( _PLINK_CHROM_ENCODE . get ( str ( data . variant . chrom ) , data . variant . chrom ) , data . variant . name , "0" , data . variant . pos , data . coded , data . reference , sep = "\t" , file = bim , ) nb_extracted += 1 if nb_extracted == 0 : logger . warning ( "No markers matched the extract list" )
Writes BED/BIM/FAM files.