def test_phase_trivial_cases(self): '''Check phasing trivial cases in trios. The trio data is (0,1=parents, 2=child). The solution is kept in the trio test file as the fictitious individual 3.''' g = self.problem.genotype itu.assert_size_equals(self.problem.genotype, 19, 3) assert_equal(self.problem.error.shape, (19, 3), 'Incorrect error array size') trio = (0, 1, 2) solution = self.solution.genotype.data h = self.problem.haplotype assert_problem_stats(self.problem, 6 * g.num_snps, 0, 0) self.phaser.run(self.problem) for snp in self.problem.snp_range: expected_parent_genotype = solution[snp, trio[0:CHILD], :] #expected_child_genotype = solution[snp,trio[CHILD],:] expected_child_haplotype = solution[snp, trio[CHILD], :] parent_genotype = g.data[snp, trio[0:CHILD], :] #child_genotype = g.data[snp,trio[CHILD]] child_haplotype = h.data[snp, trio[CHILD]] ''' print 'SNP', snp print 'Data', g.data[snp,trio,:] print 'Imputed parent', parent_genotype print 'Child hap', child_haplotype print 'Solution hap', solution[snp,trio,:] ''' assert_equal(child_haplotype, expected_child_haplotype, 'Wrong child haplotype by trivial phaser at snp %d' % (snp,)) assert_equal(parent_genotype, expected_parent_genotype, 'Wrong parent genotype imputation by trivial phaser at snp %d' % (snp,)) #assert_equal(np.sort(child_genotype), np.sort(expected_child_genotype), 'Wrong child genotype imputation by trivial phaser at snp %d' % (snp,)) assert_problem_stats(self.problem, 6 * g.num_snps, 66, 6, error_rate=0.1)
def test_phase_trivial_cases(self): '''Check phasing trivial cases in trios. The trio data is (0,1=parents, 2=child). The solution is kept in the trio test file as the fictitious individual 3.''' g = self.problem.genotype itu.assert_size_equals(self.problem.genotype, 1, 2) duo = (0, 1) solution = self.solution.genotype.data h = self.problem.haplotype assert_problem_stats(self.problem, 4 * g.num_snps, 0, 0) self.phaser.run(self.problem) for snp in self.problem.snp_range: expected_parent_genotype = solution[snp, duo[0], :] #expected_child_genotype = solution[snp,trio[CHILD],:] expected_child_haplotype = solution[snp, duo[1], :] parent_genotype = g.data[snp, duo[0], :] #child_genotype = g.data[snp,trio[CHILD]] child_haplotype = h.data[snp, duo[1]] ''' print 'SNP', snp print 'Data', g.data[snp,trio,:] print 'Imputed parent', parent_genotype print 'Child hap', child_haplotype print 'Solution hap', solution[snp,trio,:] ''' assert_equal(child_haplotype, expected_child_haplotype, 'Wrong child haplotype by trivial phaser at snp %d' % (snp,)) assert_equal(parent_genotype, expected_parent_genotype, 'Wrong parent genotype imputation by trivial phaser at snp %d' % (snp,)) #assert_equal(np.sort(child_genotype), np.sort(expected_child_genotype), 'Wrong child genotype imputation by trivial phaser at snp %d' % (snp,)) assert_problem_stats(self.problem, 4 * g.num_snps, 4, 0)
def test_phase_family(self): '''Check phasing trivial cases in all genotyped trios.''' problem = io.read_plink(pedigree=itu.HUTT_PED, prefix=itu.GENOTYPE_SAMPLE, haplotype=None) itu.assert_size_equals(problem.genotype, 8, 1415) assert_equal(len(problem.trios()), 869, 'Unexpected # of genotyped trios') self.phaser.run(problem, PhaseParam(debug=False)) itu.assert_problem_stats(problem, 22640, 20225, 144)
def test_phase_trivial_cases_all_trios(self): '''Check phasing trivial cases in all genotyped trios.''' itu.assert_size_equals(self.problem.genotype, 8, 1415) assert_equal(len(self.problem.trios()), 869, 'Unexpected # of genotyped trios') self.phaser.run(self.problem) itu.assert_problem_stats(self.problem, 22640, 18567, 10)
def test_child_comparison_one_parent(self): '''Test applying child comparison to a nuclear family with many genotyped kids but only one genotyped parent.''' problem = io.read_npz(itu.FAMILY945_ONE_PARENT_STAGE2) itu.assert_size_equals(problem.genotype, 3218, 8) itu.assert_problem_stats(problem, 51488, 44150, 96) phaser = family_child_comparison_phaser(debug=False) phaser.run(problem) itu.assert_problem_stats(problem, 51488, 47343, 101)
def test_phase_trivial_cases_all_trios(self): '''Check phasing trivial cases in all genotyped trios.''' itu.assert_size_equals(self.problem.genotype, 8, 1415) assert_equal(len(self.problem.trios()), 869, 'Unexpected # of genotyped trios') self.phaser.run(self.problem) itu.assert_problem_stats(self.problem, 22640, 18567, 10) #--------------------------------------------- # Private Methods #---------------------------------------------
def test_family_12(self): '''Test comparing sibs with non-genotyped parents (stage 4).''' problem = io.read_npz(itu.FAMILY12_STAGE2) itu.assert_size_equals(problem.genotype, 3218, 7) itu.assert_problem_stats(problem, 45052, 42162, 237) assert_equal(len(list(problem.families(genotyped=False))), 1, 'Incorrect number of families') phaser = family_sib_comparison_phaser() phaser.run(problem, PhaseParam(single_member=1)) itu.assert_problem_stats(problem, 45052, 42162, 237)
def test_family_963(self): '''Test comparing sibs with non-genotyped parents (stage 4). This was a problematic family.''' problem = io.read_npz(itu.FAMILY963_STAGE4) itu.assert_size_equals(problem.genotype, 3218, 3) itu.assert_problem_stats(problem, 19308, 19286, 23) assert_equal(len(list(problem.families(genotyped=False))), 1, 'Incorrect number of families') phaser = family_sib_comparison_phaser() phaser.run(problem) itu.assert_problem_stats(problem, 19308, 19286, 23)
def test_family_2003_need_poo_alignment(self): '''Test comparing sibs with non-genotyped parents (stage 4). This case highlights the need to align POO-phases, i.e., swap founder haps to correctly patch families at individual ID 28412 (our original index 386; in this problem, index 10).''' problem = io.read_npz(itu.FAMILY2003_STAGE3) itu.assert_size_equals(problem.genotype, 3218, 9) itu.assert_problem_stats(problem, 57924, 43339, 85) assert_equal(len(list(problem.families(genotyped=False))), 1, 'Incorrect number of families') #f = problem.families(genotyped=False)[0] #print f.member_list #print problem.pedigree.sample_id phaser = family_sib_comparison_phaser() phaser.run(problem) itu.assert_problem_stats(problem, 57924, 57515, 85)
def test_outer_duo(self): '''Test applying child comparison to a nuclear family with many genotyped kids but only one genotyped parent. Seems to be fine for now: too many errors are flagged, but we are not going to split hair.''' p = self.problem # h = p.haplotype # (f, m) = (self.family.father, self.family.mother) snp = p.info.snp_by_name('rs5746679') assert_equal(snp, [3], 'Wrong SNP index') itu.assert_size_equals(p.genotype, 3218, 7) itu.assert_problem_stats(p, 45052, 40086, 4) # print 'genotypes' # print p.genotype.data[snp, :, :] # print 'haplotypes' # print p.haplotype.data[snp, :, :] phaser = family_phaser() phaser.run(p) itu.assert_problem_stats(p, 45052, 45023, 25)
def test_phase_and_get_stats(self): '''Test gathering phasing statistics in a Stats object.''' nuclear_family_phaser().run(self.problem) g_orig = self.problem.genotype.data.copy() stats = v.Stats(g_orig, self.problem.haplotype, self.problem.num_errors) itu.assert_problem_stats(self.problem, 167336, 137693, 102) # Check stats fields partially_filled_genotype = np.where(recode.recode_single_genotype(self.problem.genotype.data) < 0) assert_equal(len(partially_filled_genotype[0]), 279, 'Unexpected # of partially-filled genotypes') assert_equal(stats.imputed.total, 185, 'Unexpected # of fully-imputed genotypes') assert_almost_equal(stats.imputed.fraction, 0.00221, decimal=5, err_msg='Unexpected fraction of fully-imputed genotypes') assert_equal(stats.imputed_partial.total, 45, 'Unexpected # of partially-imputed genotypes') # Check that printouts don't crash us stats.pprint(open(os.devnull, 'wb')) # Check that stats is pickable and unpicklable out_name = util.temp_filename(suffix='.npz') out = open(out_name, 'wb') np.savez(out, stats=np.array([stats])) out.close() loaded = np.load(out_name) assert_equal(loaded['stats'][0].imputed_partial.total, stats.imputed_partial.total, 'Pickling & unpickling a Stats object failed')