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

import uk.ac.ebi.quickgo.annotation.common.document.AnnotationDocument;

import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;

/**
 * For the contents of the co-occurring terms matrix calculate co-occurrence statistics.
 *
 * @author Tony Wardell
 * Date: 26/11/2015
 * Time: 11:59
 * Created with IntelliJ IDEA.
 *
 */
public class StatisticsCalculator implements ItemProcessor<String, List<CoTerm>> {

    //This is the count of all gene products for the term. We hold this figure separately as it is used many times.
    private Map<String, AtomicLong> termGPCount;

    //Holds a termN by termN matrix, each cell of which holds the count of gp this intersection of terms hold
    private Map<String, Map<String, AtomicLong>> coTermMatrix;

    //Total number of unique gene products that have annotations
    private long geneProductCount;
    private final CoTermsAggregator aggregator;

    public StatisticsCalculator(ItemWriter<AnnotationDocument> aggregator) {
        Preconditions.checkArgument(aggregator!=null, "The aggregator instance passed to the Statistics Calculator " +
                "constructor cannot be null");
        this.aggregator = (CoTermsAggregator)aggregator;
    }

    /**
     * Read each line in the term to term matrix for the selected term. For each calculate a CoTerm instance.
     *
     */
    public List<CoTerm> process(String goTerm) {
        return createCoTermsForSelectedTerm(goTerm).highestSimilarity();
    }

    public void initialize() {
        this.geneProductCount = aggregator.getTotalOfAnnotatedGeneProducts();
        this.termGPCount = aggregator.getGeneProductCounts();
        this.coTermMatrix = aggregator.getCoTerms();
    }

    /**
     * Create a CoTermsForSelectedTerm instance for each compared term.
     * @param goTerm The GO Term for which the co-occurrence statistics will be calculated.
     * @return CoTermsForSelectedTerm instance with co-occurring statistics calculated for every
     * co-occurring term.
     */
    private CoTermsForSelectedTerm createCoTermsForSelectedTerm(String goTerm) {

        Preconditions.checkArgument(null != goTerm, "Target GO term id passed to createCoTermsForSelectedTerm should not " +
                "be null");

        Map<String, AtomicLong> coTermsForTarget = coTermMatrix.get(goTerm);
//        CoTermsForSelectedTerm
        //                coTerms = new CoTermsForSelectedTerm(geneProductCount, termGPCount.get(goTerm).get());

        CoTermsForSelectedTerm.Builder coTermsBuilder =  new CoTermsForSelectedTerm.Builder()
                .setTotalNumberOfGeneProducts
        (geneProductCount).setSelected(termGPCount.get(goTerm).get());

        for (String comparedTerm : coTermsForTarget.keySet()) {

            coTermsBuilder.addCoTerm(new CoTerm.Builder().setTarget(goTerm).setComparedTerm
                    (comparedTerm)
                    .setCompared(termGPCount.get(comparedTerm).get())
                    .setTogether(coTermsForTarget.get(comparedTerm).get()).createCoTerm());
        }
        return coTermsBuilder.build();

    }

    /**
     *  Get list of compared terms, ordered by significance ratio descending
     * @param coTerms
     * @return
     */
    private List<CoTerm> resultsForOneGoTerm(CoTermsForSelectedTerm coTerms) {
        return coTerms.highestSimilarity();
    }

}

@@ -700,492 +700,139 @@
     
-//This is the count of all gene products for the term. We hold this figure separately as it is used many times.%0A    private Map%3CString, AtomicLong%3E termGPCount;%0A%0A    //Holds a termN by termN matrix, each cell of which holds the count of gp this intersection of terms hold%0A    private Map%3CString, Map%3CString, AtomicLong%3E%3E coTermMatrix;%0A%0A    //Total number of unique gene products that have annotations%0A    private long geneProductCount;%0A    private final CoTermsAggregator aggregator;%0A
+private final CoTermsAggregator aggregator;%0A%0A    /**%0A     *%0A     * @param aggregator holds the data for co-occurring terms.%0A     */
 %0A   
@@ -1154,81 +1154,77 @@
   * 
-Read each line in the term to term matrix for the selected term. For each
+For the passed in GO Term id, find the list of co-occurring terms and
  cal
@@ -1226,26 +1226,24 @@
 d calculate 
-a 
 CoTerm insta
@@ -1245,24 +1245,25 @@
 instance
+s
 .%0A     *
 %0A     */
@@ -1258,366 +1258,522 @@
    *
-%0A     */%0A    public List%3CCoTerm%3E process(String goTerm) %7B%0A        return createCoTermsForSelectedTerm(goTerm).highestSimilarity();%0A    %7D%0A%0A    public void initialize() %7B%0A        this.
+ @param goTerm the GO Term id for which co-occurring statistics will be calculated, co-occurring term list of%0A     * objects will be returned containing data and statistics.%0A     * @return a list of CoTerm objects. Each object represents a single permutation of two GO Terms that are%0A     * used to annotate the same 
 gene
-P
+ p
 roduct
-Count = aggregator.getTotalOfAnnotatedGeneProducts();%0A        this.termGPCount = aggregator.getGeneProductCounts();%0A        this.coTermMatrix = agg
+, and the statistics about that permutation.%0A     */%0A    @Override%0A    public List%3CCoTerm%3E process(String goTerm) %7B%0A        return c
 re
-g
 at
-or.getCoTerms
+eCoTermsForSelectedTerm(goTerm).highestSimilarity
 ();%0A
@@ -2373,159 +2373,31 @@
 t = 
-coTermMatrix.get(goTerm);%0A//        CoTermsForSelectedTerm%0A        //                coTerms = new CoTermsForSelectedTerm(geneProductCount, termGPCount
+aggregator.getCoTerms()
 .get
@@ -2400,33 +2400,25 @@
 .get(goTerm)
-.get());%0A
+;
 %0A        CoT
@@ -2549,19 +2549,40 @@
 ucts
-%0A        (g
+(aggregator.getTotalOfAnnotatedG
 eneP
@@ -2587,21 +2587,19 @@
 eProduct
-Count
+s()
 ).setSel
@@ -2604,27 +2604,49 @@
 elected(
-termGP
+aggregator.getGeneProduct
 Count
+s()
 .get(goT
@@ -2719,25 +2719,24 @@
 keySet()) %7B%0A
-%0A
             
@@ -2887,19 +2887,41 @@
 red(
-termGP
+aggregator.getGeneProduct
 Count
+s()
 .get
@@ -3094,267 +3094,6 @@
   %7D%0A
-%0A    /**%0A     *  Get list of compared terms, ordered by significance ratio descending%0A     * @param coTerms%0A     * @return%0A     */%0A    private List%3CCoTerm%3E resultsForOneGoTerm(CoTermsForSelectedTerm coTerms) %7B%0A        return coTerms.highestSimilarity();%0A    %7D%0A%0A
 %7D%0A

Remove the initialize method - use the aggregator instance directly for required data.