/*
 * The Gemma project
 * 
 * Copyright (c) 2012 University of British Columbia
 * 
 * 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.
 *
 */

package ubic.gemma.apps;

import java.util.Collection;

import org.apache.commons.lang.StringUtils;

import ubic.gemma.loader.expression.geo.DataUpdater;
import ubic.gemma.model.expression.arrayDesign.ArrayDesign;
import ubic.gemma.model.expression.arrayDesign.TechnologyType;
import ubic.gemma.model.expression.experiment.BioAssaySet;
import ubic.gemma.model.expression.experiment.ExpressionExperiment;

/**
 * Add (or possibly replace) the data associated with an experiment. Cases include Affymetrix exon arrays, RNA-seq data.
 * 
 * @author paul
 * @version $Id$
 */
public class DataUpdaterCli extends ExpressionExperimentManipulatingCLI {

    private String aptFile = null;

    @Override
    protected void buildOptions() {
        super.buildOptions();
        super.addOption( "aptFile", true,
                "File output from apt-probeset-summarize; use if you want to override usual GEO download behaviour" );
    }

    @Override
    protected void processOptions() {
        super.processOptions();
        if ( hasOption( "aptfile" ) ) {
            this.aptFile = getOptionValue( "aptfile" );
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see ubic.gemma.util.AbstractCLI#doWork(java.lang.String[])
     */
    @Override
    protected Exception doWork( String[] args ) {
        super.processCommandLine( "DataUpdater", args );

        DataUpdater serv = getBean( DataUpdater.class );

        if ( StringUtils.isNotBlank( aptFile ) ) {
            if ( this.expressionExperiments.size() > 1 ) {

                throw new IllegalArgumentException( "Can't use -aptfile unless you are doing just one experiment" );
            }
            BioAssaySet ee = this.expressionExperiments.iterator().next();
            ExpressionExperiment thawedEe = this.eeService.thawLite( ( ExpressionExperiment ) ee );

            Collection<ArrayDesign> arrayDesignsUsed = this.eeService.getArrayDesignsUsed( ee );

            if ( arrayDesignsUsed.size() > 1 ) {
                throw new IllegalArgumentException( "Cannot update data for experiment that uses multiple platforms" );
            }

            ArrayDesign ad = arrayDesignsUsed.iterator().next();
            try {
                if ( ad.getName().toLowerCase().contains( "exon" ) ) {
                    serv.addAffyExonArrayData( thawedEe, aptFile );
                    this.successObjects.add( thawedEe.toString() );
                } else {
                    throw new IllegalArgumentException( "Option -aptfile only valid if you are using an exon array." );
                }
            } catch ( Exception e ) {
                this.errorObjects.add( ee + " " + e.getLocalizedMessage() );
            }
        }

        for ( BioAssaySet ee : this.expressionExperiments ) {
            try {
                ExpressionExperiment thawedEe = this.eeService.thawLite( ( ExpressionExperiment ) ee );

                Collection<ArrayDesign> arrayDesignsUsed = this.eeService.getArrayDesignsUsed( ee );

                if ( arrayDesignsUsed.size() > 1 ) {
                    log.warn( "Cannot update data for experiment that uses multiple platforms" );
                    continue;
                }

                ArrayDesign ad = arrayDesignsUsed.iterator().next();

                if ( ad.getName().toLowerCase().contains( "exon" )
                        && ad.getTechnologyType().equals( TechnologyType.ONECOLOR ) ) {
                    log.info( thawedEe + " looks like affy exon array" );
                    serv.addAffyExonArrayData( thawedEe );
                    this.successObjects.add( thawedEe.toString() );
                } else {
                    log.warn( thawedEe + ": Don't know how to add data to this yet" );
                    this.errorObjects.add( thawedEe + ": Don't know how to add data to this yet" );
                }
            } catch ( Exception e ) {
                this.errorObjects.add( ee + " " + e.getLocalizedMessage() );
            }
        }

        super.summarizeProcessing();

        return null;
    }

    /**
     * @param args
     */
    public static void main( String[] args ) {
        DataUpdaterCli c = new DataUpdaterCli();
        c.doWork( args );
    }

}

@@ -1285,24 +1285,82 @@
 atingCLI %7B%0A%0A
+    private static final String APT_FILE_OPT = %22aptFile%22;%0A
     private 
@@ -1382,16 +1382,16 @@
 = null;%0A
-
 %0A    @Ov
@@ -1488,25 +1488,28 @@
 Option( 
-%22aptFile%22
+APT_FILE_OPT
 , true,%0A
@@ -1742,25 +1742,28 @@
 Option( 
-%22aptfile%22
+APT_FILE_OPT
  ) ) %7B%0A 
@@ -1808,17 +1808,20 @@
 ue( 
-%22aptfile%22
+APT_FILE_OPT
  );%0A
@@ -3400,33 +3400,84 @@
  );%0A            
-%7D
+    return e;%0A            %7D%0A            return null;
 %0A        %7D%0A%0A    

fix option