package uk.ac.ebi.atlas.profiles.baseline;

import com.esotericsoftware.kryo.io.UnsafeInput;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import uk.ac.ebi.atlas.commons.streams.ObjectInputStream;
import uk.ac.ebi.atlas.model.experiment.baseline.BaselineExperiment;
import uk.ac.ebi.atlas.model.experiment.baseline.BaselineExpression;
import uk.ac.ebi.atlas.model.experiment.baseline.BaselineProfile;
import uk.ac.ebi.atlas.model.resource.AtlasResource;
import uk.ac.ebi.atlas.profiles.BaselineExpressionsKryoReader;
import uk.ac.ebi.atlas.profiles.BaselineProfileKryoInputStream;
import uk.ac.ebi.atlas.profiles.tsv.ExpressionsRowDeserializer;
import uk.ac.ebi.atlas.profiles.tsv.ExpressionsRowDeserializerBuilder;
import uk.ac.ebi.atlas.resource.DataFileHub;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class RnaSeqBaselineProfileStreamFactory extends BaselineProfileStreamFactory {

    @Inject
    RnaSeqBaselineProfileStreamFactory(DataFileHub dataFileHub) {
        super(dataFileHub);
    }

    @Override
    protected ExpressionsRowDeserializerBuilder<BaselineExpression> getExpressionsRowDeserializerBuilder(BaselineExperiment experiment) {
        return new RnaSeqBaselineExpressionsRowDeserializerBuilder(experiment);
    }

    @Override
    public ObjectInputStream<BaselineProfile> create(BaselineExperiment experiment, BaselineProfileStreamOptions
            baselineProfileStreamOptions){
        AtlasResource<UnsafeInput> kryoFile = dataFileHub.getKryoFileForReading(experiment.getAccession());
        if(kryoFile.exists()){
            return new BaselineProfileKryoInputStream(
                    BaselineExpressionsKryoReader.create(kryoFile), experiment, filterExpressions(experiment,
                    baselineProfileStreamOptions));
        } else {
            return super.create(experiment, baselineProfileStreamOptions);
        }
    }


    static class RnaSeqBaselineExpressionsRowDeserializerBuilder implements ExpressionsRowDeserializerBuilder<BaselineExpression> {

        protected final BaselineExperiment baselineExperiment;

        public RnaSeqBaselineExpressionsRowDeserializerBuilder(BaselineExperiment baselineExperiment) {
            this.baselineExperiment = baselineExperiment;
        }


        @Override
        public ExpressionsRowDeserializer<BaselineExpression> build(String... tsvFileHeaders) {

            return new ExpressionsRowTsvDeserializerBaseline(baselineExperiment.getDataColumnDescriptors());
        }

    }
}

@@ -90,89 +90,8 @@
 ut;%0A
-import com.google.common.base.Optional;%0Aimport com.google.common.base.Predicate;%0A
 impo
@@ -1054,16 +1054,20 @@
 ression%3E
+%0A   
  getExpr
@@ -1121,32 +1121,32 @@
 t experiment) %7B%0A
-
         return n
@@ -1313,16 +1313,69 @@
 eriment,
+%0A                                                    
  Baselin
@@ -1395,28 +1395,16 @@
 mOptions
-%0A           
  baselin
@@ -1425,16 +1425,17 @@
 Options)
+ 
 %7B%0A      
@@ -1546,16 +1546,17 @@
       if
+ 
 (kryoFil
@@ -1566,16 +1566,17 @@
 xists())
+ 
 %7B%0A      
@@ -1703,16 +1703,36 @@
 eriment,
+%0A                   
  filterE
@@ -1753,36 +1753,16 @@
 eriment,
-%0A                   
  baselin
@@ -1892,24 +1892,24 @@
   %7D%0A    %7D%0A%0A%0A
-
     static c
@@ -1960,16 +1960,28 @@
 rBuilder
+%0A           
  impleme

Remove stale imports, reformat code