/*
 * Copyright 2008-2013 Microarray Informatics Team, EMBL-European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

package uk.ac.ebi.atlas.model.differential;

import uk.ac.ebi.atlas.model.GeneProfilesList;

import java.util.Collection;

import static java.lang.Math.max;
import static java.lang.Math.min;

public class DifferentialProfilesList<T extends DifferentialProfile> extends GeneProfilesList<T> implements DifferentialExpressionLimits {

    public DifferentialProfilesList() {
        // default, with no starting collection
    }

    public DifferentialProfilesList(Collection<T> collection) {
        super(collection);
    }

    public double getMaxUpRegulatedExpressionLevel(){
        double maxUpRegulatedExpressionLevel = -Double.MAX_VALUE;
        for (DifferentialProfile differentialProfile : this) {
            double expressionLevel = differentialProfile.getMaxUpRegulatedExpressionLevel();
            if (!Double.isNaN(expressionLevel)) {
                maxUpRegulatedExpressionLevel = max(maxUpRegulatedExpressionLevel, expressionLevel);
            }
        }
        return maxUpRegulatedExpressionLevel == -Double.MAX_VALUE ? Double.NaN : maxUpRegulatedExpressionLevel;
    }

    public double getMinUpRegulatedExpressionLevel(){
        Double minUpRegulatedExpressionLevel = null;
        for (DifferentialProfile differentialProfile : this) {
            double expressionLevel = differentialProfile.getMinUpRegulatedExpressionLevel();
            if (!Double.isNaN(expressionLevel)) {
                minUpRegulatedExpressionLevel = (minUpRegulatedExpressionLevel == null) ? expressionLevel : min(minUpRegulatedExpressionLevel, expressionLevel);
            }
        }
        return minUpRegulatedExpressionLevel == Double.MAX_VALUE ? Double.NaN : minUpRegulatedExpressionLevel;
    }


    public double getMaxDownRegulatedExpressionLevel(){
        double maxDownRegulatedExpressionLevel = -Double.MAX_VALUE;
        for (DifferentialProfile differentialProfile : this) {
            double expressionLevel = differentialProfile.getMaxDownRegulatedExpressionLevel();
            if (!Double.isNaN(expressionLevel)) {
                maxDownRegulatedExpressionLevel = max(maxDownRegulatedExpressionLevel, Math.abs(expressionLevel));
            }
        }
        return maxDownRegulatedExpressionLevel == -Double.MAX_VALUE ? Double.NaN : negate(maxDownRegulatedExpressionLevel);
    }

    public double getMinDownRegulatedExpressionLevel(){
        Double minDownRegulatedExpressionLevel = null;
        for (DifferentialProfile differentialProfile : this) {
            double expressionLevel = differentialProfile.getMinDownRegulatedExpressionLevel();
            if (!Double.isNaN(expressionLevel)) {
                minDownRegulatedExpressionLevel = (minDownRegulatedExpressionLevel == null) ? Math.abs(expressionLevel) : min(minDownRegulatedExpressionLevel, Math.abs(expressionLevel));
            }
        }
        return minDownRegulatedExpressionLevel == null ? Double.NaN : negate(minDownRegulatedExpressionLevel);
    }

    public static double negate(double value) {
        return value == 0 ? 0 : -value;
    }
}

@@ -2174,62 +2174,8 @@
 ();%0A
-            if (!Double.isNaN(expressionLevel)) %7B%0A    
     
@@ -2323,38 +2323,24 @@
 sionLevel);%0A
-            %7D%0A
         %7D%0A  
@@ -3331,62 +3331,8 @@
 ();%0A
-            if (!Double.isNaN(expressionLevel)) %7B%0A    
     
@@ -3506,38 +3506,24 @@
 ionLevel));%0A
-            %7D%0A
         %7D%0A  

Gene query "AT1G07160" for experiment E-GEOD-11758