Пример #1
0
    def testPhenoHeader(self):
        """Phenotype with header and covariate.

        We are just making sure everything looks reasonable when no complications are involved"""

        # Indicate that we want to use sex as a covariate
        PhenoCovar.sex_as_covariate = True

        pc = PhenoCovar()
        load_pedigree(pc, self.ped)
        self.assertEqual(1, len(pc.phenotype_data))
        self.assertEqual(1, len(pc.covariate_data))
        self.assertEqual(8, len(pc.phenotype_data[0]))
        self.assertEqual(1, len(pc.phenotype_names))
        self.assertEqual("Pheno-1", pc.phenotype_names[0])

        for idx in range(0, len(self.pheno)):
            self.assertAlmostEqual(self.pheno[idx], pc.phenotype_data[0][idx])
            self.assertEqual(self.sex[idx], pc.covariate_data[0][idx])

        pc.load_phenofile(self.header)
        self.assertEqual("BMI", pc.phenotype_names[0])
        pheno = [
            0.9, 1.0, 0.4, 0.8, 1, 0.1, PhenoCovar.missing_encoding,
            PhenoCovar.missing_encoding
        ]
        for idx in range(0, len(pheno)):
            self.assertAlmostEqual(pheno[idx], pc.phenotype_data[0][idx])
Пример #2
0
    def testPhenoNoHeader(self):
        # Indicate that we want to use sex as a covariate
        PhenoCovar.sex_as_covariate = True

        pc = PhenoCovar()
        load_pedigree(pc, self.ped)
        pc.load_phenofile(open(self.filenames[3]), indices=[2,3])
        self.assertEqual(2, len(pc.phenotype_data))
        self.assertEqual(1, len(pc.covariate_data))
        self.assertEqual(6, len(pc.phenotype_data[0]))
        self.assertEqual(6, len(pc.phenotype_data[1]))
        self.assertEqual(6, len(pc.covariate_data[0]))
        self.assertEqual(["Pheno-2", "Pheno-3"], pc.phenotype_names)

        index = 0
        for p1, p2, p3 in self.phenotypes:
            self.assertEqual(p2, pc.phenotype_data[0][index])
            self.assertEqual(p3, pc.phenotype_data[1][index])
            index += 1
        pc.load_phenofile(open(self.filenames[3]), indices=[1,2,3])
        self.assertEqual(3, len(pc.phenotype_data))
        self.assertEqual(1, len(pc.covariate_data))
        self.assertEqual(6, len(pc.phenotype_data[0]))
        self.assertEqual(6, len(pc.phenotype_data[1]))
        self.assertEqual(6, len(pc.phenotype_data[2]))
        self.assertEqual(["Pheno-1", "Pheno-2", "Pheno-3"], pc.phenotype_names)

        index = 0
        for p1, p2, p3 in self.phenotypes:
            self.assertEqual(p1, pc.phenotype_data[0][index])
            self.assertEqual(p2, pc.phenotype_data[1][index])
            self.assertEqual(p3, pc.phenotype_data[2][index])
            index += 1
Пример #3
0
    def testMultiPhenoHeader(self):
        self.ped = get_lines(self.filenames[2])
        # Indicate that we want to use sex as a covariate
        PhenoCovar.sex_as_covariate = True

        pc = PhenoCovar()
        load_pedigree(pc, self.ped)
        with open(self.filenames[4]) as f:
            pc.load_phenofile(f, indices=[2, 3])
        self.assertEqual(2, len(pc.phenotype_data))
        self.assertEqual(1, len(pc.covariate_data))
        self.assertEqual(6, len(pc.phenotype_data[0]))
        self.assertEqual(6, len(pc.phenotype_data[1]))
        self.assertEqual(6, len(pc.covariate_data[0]))
        self.assertEqual(["IBM", "MSA"], pc.phenotype_names)
        self.assertEqual(["SEX"], pc.covariate_labels)

        sex = [1, 1, 2, 2, 1, 1]
        covariates = [[0.1, 1.0, 0.5], [0.2, 0.5, 1.0], [0.3, 0.6, 0.1],
                      [0.4, 0.5, 0.5], [0.5, 1.0, 1.0], [0.6, 0.1, 0.2]]

        index = 0
        for p1, p2, p3 in covariates:
            self.assertEqual(sex[index], pc.covariate_data[0][index])
            self.assertEqual(p2, pc.phenotype_data[0][index])
            self.assertEqual(p3, pc.phenotype_data[1][index])
            index += 1
Пример #4
0
    def test_sample_pheno_with_header(self):
        # Indicate that we want to use sex as a covariate
        PhenoCovar.sex_as_covariate = True

        pc = PhenoCovar()
        load_pedigree(pc, self.ped)
        pc.load_phenofile(open(self.filenames[8]), indices=[1,2], sample_file=True)
        self.assertEqual(2, len(pc.phenotype_data))
        self.assertEqual(1, len(pc.covariate_data))
        self.assertEqual(6, len(pc.phenotype_data[0]))
        self.assertEqual(6, len(pc.phenotype_data[1]))
        self.assertEqual(6, len(pc.covariate_data[0]))
        self.assertEqual(["BMI", "IBM"], pc.phenotype_names)



        index = 0
        for p1, p2, p3 in self.phenotypes:
            self.assertEqual(p1, pc.phenotype_data[0][index])
            self.assertEqual(p2, pc.phenotype_data[1][index])
            index += 1
        pc.load_phenofile(open(self.filenames[8]), indices=[1,2,3], sample_file=True)
        self.assertEqual(3, len(pc.phenotype_data))
        self.assertEqual(1, len(pc.covariate_data))
        self.assertEqual(6, len(pc.phenotype_data[0]))
        self.assertEqual(6, len(pc.phenotype_data[1]))
        self.assertEqual(6, len(pc.phenotype_data[2]))
        self.assertEqual(["BMI", "IBM", "MSA"], pc.phenotype_names)

        index = 0
        for p1, p2, p3 in self.phenotypes:
            self.assertEqual(p1, pc.phenotype_data[0][index])
            self.assertEqual(p2, pc.phenotype_data[1][index])
            self.assertEqual(p3, pc.phenotype_data[2][index])
            index += 1
Пример #5
0
    def testForInvariant(self):
        prefix = "__test_pedigree"
        self.pheno_file = "%s_mch.txt" % (prefix)
        with open(self.pheno_file, "w") as f:
            f.write("""FID\tIID\tBMI\tIBM\tMSA
1\t1\t0.1\t1.0\t1.0
2\t2\t0.2\t0.5\t1.0
3\t3\t0.3\t0.6\t1.0
4\t4\t0.4\t0.5\t1.0
5\t5\t0.5\t1.0\t1.0
6\t6\t0.6\t0.1\t1.0
17\t7\t0.1\t1.0\t1.0
8\t8\t0.2\t0.5\t1.0
9\t9\t0.3\t0.6\t1.0
10\t10\t0.4\t0.5\t1.0
11\t11\t0.5\t1.0\t1.0
12\t12\t0.6\t0.1\t1.0""")

        PhenoCovar.sex_as_covariate = True
        pc = PhenoCovar()
        ped_parser = PedigreeParser(self.map_filename, self.ped_filename)
        ped_parser.load_mapfile()
        ped_parser.load_genotypes(pc)
        with open(self.pheno_file) as f2:
            pc.load_phenofile(f2, indices=[3])
            index = 0
            mapdata = get_lines(self.map_filename, split=True)

            with self.assertRaises(InvariantVar):
                for snp in ped_parser:
                    for y in pc:
                        non_missing = numpy.ones(len(snp.genotype_data),
                                                 dtype=bool)
                        (pheno, covariates, nonmissing) = y.get_variables(
                            numpy.invert(non_missing))
Пример #6
0
    def test_pheno_header(self):
        pc = PhenoCovar()
        load_pedigree(pc, self.ped)

        pc.load_phenofile(self.header)

        self.assertEqual(6, len(pc.phenotype_data[0]))
        self.assertEqual(1, len(pc.phenotype_names))
        self.assertEqual("BMI", pc.phenotype_names[0])

        phenotype_values = [0.9, 1.0, 0.4, 0.8, 1, 0.1]
        for idx in xrange(0, len(phenotype_values)):
            self.assertAlmostEqual(phenotype_values[idx], pc.phenotype_data[0][idx])
Пример #7
0
    def test_pheno_no_header(self):
        # Indicate that we want to use sex as a covariate
        PhenoCovar.sex_as_covariate = False
        pc = PhenoCovar()
        load_pedigree(pc, self.ped)

        pc.load_phenofile(self.no_header)
        self.assertEqual(1, len(pc.phenotype_data))
        self.assertEqual(0, len(pc.covariate_data))
        self.assertEqual(6, len(pc.phenotype_data[0]))
        self.assertEqual(1, len(pc.phenotype_names))
        self.assertEqual("Pheno-1", pc.phenotype_names[0])

        phenotype_values = [0.9, 1.0, 0.4, 0.8, 1, 0.1]
        for idx in xrange(0, len(phenotype_values)):
            self.assertAlmostEqual(phenotype_values[idx], pc.phenotype_data[0][idx])
Пример #8
0
    def test_invalid_input(self):
        pc = PhenoCovar()

        with self.assertRaises(InvalidSelection):
            pc.load_phenofile(self.header, indices=[2])
        with self.assertRaises(InvalidSelection):
            pc.load_phenofile(self.header, indices=[1, 2, 3])

        with self.assertRaises(MalformedInputFile):
            pc.load_phenofile(self.no_header, names=["BMI"])
        with self.assertRaises(MalformedInputFile):
            pc.load_phenofile(self.no_header, names=["BMI", "AGE"])

        # Make sure that we throw an exception when we provide a name that isn't in the header
        with self.assertRaises(InvalidSelection):
            pc.load_phenofile(self.header, names=["AGE"])
Пример #9
0
    def test_pheno_with_missing(self):
        PhenoCovar.sex_as_covariate = False

        phenotypes = [[-9, 1.0, 0.5], [0.2, -9, 1.0], [0.3, 0.6, -9],
                      [0.4, 0.5, 0.5], [0.5, 1.0, 1.0], [0.6, 0.1, 0.2]]

        pc = PhenoCovar()
        load_pedigree(pc, self.ped)
        with open(self.filenames[7]) as f:
            pc.load_phenofile(f, indices=[1])
        with open(self.filenames[7]) as f:
            pc.load_covarfile(f, indices=[2, 3])

        missing = [True, True, True, False, False, False]
        index = 0
        for y in pc:
            pheno, covars, nonmissing = y.get_variables()
            self.assertEqual(3, numpy.sum(nonmissing))
            self.assertEqual(missing, list(numpy.invert(nonmissing)))
Пример #10
0
    def testPedMultiPheno(self):
        PhenoCovar.sex_as_covariate = True
        pc = PhenoCovar()
        ped_parser = PedigreeParser(self.map_filename, self.ped_filename)
        ped_parser.load_mapfile()
        ped_parser.load_genotypes(pc)

        with open(self.pheno_file) as f:
            pc.load_phenofile(f, indices=[2, 3])
        mapdata = get_lines(self.map_filename, split=True)

        sex = [1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1]
        pheno_data = [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6],
                      [1.0, 0.5, 0.6, 0.5, 1.0, 0.1],
                      [0.5, 1.0, 0.1, 0.5, 1.0, 0.2]]
        dual_pheno = [[1.0, 0.5, 0.6, 0.5, 1.0, 0.1, 0.5, 0.6, 0.5, 1.0, 0.1],
                      [0.5, 1.0, 0.1, 0.5, 1.0, 0.2, 1.0, 0.1, 0.5, 1.0, 0.2]]

        self.assertEqual(2, len(pc.phenotype_data))
        index = 0
        for snp in ped_parser:
            self.assertEqual(int(mapdata[index][0]), snp.chr)
            self.assertEqual(int(mapdata[index][3]), snp.pos)
            self.assertEqual(mapdata[index][1], snp.rsid)
            self.assertEqual(self.genotypes[index], list(snp.genotype_data))

            idx = 0
            for y in pc:
                non_missing = numpy.ones(len(snp.genotype_data), dtype=bool)
                non_missing[6] = False
                (pheno, covariates,
                 nm_indata) = y.get_variables(numpy.invert(non_missing))
                for i in range(0, 11):
                    self.assertEqual(sex[i], covariates[0][i])
                    self.assertAlmostEqual(dual_pheno[idx][i], pheno[i])

                idx += 1

            self.assertEqual(2, idx)

            index += 1
        self.assertEqual(7, index)
Пример #11
0
    def testWithSample(self):

        pc = PhenoCovar()
        parser = libgwas.bgen_parser.Parser(self.nomissing,
                                            self.nomissing_sample)

        parser.load_family_details(pc)
        parser.load_genotypes()
        with open(self.nomissing_sample) as file:
            pc.load_phenofile(file, names=['plink_pheno'], sample_file=True)
        with open(self.nomissing_sample) as file:
            pc.load_covarfile(file, names=['sex'], sample_file=True)

        idx = 0
        for id in self.ind_ids:
            self.assertTrue(id in pc.pedigree_data)
            self.assertEqual(self.phenotypes[idx], pc.phenotype_data[0][idx])
            self.assertEqual(self.sex[idx], pc.covariate_data[0][idx])
            idx += 1
        self.assertEqual(12, idx)
Пример #12
0
    def testPedMultiPheno(self):
        PhenoCovar.sex_as_covariate = True
        pc = PhenoCovar()
        ped_parser = PedigreeParser(self.map_filename, self.ped_filename)
        ped_parser.load_mapfile()
        ped_parser.load_genotypes(pc)
        pc.load_phenofile(open(self.pheno_file), indices=[2, 3])
        mapdata = [x.strip().split() for x in open(self.map_filename).readlines()]

        sex = [1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1]
        pheno_data = [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [1.0, 0.5, 0.6, 0.5, 1.0, 0.1], [0.5, 1.0, 0.1, 0.5, 1.0, 0.2]]
        dual_pheno = [
            [1.0, 0.5, 0.6, 0.5, 1.0, 0.1, 0.5, 0.6, 0.5, 1.0, 0.1],
            [0.5, 1.0, 0.1, 0.5, 1.0, 0.2, 1.0, 0.1, 0.5, 1.0, 0.2],
        ]

        self.assertEqual(2, len(pc.phenotype_data))
        index = 0
        for snp in ped_parser:
            self.assertEqual(int(mapdata[index][0]), snp.chr)
            self.assertEqual(int(mapdata[index][3]), snp.pos)
            self.assertEqual(mapdata[index][1], snp.rsid)
            self.assertEqual(self.genotypes[index], list(snp.genotype_data))

            idx = 0
            for y in pc:
                non_missing = numpy.ones(len(snp.genotype_data), dtype=bool)
                non_missing[6] = False
                (pheno, covariates, nm_indata) = y.get_variables(numpy.invert(non_missing))
                for i in range(0, 11):
                    self.assertEqual(sex[i], covariates[0][i])
                    self.assertAlmostEqual(dual_pheno[idx][i], pheno[i])

                idx += 1

            self.assertEqual(2, idx)

            index += 1
        self.assertEqual(7, index)
Пример #13
0
    def testForInvariant(self):
        prefix = "__test_pedigree"
        self.pheno_file = "%s_mch.txt" % (prefix)
        f = open(self.pheno_file, "w")
        f.write(
            """FID\tIID\tBMI\tIBM\tMSA
1\t1\t0.1\t1.0\t1.0
2\t2\t0.2\t0.5\t1.0
3\t3\t0.3\t0.6\t1.0
4\t4\t0.4\t0.5\t1.0
5\t5\t0.5\t1.0\t1.0
6\t6\t0.6\t0.1\t1.0
17\t7\t0.1\t1.0\t1.0
8\t8\t0.2\t0.5\t1.0
9\t9\t0.3\t0.6\t1.0
10\t10\t0.4\t0.5\t1.0
11\t11\t0.5\t1.0\t1.0
12\t12\t0.6\t0.1\t1.0"""
        )
        f.close()

        PhenoCovar.sex_as_covariate = True
        pc = PhenoCovar()
        ped_parser = PedigreeParser(self.map_filename, self.ped_filename)
        ped_parser.load_mapfile()
        ped_parser.load_genotypes(pc)
        pc.load_phenofile(open(self.pheno_file), indices=[3])
        index = 0

        mapdata = [x.strip().split() for x in open(self.map_filename).readlines()]

        with self.assertRaises(InvariantVar):
            for snp in ped_parser:
                for y in pc:
                    non_missing = numpy.ones(len(snp.genotype_data), dtype=bool)
                    (pheno, covariates, nonmissing) = y.get_variables(numpy.invert(non_missing))
Пример #14
0
            mphenos = []
            if args.mphenos != "":
                mphenos = args.mphenos.split(",")

            nphenos = []
            if args.pheno_names != "":
                nphenos = args.pheno_names.split(",")

            if len(mphenos) + len(nphenos) == 0 and not args.all_pheno:
                libgwas.Exit("You must select one or more phenotypes when ")
            sample_file = False
            pheno_filename = args.pheno
            if args.sample_pheno:
                pheno_filename = args.sample_pheno
                sample_file = True
            pheno_covar.load_phenofile(pheno_filename, mphenos, nphenos, sample_file)

        if args.covar:
            pheno_covar.load_covarfile(args.covar, args.covar_numbers.split(","), args.covar_names.split(","))
        pheno_covar.do_standardize_variables = True
        return dataset, pheno_covar

    def ParseImputeFile(self, filename, offset=-1, count=-1):
        chroms = []
        archives = []
        infos = []

        for line in open(filename):
            words = line.split()

            if len(words) < 2:
Пример #15
0
    def LoadCmdLine(self, args=sys.argv[1:]):
        """Parse user arguments using argparse and set up components"""
        parser = argparse.ArgumentParser(description="MV Test: " + __version__,
                                         epilog="""
mvtest.py is uses many of the same arguments as plink, but there are a few
differences, so please consider the list above carefully.
        """)

        parser.add_argument("-v",
                            action='store_true',
                            help="Print version number")
        parser.add_argument(
            "--vall",
            action='store_true',
            help="Print version number along with each dependency")

        parser.add_argument("--chr",
                            type=int,
                            default=-1,
                            metavar="N",
                            help="Select Chromosome")
        parser.add_argument(
            "--snps",
            type=str,
            default="",
            help="Comma-delimited list of SNP(s): rs1,rs2,rs3-rs6")
        parser.add_argument("--from-bp",
                            type=int,
                            metavar="START",
                            help="SNP range start")
        parser.add_argument("--to-bp",
                            type=int,
                            metavar="END",
                            help="SNP range end")
        parser.add_argument("--from-kb",
                            type=int,
                            metavar="START",
                            help="SNP range start")
        parser.add_argument("--to-kb",
                            type=int,
                            metavar="END",
                            help="SNP range end")
        parser.add_argument("--from-mb",
                            type=int,
                            metavar="START",
                            help="SNP range start")
        parser.add_argument("--to-mb",
                            type=int,
                            metavar="END",
                            help="SNP range end")
        parser.add_argument(
            "--exclude",
            type=str,
            default="",
            help="Comma-delimited list of rsids to be excluded")

        # For now, I'm not implementing keep, since we don't have any real meaningful need for analyzing individuals
        # PLINK does, but we don't do the QC stuff they do.
        parser.add_argument(
            "--keep",
            type=str,
            default="",
            help="Comma-delimited list of individuals to be analyzed")
        parser.add_argument(
            "--remove",
            type=str,
            default="",
            help=
            "Comma-delimited list of individuals to be removed from analysis")

        parser.add_argument("--file",
                            type=str,
                            help="Prefix for .ped and .map files")
        parser.add_argument("--ped",
                            type=argparse.FileType('r'),
                            help="PLINK compatible .ped file")
        parser.add_argument("--map",
                            type=argparse.FileType('r'),
                            help="PLINK compatible .map file")
        parser.add_argument("--map3",
                            action='store_true',
                            help="MAP file has only 3 columns")
        parser.add_argument("--no-sex",
                            action='store_true',
                            help="Pedigree file doesn't have column 5 (sex)")
        parser.add_argument(
            "--no-parents",
            action="store_true",
            help="Pedigree file doesn't have columns 3 and 4 (parents)")
        parser.add_argument(
            "--no-fid",
            action="store_true",
            help="Pedigree file doesn't have column 1 (family ID)")
        parser.add_argument(
            "--no-pheno",
            action="store_true",
            help="Pedigree file doesn't have column 6 (phenotype")
        parser.add_argument("--liability",
                            action="store_true",
                            help="Pedigree file has column 7 (liability)")

        parser.add_argument("--bfile",
                            type=str,
                            help="Prefix for .bed, .bim and .fam files")
        parser.add_argument("--bed",
                            type=argparse.FileType('r'),
                            help="Binary Ped file (.bed)")
        parser.add_argument("--bim",
                            type=argparse.FileType('r'),
                            help="Binary ped marker file (.bim)")
        parser.add_argument("--fam",
                            type=argparse.FileType('r'),
                            help="Binary ped family file (.fam)")

        parser.add_argument("--tfile",
                            type=str,
                            help="Prefix for .tped and .tfam files")
        parser.add_argument("--tped",
                            type=argparse.FileType('r'),
                            help="Transposed Pedigree file (.tped)")
        parser.add_argument("--tfam",
                            type=argparse.FileType('r'),
                            help="Transposed pedigre Family file (.tfam)")
        parser.add_argument(
            "--compressed",
            action="store_true",
            help="Ped/TPed compressed with gzip (named .ped.tgz or .tped.tgz)")

        parser.add_argument(
            "--impute",
            type=argparse.FileType('r'),
            help="File containing list of impute output for analysis")
        parser.add_argument(
            "--impute-fam",
            type=argparse.FileType('r'),
            help="File containing family details for impute data")
        parser.add_argument(
            "--impute-offset",
            type=int,
            default=-1,
            help="Impute file index (1 based) to begin analysis")
        parser.add_argument(
            "--impute-count",
            type=int,
            default=-1,
            help="Number of impute files to process (for this node)")
        parser.add_argument(
            "--impute-uncompressed",
            action="store_true",
            help="Indicate that the impute input is not gzipped, but plain text"
        )
        parser.add_argument(
            "--impute-encoding",
            type=str,
            choices=['additive', 'dominant', 'recessive', 'genotype'],
            default='additive',
            help='Genetic model to be used')
        parser.add_argument("--impute-info-ext",
                            type=str,
                            default='info',
                            help="Portion of filename denotes info filename")
        parser.add_argument("--impute-gen-ext",
                            type=str,
                            default='gen.gz',
                            help="Portion of filename that denotes gen file")
        parser.add_argument(
            "--impute-info-thresh",
            type=float,
            default=0.4,
            help="Threshold for filtering imputed SNPs with poor 'info' values"
        )

        parser.add_argument(
            "--mach",
            type=argparse.FileType('r'),
            help="File containing list of MACH output for analysis")
        parser.add_argument("--mach-offset",
                            type=int,
                            default=-1,
                            help="Mach file index (1 based) to begin analysis")
        parser.add_argument(
            "--mach-count",
            type=int,
            default=-1,
            help="Number of mach files to process (for this node)")
        parser.add_argument("--mach-uncompressed",
                            action="store_true",
                            help="Indicate that the mach input is not gzipped")
        parser.add_argument(
            "--mach-chunk-size",
            type=int,
            default=100000,
            help=
            "Max number of loci to load at once (higher increases memory requirements with some speed benefits)"
        )
        parser.add_argument("--mach-info-ext",
                            type=str,
                            default="info.gz",
                            help="Portion of filename denotes info filenames")
        parser.add_argument("--mach-dose-ext",
                            type=str,
                            default="dose.gz",
                            help="Portion of filename that denotes dose files")
        parser.add_argument("--mach-min-rsquared",
                            type=float,
                            default=0.3,
                            help="Filter out loci with RSquared < this value")
        parser.add_argument(
            "--mach-chrpos",
            action="store_true",
            help=
            "When true, first col in .info file must be chr:pos (additional pieces allowed)"
        )

        parser.add_argument("--pheno",
                            type=argparse.FileType('r'),
                            help="File containing phenotypes")
        parser.add_argument("--sample-pheno",
                            type=argparse.FileType('r'),
                            help="(Mach) Sample file containing phenotypes")
        parser.add_argument(
            "--mphenos",
            type=str,
            default="",
            help=
            "Column number(s) for phenotype to be analyzed if number of columns > 1"
        )
        parser.add_argument(
            "--pheno-names",
            type=str,
            default="",
            help=
            "Name for phenotype(s) to be analyzed (must be in --pheno file)")
        parser.add_argument("--all-pheno",
                            action="store_true",
                            help="Analyze all columns from the phenotype file")
        #parser.add_argument("--all-pheno", action='store_true', help="Analyze each phenotype")

        parser.add_argument("--covar",
                            type=argparse.FileType('r'),
                            help="File containing covariates")
        parser.add_argument("--sample-covar",
                            type=argparse.FileType('r'),
                            help="(Mach) Sample file containing covariates")
        parser.add_argument("--covar-numbers",
                            type=str,
                            default="",
                            help="Comma-separated list of covariate indices")
        parser.add_argument("--covar-names",
                            type=str,
                            default="",
                            help="Comma-separated list of covariate names")
        parser.add_argument(
            "--sex",
            action='store_true',
            help="Use sex from the pedigree file as a covariate")
        parser.add_argument("--missing-phenotype",
                            type=float,
                            default=-9.0,
                            help="Encoding for missing phenotypes")

        parser.add_argument("--maf",
                            type=float,
                            default=0.0,
                            help="Minimum MAF allowed for analysis")
        parser.add_argument("--max-maf",
                            type=float,
                            default=1.0,
                            help="MAX MAF allowed for analysis")
        parser.add_argument("--geno",
                            type=float,
                            default=1.0,
                            help="MAX per-SNP missing for analysis")
        parser.add_argument("--mind",
                            type=float,
                            default=1.0,
                            help="MAX per-person missing")

        parser.add_argument("--verbose",
                            action='store_true',
                            help="Output additional data details")

        parser.set_defaults(all_pheno=False, sex=False, mach_chrpos=False)
        args = parser.parse_args(args)

        # Report version, if requested, and exit
        if args.v:
            print("%s: %s" % (os.path.basename(__file__), __version__),
                  file=sys.stderr)
            sys.exit(0)

        if args.vall:
            print("%s: %s" % (os.path.basename(__file__), __version__),
                  file=sys.stderr)
            print("%s: %s" %
                  (os.path.dirname(libgwas.__file__), libgwas.__version__),
                  file=sys.stderr)
            print("%s: %s" %
                  (os.path.dirname(scipy.__file__), scipy.__version__),
                  file=sys.stderr)
            print("%s: %s" %
                  (os.path.dirname(numpy.__file__), numpy.__version__),
                  file=sys.stderr)
            sys.exit(0)

        ###############################################################################################################
        # Here we deal with the various ways we filter SNPs in and out of anlysis
        # We might handle MACH files differently. We'll default the chromosome
        # to be "NA" which is how those can be returned.
        if args.mach is None or args.mach_chrpos:
            BoundaryCheck.chrom = args.chr
        else:
            if args.chr != -1:
                libgwas.Exit(
                    ("Positional based filtering (--chr, --from/--to)" +
                     " only work with mach_chrpos. See manual for details."))
            BoundaryCheck.chrom = "NA"
        snps = args.snps.split(",")
        try:
            b = BoundaryCheck(bp=(args.from_bp, args.to_bp),
                              kb=(args.from_kb, args.to_kb),
                              mb=(args.from_mb, args.to_mb))
        except InvalidBoundarySpec as e:
            print("Invalid boundary spec associated: %s" %
                  (e.malformed_boundary),
                  file=sys.stderr)
            sys.exit(1)
        try:
            s = SnpBoundaryCheck(snps=snps)
        except InvalidBoundarySpec as e:
            print("Invalid SNP boundary defined: %s" % (e.malformed_boundary),
                  file=sys.stderr)
            print(
                "SNPs must be either single or have be a range such as rs123-rs345",
                file=sys.stderr)
            sys.exit(1)

        if b.valid and s.valid:
            print(
                "Only one type of boundary conditions is permitted. Either use --from-bp, etc. or rs123-rs345. ",
                file=sys.stderr)
            sys.exit(1)

        if len(b.bounds) > 0 and not b.valid:
            if BoundaryCheck.chrom == "NA":
                libgwas.Exit(
                    ("Positional based filtering (--chr, --from/--to)" +
                     " only work with mach_chrpos. See manual for details."))

        if s.valid:
            DataParser.boundary = s
        # If b isn't valid, we still want to potentially allow for chr and SNPs, it just won't have
        else:
            b.LoadSNPs(snps)
            # any actual boundary listings
            DataParser.boundary = b
        DataParser.boundary.LoadExclusions(snps=args.exclude.split(","))

        ###############################################################################################################
        # Setup the various Dataset filter criteria
        DataParser.min_maf = args.maf
        DataParser.max_maf = args.max_maf
        DataParser.snp_miss_tol = args.geno
        DataParser.ind_miss_tol = args.mind

        DataParser.ind_exclusions = ParseIndList(args.remove)

        PhenoCovar.sex_as_covariate = args.sex

        if args.compressed:
            DataParser.compressed_pedigree = True

        DataParser.has_sex = not args.no_sex
        DataParser.has_parents = not args.no_parents
        DataParser.has_fid = not args.no_fid
        DataParser.has_pheno = not args.no_pheno
        DataParser.has_liability = args.liability

        pheno_covar = PhenoCovar()
        self.verbose = False
        if args.verbose:
            self.verbose = True

        if args.file != None or args.ped or args.map:
            if args.ped and not args.map or args.map and not args.ped:
                print(
                    "When analyzing pedigree data, both .map and .ped must be specified",
                    file=sys.stderr)
                sys.exit(1)
            if args.ped:
                dataset = pedigree_parser.Parser(args.map.name, args.ped.name)
            else:
                dataset = pedigree_parser.Parser("%s.map" % (args.file),
                                                 "%s.ped" % (args.file))

            dataset.load_mapfile(map3=args.map3)
            dataset.load_genotypes(pheno_covar)
        elif args.tfile != None or args.tped or args.tfam:
            if args.tped and not args.tfam or args.tfam and not args.tped:
                print(
                    "When analyzing transposed pedigree data, both .tfam and .tped must be specified",
                    file=sys.stderr)
                sys.exit(1)
            if args.tped:
                dataset = transposed_pedigree_parser.Parser(
                    args.tfam.name, args.tped.name)
            else:
                dataset = transposed_pedigree_parser.Parser(
                    "%s.tfam" % (args.tfile), "%s.tped" % (args.tfile))
            dataset.load_tfam(pheno_covar)
            dataset.load_genotypes()
        elif args.bfile != None:
            dataset = bed_parser.Parser("%s.fam" % (args.bfile),
                                        "%s.bim" % (args.bfile),
                                        "%s.bed" % (args.bfile))
            dataset.load_bim(map3=args.map3)
            dataset.load_fam(pheno_covar)
            dataset.load_genotypes()
        elif args.bed or args.bim or args.fam:
            if (args.bed and not args.fam or not args.bim) or (
                    args.bim and not args.bed
                    or not args.fam) or (args.fam and not args.bed
                                         or not args.bim):
                print(
                    "When analyzing binary pedigree data, .bed, .bim and .fam files must be provided",
                    file=sys.stderr)
                sys.exit(1)
            dataset = bed_parser.Parser(args.fam, args.bim, args.bed)
            dataset.load_bim(map3=args.map3)
            dataset.load_fam(pheno_covar)
            dataset.load_genotypes()
        elif args.impute:
            DataParser.compressed_pedigree = not args.impute_uncompressed

            if (args.impute_offset > 0 and args.impute_count == -1) or (
                    args.impute_offset == -1 and args.impute_count > 0):
                print(
                    "--impute-count and --impute_offset must both > 0 if one is set other than -1.  ",
                    file=sys.stderr)
                sys.exit(1)
            if DataParser.snp_miss_tol != 1.0:
                print("--geno does not have any impact on imputed data",
                      file=sys.stderr)
                sys.exit(1)
            if DataParser.ind_miss_tol != 1.0:
                print("--mind does not have any impact on imputed data",
                      file=sys.stderr)
                sys.exit(1)
            impute_parser.SetEncoding(args.impute_encoding)
            impute_parser.Parser.info_ext = args.impute_info_ext
            impute_parser.Parser.info_threshold = args.impute_info_thresh
            libgwas.ExitIf(
                "--impute-fam is required for when processing imputed data",
                args.impute_fam == None)
            archives, chroms, infos = self.ParseImputeFile(
                args.impute.name, args.impute_offset, args.impute_count)
            dataset = impute_parser.Parser(args.impute_fam.name, archives,
                                           chroms, infos)
            dataset.load_family_details(pheno_covar)
            dataset.load_genotypes()
        elif args.mach:

            DataParser.compressed_pedigree = not args.mach_uncompressed
            if (args.mach_offset > 0
                    and args.mach_count == -1) or (args.mach_offset == -1
                                                   and args.impute_count > 0):
                print(
                    "--mach-count and --mach_offset must both be > 0 if one is set other than -1. ",
                    file=sys.stderr)
                sys.exit(1)
            if DataParser.snp_miss_tol != 1.0:
                print("--geno does not have any impact on imputed data",
                      file=sys.stderr)
                sys.exit(1)
            if DataParser.ind_miss_tol != 1.0:
                print("--mind does not have any impact on imputed data",
                      file=sys.stderr)
                sys.exit(1)
            if BoundaryCheck.chrom != "NA" and not args.mach_chrpos:
                libgwas.Exit(
                    ("Positional based filtering (--chr, --from/--to)" +
                     " only work with mach_chrpos. See manual for details."))
            mach_parser.Parser.chrpos_encoding = args.mach_chrpos
            mach_parser.Parser.info_ext = args.mach_info_ext
            mach_parser.Parser.dosage_ext = args.mach_dose_ext
            mach_parser.Parser.chunk_stride = args.mach_chunk_size
            mach_parser.Parser.min_rsquared = args.mach_min_rsquared
            archives, infos = self.ParseMachFile(args.mach.name,
                                                 args.mach_offset,
                                                 args.mach_count)
            dataset = mach_parser.Parser(archives, infos)
            dataset.load_family_details(pheno_covar)
            dataset.load_genotypes()

        else:
            parser.print_usage(sys.stderr)
            print(
                "\nNo data has been specified. Users must specify either pedigree or transposed pedigree to continue",
                file=sys.stderr)
            sys.exit(1)

        if args.pheno or args.sample_pheno:
            mphenos = []
            if args.mphenos != "":
                mphenos = args.mphenos.split(",")

            nphenos = []
            if args.pheno_names != "":
                nphenos = args.pheno_names.split(",")

            if len(mphenos) + len(nphenos) == 0 and not args.all_pheno:
                libgwas.Exit("You must select one or more phenotypes when ")
            sample_file = False
            pheno_filename = args.pheno
            if args.sample_pheno:
                pheno_filename = args.sample_pheno
                sample_file = True
            pheno_covar.load_phenofile(pheno_filename, mphenos, nphenos,
                                       sample_file)

        if args.covar:
            pheno_covar.load_covarfile(args.covar,
                                       args.covar_numbers.split(","),
                                       args.covar_names.split(","))
        pheno_covar.do_standardize_variables = True
        return dataset, pheno_covar
Пример #16
0
            mphenos = []
            if args.mphenos != "":
                mphenos = args.mphenos.split(",")

            nphenos = []
            if args.pheno_names != "":
                nphenos = args.pheno_names.split(",")

            if len(mphenos) + len(nphenos) == 0 and not args.all_pheno:
                libgwas.Exit("You must select one or more phenotypes when ")
            sample_file = False
            pheno_filename = args.pheno
            if args.sample_pheno:
                pheno_filename = args.sample_pheno
                sample_file = True
            pheno_covar.load_phenofile(pheno_filename, mphenos, nphenos,
                                       sample_file)

        if args.covar:
            pheno_covar.load_covarfile(args.covar,
                                       args.covar_numbers.split(","),
                                       args.covar_names.split(","))
        pheno_covar.do_standardize_variables = True
        return dataset, pheno_covar

    def ParseImputeFile(self, filename, offset=-1, count=-1):
        chroms = []
        archives = []
        infos = []

        for line in open(filename):
            words = line.split()
Пример #17
0
    def test_pheno_header_with_string_indices(self):
        pc = PhenoCovar()
        load_pedigree(pc, self.ped)

        # Application was feeding in empty string for names and excepting
        pc.load_phenofile(self.header, indices='1')