Hello everyone. I have some problems in implementing some stuffs about genetics in java. Here's the scenario.

A human cell contains 23 pairs of chromosomes. Each chromosome in each pair has thousands of genes. Each copy of a gene is called an allele. These alleles determine the different traits of a human(eye color, height, hair color, genetic diseases etc). An allele can either be dominant or recessive.

A dominant allele is always expressed in the genome of its bearer. However, if information from one allele is not expressed when a dominant allele of the same gene is present, it is a recessive allele. The peculiarity of the recessive allele of a gene is that it can be present in the genome and transmitted over several generations without it is expressed in the phenotype its bearers. If there is no dominant allele, two copies of the gene have the same recessive allele (homozygous recessive) then the recessive character is expressed.

So now, on a pair of chromosome of the cell of a child, one chromosome comes from the mother and the other from the father. So lets say on position 1 of the father chromosome contains an allele (either dominant or recessive) of the gene responsible for eye color, on the same position of the mother chromosome must contain another allele (either dominant or recessive) of the same gene. So how can i represent these classes allele, gene correctly? Here's what i have so far

Allele.java

public class Allele {

    private String trait;
    private int type;

    public Allele(String trait, int type) {
        this.trait = trait;
        this.type = type;
    }

    public boolean equals(Allele a){
        if(this.getTrait().equals(a.getTrait()) && this.getType()==a.getType())
            return true;

        return false;
    }

    /**
     * @return the trait
     */
    public String getTrait() {
        return trait;
    }

    /**
     * @param trait the trait to set
     */
    public void setTrait(String trait) {
        this.trait = trait;
    }

    /**
     * @param type the type to set
     */
    public void setType(int type) {
        this.type = type;
    }

    /**
     * @return the type either recessive(0) or dominant(1)
     */
    public int getType() {
        return type;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Allele [trait=" + trait + ", type=" +
        (this.getType()==0?"recessive":"dominant")+ "]";
    }
}

Gene.java

public class Gene implements Cloneable {

    private Allele allele;

    public Gene(){
        super();
    }

    public Gene(Allele allele){
        super();
        this.allele = allele;
    }

    /**
     * Randomly selects a trait from trait1 or trait2 and returns a new Gene with that trait
     * @param trait1
     * @param trait2
     * 
     * @return a new Gene
     */
    public Gene randomAllele(Allele trait1, Allele trait2){
        Allele allele = null;
        Random rand = new Random();
        int i = rand.nextInt(2);// generate between 0 and 2: only 2 possibilities: 0 or 1

        switch(i){
        case 0:
            allele = trait1;
            break;

        case 1:
            allele = trait2;
            break;
        }

        return new Gene(allele);
    }

    /**
     * Clones a gene
     */
    public Gene clone() throws CloneNotSupportedException{
        Gene g;
        g = (Gene) super.clone();
        return g;
    }

    public boolean equals(Gene g){
        if(this.getAllele().equals(g.getAllele()) && 
                this.getAlleleType() == g.getAlleleType() )
            return true;
        return false;
    }

    /**
     * @param allele the allele to set
     */
    public void setAllele(Allele allele) {
        this.allele = allele;
    }

    /**
     * 
     * @return - the allele 
     */
    public Allele getAllele() {
        return allele;
    }

    /**
     * 
     * @return the trait of the gene or allele
     */
    public String getAlleleTrait(){
        return allele.getTrait();
    }

    /**
     * 
     * @return allele type. Either recessive or dominant
     */
    public int getAlleleType(){
        return allele.getType();
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Gene [" + allele +"]";
    }
}

How can I also represent the class chromosome? I'm thinking about using an arraylist of gene types. But the problem using this is that I cannot define for example the 1st position of the array should be used for eye color, the second position for height etc. Any suggestions please? Thanks

My first thought is to use an implementation of the Map interface, such as Hashtable or HashMap to store your data. The key to the map would be a String for the particular trait such as eye color, and the value would be the corresponding chromosome.

Give this a try and see if it solves your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.