Page 11
Problem 1.1 How many different genotypes are there at a locus with n alleles that differ by state? You already know that there is one genotype at a locus with one allele and three genotypes at a locus with two alleles. Continue this with three, four, and more alleles until you divine the general case.
Note:
Take n=4 as an example, for homozygote, there are four (n) genotypes:
Namely, there are n homozygotes. For heterozygote, there are (we see Aij and Aji are the same) three (n-1) heterozygotes start with 1 (A12, A13, A14), two (n-2) heterozygotes start with 2 (A23, A24) and one heterozygote starts with 3 (A34).
So for the general case there are altogether n + (n-1) + (n-2) + … + 1 genotypes with n alleles that differ by state. Namely n(n+1)/2.
Use Java Stream to print all genotypes:
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class PrintGenotypes{
public static void main(String[] args){
int n = 4;
Stream<int[]> genotypes =
IntStream.rangeClosed(1, n).boxed()
.flatMap(i ->
IntStream.rangeClosed(i, n)
.filter(j -> j>=i)
.mapToObj(j ->
new int[]{i, j})
);
genotypes
.forEach(t ->
System.out.println("A" + t[0] + "" + t[1] ));
}
}
Output:
A11
A12
A13
A14
A22
A23
A24
A33
A34
A44