package uk.ac.ebi.quickgo.index.annotation.coterms;

import com.google.common.base.Preconditions;
import java.text.DecimalFormat;

/**
 * A co-occurrence statistics entity. Represents two goTerms which are being used to annotate the same gene product.
 * This class only holds the compared to term however, as instances of this class are held in a collection for the
 * selected (target) goTerm.
 * @author twardell
 *
 */
public class Co_occurringTerm {

    private final String target;
    private final String comparedTerm;
    private final long together;
    private final long compared;
    private float similarityRatio;
    private float probabilityRatio;

    /**
     * Create a permutation for the compared term.
     * @param target the id of the GO Term against which we will make co-occurring terms
     * @param comparedTerm term id for this statistics
     * @param compared count of gene products where compared term is annotated
     * @param together count of gene products where both selected and compared terms are annotated
     */
    public Co_occurringTerm(String target, String comparedTerm, long compared, long together) {
        this.target = target;
        this.comparedTerm = comparedTerm;
        this.compared = compared;
        this.together = together;
    }

    /**
     * Probability similarity ratio
     * Ratio of probability of both terms to probability of either term
     * <code>=#together/(#selected+#compared-#together)</code>
     * Probability of term here estimated as fraction of proteins annotated to term.
     * @param selected Total count of gene products annotated to selected term
     * @return the calculated similarity ratio
     */
    public float calculateProbabilitySimilarityRatio(float selected) {

        Preconditions
                .checkArgument(selected != 0, "CoOccurringTerm::calculateProbabilitySimilarityRatio The value for" +
                        " selected should not be zero");

        DecimalFormat twoDForm = new DecimalFormat("#.##");
        float similarityRatio = 100 * ((this.together) / (selected + this.compared - this.together));
        float psRatio = Float.valueOf(twoDForm.format(similarityRatio));// Round it with 2 decimals
        if (Math.round(psRatio) == 100) {
            this.similarityRatio = 100;
        }
        this.similarityRatio = psRatio;
        return this.similarityRatio;
    }

    /**
     * Probability ratio
     * Ratio of probability of compared term given selected term to probability of compared term
     * <code>=(#together/selected)/(#compared/#all)</code>
     * Probability of term here estimated as fraction of proteins annotated to term.
     * @param selected Total count of gene products annotated to selected term
     * @return the calculated probability ratio
     */
    public float calculateProbabilityRatio(float selected, float all) {

        Preconditions.checkArgument(selected != 0, "CoOccurringTerm::calculateProbabilityRatio The value for selected" +
                " should not be zero");
        Preconditions.checkArgument(all != 0, "CoOccurringTerm::calculateProbabilityRatio The value for all" +
                " should not be zero");

        DecimalFormat twoDForm = new DecimalFormat("#.##");
        probabilityRatio = (this.together / selected) / (this.compared / all);
        probabilityRatio = Float.valueOf(twoDForm.format(probabilityRatio));// Round it with 2 decimals
        return probabilityRatio;
    }

    /**
     * Ratio of probability of both terms to probability of either term
     * @return similarityRatio
     */
    public float getSimilarityRatio() {
        return similarityRatio;
    }

    /**
     * Ratio of probability of compared term given selected term to probability of compared term
     * @return probabilityRatio
     */
    public float getProbabilityRatio() {
        return probabilityRatio;
    }

    /**
     *
     * @return the term with which this co-occurring term co-occurs.
     */
    public String getTarget() {
        return target;
    }

    /**
     * The co-occurring term
     * @return the GOTerm used as the compared too term
     */
    public String getComparedTerm() {
        return comparedTerm;
    }

    /**
     * Count of gene products where both selected and compared terms are annotated
     * @return count
     */
    public long getTogether() {
        return together;
    }

    /**
     * Count of gene products where compared term is annotated
     * @return count
     */
    public long getCompared() {
        return compared;
    }

    @Override public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Co_occurringTerm that = (Co_occurringTerm) o;

        if (together != that.together) {
            return false;
        }
        if (compared != that.compared) {
            return false;
        }
        if (Float.compare(that.similarityRatio, similarityRatio) != 0) {
            return false;
        }
        if (Float.compare(that.probabilityRatio, probabilityRatio) != 0) {
            return false;
        }
        if (!target.equals(that.target)) {
            return false;
        }
        return comparedTerm.equals(that.comparedTerm);

    }

    @Override public int hashCode() {
        return target.hashCode();
    }
}

@@ -138,78 +138,83 @@
  A c
-o-occurrence statistics entity. Represents
+lass that represents the intersection between
  two 
-goT
+GO t
 erms
+,
  which 
-ar
+hav
 e be
-ing
+en
  use
@@ -230,16 +230,19 @@
 tate the
+%0A *
  same ge
@@ -255,67 +255,39 @@
 duct
-.%0A * This class only holds the compared to term however, as
+s in an annotation data set. An
  ins
@@ -291,17 +291,16 @@
 instance
-s
  of this
@@ -310,68 +310,138 @@
 ass 
-are held in a collection for the%0A * selected (target) goTerm
+will hold statistics related to this%0A * intersection.%0A * The data in this class is used by the QuickGO and Protein2GO applications
 .%0A *
@@ -814,53 +814,44 @@
 erm 
-against which we will make co-occurring t
+which is our compared 'from' GO T
 erm
-s
 %0A   
@@ -879,34 +879,63 @@
 rm t
+h
 e
-rm
  id 
-for this statistics
+of the GO Term which is our compared 'to' GO Term
 %0A   
@@ -954,25 +954,25 @@
 ompared 
-c
+C
 ount of 
 gene pro
@@ -959,36 +959,31 @@
 ed Count of 
-gene product
+protein
 s where comp
@@ -1028,25 +1028,25 @@
 ogether 
-c
+C
 ount of 
 gene pro
@@ -1033,36 +1033,31 @@
 er Count of 
-gene product
+protein
 s where both
@@ -1053,32 +1053,30 @@
  where both 
-selected
+target
  and compare
@@ -1695,55 +1695,8 @@
 erm%0A
-     * @return the calculated similarity ratio%0A
     
@@ -1702,37 +1702,36 @@
   */%0A    public 
-float
+void
  calculateProbab
@@ -1833,33 +1833,34 @@
 lected != 0, %22Co
-O
+_o
 ccurringTerm::ca
@@ -2366,45 +2366,8 @@
 io;%0A
-        return this.similarityRatio;%0A
     
@@ -2726,56 +2726,8 @@
 erm%0A
-     * @return the calculated probability ratio%0A
     
@@ -2849,33 +2849,34 @@
 lected != 0, %22Co
-O
+_o
 ccurringTerm::ca
@@ -3014,17 +3014,18 @@
 = 0, %22Co
-O
+_o
 ccurring

Improve javadoc. Change the statistics calculation methods so that they no longer return values - this isn't required and suggests these methods could be called repeatedly on this class.