From a374aca07b994ea2fbfbb03f936892e9cb4c21d0 Mon Sep 17 00:00:00 2001 From: Nahuel Soldevilla Date: Tue, 12 Sep 2023 16:32:03 -0300 Subject: [PATCH 1/2] IB-2: Test scenario: COP of an F1 with itself --- .../pedigree/cop/CopServiceImplTest.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/test/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopServiceImplTest.java b/src/test/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopServiceImplTest.java index 17a17eb533..500609e83e 100644 --- a/src/test/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopServiceImplTest.java +++ b/src/test/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopServiceImplTest.java @@ -153,6 +153,87 @@ public void testCoefficientOfInbreeding_KnownSource() { assertThat(this.copService.coefficientOfInbreeding(p6.getGid(), BTypeEnum.SELF_FERTILIZING), is(31 / 32d)); } + /** + *
+	 *   AA  BB  CC  DD
+	 *     EE      FF
+	 *         GG
+	 *
+	 * gid1 gid2 cop
+	 * "AA" "AA" 1
+	 * "BB" "AA" 0
+	 * "BB" "BB" 1
+	 * "CC" "AA" 0
+	 * "CC" "BB" 0
+	 * "CC" "CC" 1
+	 * "DD" "AA" 0
+	 * "DD" "BB" 0
+	 * "DD" "CC" 0
+	 * "DD" "DD" 1
+	 * "EE" "AA" 0.5
+	 * "EE" "BB" 0.5
+	 * "EE" "CC" 0
+	 * "EE" "DD" 0
+	 * "EE" "EE" 0.5
+	 * "FF" "AA" 0
+	 * "FF" "BB" 0
+	 * "FF" "CC" 0.5
+	 * "FF" "DD" 0.5
+	 * "FF" "EE" 0
+	 * "FF" "FF" 0.5
+	 * "GG" "AA" 0.25
+	 * "GG" "BB" 0.25
+	 * "GG" "CC" 0.25
+	 * "GG" "DD" 0.25
+	 * "GG" "EE" 0.25
+	 * "GG" "FF" 0.25
+	 * "GG" "GG" 0.5
+	 * 
+ * + */ + @Test + public void testCase1() { + this.btype = BTypeEnum.SELF_FERTILIZING; + + final Germplasm aa = this.createGermplasm("AA", 0, 0, 0); + final Germplasm bb = this.createGermplasm("BB", 0, 0, 0); + final Germplasm cc = this.createGermplasm("CC", 0, 0, 0); + final Germplasm dd = this.createGermplasm("DD", 0, 0, 0); + final Germplasm ee = this.createGermplasm("EE", 2, aa.getGid(), bb.getGid()); + final Germplasm ff = this.createGermplasm("FF", 2, cc.getGid(), dd.getGid()); + final Germplasm gg = this.createGermplasm("GG", 2, ee.getGid(), ff.getGid()); + + assertThat(this.copService.coefficientOfParentage(aa.getGid(), aa.getGid(), this.btype), is(1d)); + assertThat(this.copService.coefficientOfParentage(bb.getGid(), aa.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(bb.getGid(), bb.getGid(), this.btype), is(1d)); + assertThat(this.copService.coefficientOfParentage(cc.getGid(), aa.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(cc.getGid(), bb.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(cc.getGid(), cc.getGid(), this.btype), is(1d)); + assertThat(this.copService.coefficientOfParentage(dd.getGid(), aa.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(dd.getGid(), bb.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(dd.getGid(), cc.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(dd.getGid(), dd.getGid(), this.btype), is(1d)); + assertThat(this.copService.coefficientOfParentage(ee.getGid(), aa.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(ee.getGid(), bb.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(ee.getGid(), cc.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(ee.getGid(), dd.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(ee.getGid(), ee.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), aa.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), bb.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), cc.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), dd.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), ee.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), ff.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), aa.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), bb.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), cc.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), dd.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), ee.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), ff.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), gg.getGid(), this.btype), is(1 / 2d)); + } + + private Germplasm createGermplasm(final String name, final int gnpgs, final int gpid1, final int gpid2) { final Name preferredName = new Name(); preferredName.setNval(name); From 5a344e6610538570aec73a4d06edf1a949e5d14d Mon Sep 17 00:00:00 2001 From: Nahuel Soldevilla Date: Tue, 12 Sep 2023 17:41:04 -0300 Subject: [PATCH 2/2] IB-2: Fix edge case where root nodes have number of progenitors = -1 --- .../pedigree/cop/CopCalculation.java | 8 +++- .../pedigree/cop/CopServiceImplTest.java | 44 ++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopCalculation.java b/src/main/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopCalculation.java index 1e89dd4522..a0a7b7d4b8 100644 --- a/src/main/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopCalculation.java +++ b/src/main/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopCalculation.java @@ -248,7 +248,7 @@ public double coefficientOfInbreeding(final GermplasmTreeNode g) { * D */ GermplasmTreeNode source = g.getMaleParentNode(); - while (!isUnknown(source) && isDerivative(source)) { + while (hasSourceParent(source)) { source = source.getMaleParentNode(); } /* @@ -388,7 +388,7 @@ private int countInbreedingGenerations(final GermplasmTreeNode g) { } int count = 1; - while (!isUnknown(source) && isDerivative(source) && !isUnknown(source.getMaleParentNode())) { + while (hasSourceParent(source)) { count++; source = source.getMaleParentNode(); } @@ -540,6 +540,10 @@ private static boolean isUnknown(final GermplasmTreeNode node) { return node == null || node.getGid() == UNKNOWN_GID; } + private static boolean hasSourceParent(final GermplasmTreeNode source) { + return !isUnknown(source) && isDerivative(source) && !isUnknown(source.getMaleParentNode()); + } + /** * * @param g1 diff --git a/src/test/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopServiceImplTest.java b/src/test/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopServiceImplTest.java index 500609e83e..faece176c6 100644 --- a/src/test/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopServiceImplTest.java +++ b/src/test/java/org/generationcp/middleware/api/germplasm/pedigree/cop/CopServiceImplTest.java @@ -192,7 +192,7 @@ public void testCoefficientOfInbreeding_KnownSource() { * */ @Test - public void testCase1() { + public void testCase_SelfFertilizingF2() { this.btype = BTypeEnum.SELF_FERTILIZING; final Germplasm aa = this.createGermplasm("AA", 0, 0, 0); @@ -233,6 +233,48 @@ public void testCase1() { assertThat(this.copService.coefficientOfParentage(gg.getGid(), gg.getGid(), this.btype), is(1 / 2d)); } + @Test + public void testCase_SelfFertilizingF2WithIncorrectProgenitorNumberForF0() { + this.btype = BTypeEnum.SELF_FERTILIZING; + + final Germplasm aa = this.createGermplasm("AA", -1, 0, 0); + final Germplasm bb = this.createGermplasm("BB", -1, 0, 0); + final Germplasm cc = this.createGermplasm("CC", -1, 0, 0); + final Germplasm dd = this.createGermplasm("DD", -1, 0, 0); + final Germplasm ee = this.createGermplasm("EE", 2, aa.getGid(), bb.getGid()); + final Germplasm ff = this.createGermplasm("FF", 2, cc.getGid(), dd.getGid()); + final Germplasm gg = this.createGermplasm("GG", 2, ee.getGid(), ff.getGid()); + + assertThat(this.copService.coefficientOfParentage(aa.getGid(), aa.getGid(), this.btype), is(1d)); + assertThat(this.copService.coefficientOfParentage(bb.getGid(), aa.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(bb.getGid(), bb.getGid(), this.btype), is(1d)); + assertThat(this.copService.coefficientOfParentage(cc.getGid(), aa.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(cc.getGid(), bb.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(cc.getGid(), cc.getGid(), this.btype), is(1d)); + assertThat(this.copService.coefficientOfParentage(dd.getGid(), aa.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(dd.getGid(), bb.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(dd.getGid(), cc.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(dd.getGid(), dd.getGid(), this.btype), is(1d)); + assertThat(this.copService.coefficientOfParentage(ee.getGid(), aa.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(ee.getGid(), bb.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(ee.getGid(), cc.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(ee.getGid(), dd.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(ee.getGid(), ee.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), aa.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), bb.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), cc.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), dd.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), ee.getGid(), this.btype), is(0d)); + assertThat(this.copService.coefficientOfParentage(ff.getGid(), ff.getGid(), this.btype), is(1 / 2d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), aa.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), bb.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), cc.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), dd.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), ee.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), ff.getGid(), this.btype), is(1 / 4d)); + assertThat(this.copService.coefficientOfParentage(gg.getGid(), gg.getGid(), this.btype), is(1 / 2d)); + } + private Germplasm createGermplasm(final String name, final int gnpgs, final int gpid1, final int gpid2) { final Name preferredName = new Name();