Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 16 additions & 78 deletions gemma-cli/src/main/java/ubic/gemma/apps/SplitExperimentCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@
package ubic.gemma.apps;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import ubic.gemma.cli.util.EntityLocator;
import ubic.gemma.core.analysis.preprocess.SplitExperimentService;
import ubic.gemma.model.analysis.expression.ExpressionExperimentSet;
import ubic.gemma.model.expression.experiment.ExperimentFactorUtils;
import ubic.gemma.model.expression.experiment.ExperimentalFactor;
import ubic.gemma.model.expression.experiment.ExperimentalFactorValueObject;
import ubic.gemma.model.expression.experiment.ExpressionExperiment;
import ubic.gemma.persistence.service.expression.experiment.ExperimentalFactorService;

import java.util.Collection;

/**
* Split an experiment into parts based on an experimental factor
Expand All @@ -41,18 +36,18 @@
*/
public class SplitExperimentCli extends ExpressionExperimentManipulatingCLI {

/**
*
*/
private static final String FACTOR_OPTION = "factor";
private static final String FACTOR_OPTION = "factor",
SKIP_POST_PROCESSING_OPTION = "nopost",
DELETE_ORIGINAL_EXPERIMENT_OPTION = "deleteOriginalExperiment";

@Autowired
private SplitExperimentService serv;
@Autowired
private ExperimentalFactorService efs;
private EntityLocator entityLocator;

private Long factorId;
private String factorName;
private String factorIdentifier;
private boolean skipPostProcessing;
private boolean deleteOriginalExperiment;

public SplitExperimentCli() {
super();
Expand All @@ -71,80 +66,23 @@ public String getShortDesc() {

@Override
protected void buildExperimentOptions( Options options ) {
options.addOption( Option.builder( FACTOR_OPTION ).hasArg()
.desc( "ID numbers, categories or names of the factor to use, with spaces replaced by underscores (must not be 'batch')" )
.build() );
options.addRequiredOption( FACTOR_OPTION, "factor", true, "ID numbers, categories or names of the factor to use, with spaces replaced by underscores (must not be 'batch')" );
options.addOption( SKIP_POST_PROCESSING_OPTION, "no-post-processing", false, "Skip post-processing of resulting splits if applicable." );
options.addOption( DELETE_ORIGINAL_EXPERIMENT_OPTION, "delete-original-experiment", false, "Delete the original experiment once the split succeeds." );
}

@Override
protected void processExperimentOptions( CommandLine commandLine ) throws ParseException {
if ( !commandLine.hasOption( FACTOR_OPTION ) ) {
throw new IllegalArgumentException( "Please specify the factor" );
}
String rawFactor = commandLine.getOptionValue( FACTOR_OPTION );
try {
this.factorId = Long.parseLong( rawFactor );
} catch ( NumberFormatException e ) {
this.factorName = rawFactor;
}
factorIdentifier = commandLine.getOptionValue( FACTOR_OPTION );
skipPostProcessing = commandLine.hasOption( SKIP_POST_PROCESSING_OPTION );
deleteOriginalExperiment = commandLine.hasOption( DELETE_ORIGINAL_EXPERIMENT_OPTION );
}

@Override
protected void processExpressionExperiment( ExpressionExperiment ee ) {
ee = this.eeService.thawLite( ee );
ExperimentalFactor splitOn = this.guessFactor( ee );
ExpressionExperimentSet eeSet = serv.split( ee, splitOn, true );
ExperimentalFactor splitOn = entityLocator.locateExperimentalFactor( ee, factorIdentifier );
ExpressionExperimentSet eeSet = serv.split( ee, splitOn, !skipPostProcessing, deleteOriginalExperiment );
addSuccessObject( ee, "Experiment was split on " + splitOn + " into " + eeSet.getExperiments().size() + " parts." );
}

/**
* Adapted from code in DifferentialExpressionAnalysisCli
*/
private ExperimentalFactor guessFactor( ExpressionExperiment ee ) {
if ( ee.getExperimentalDesign() == null ) {
throw new IllegalStateException( ee + " does not have an experimental design, it cannot be split on a factor." );
}

if ( this.factorName != null ) {

Collection<ExperimentalFactor> experimentalFactors = ee.getExperimentalDesign().getExperimentalFactors();
for ( ExperimentalFactor experimentalFactor : experimentalFactors ) {

// has already implemented way of figuring out human-friendly name of factor value.
ExperimentalFactorValueObject fvo = new ExperimentalFactorValueObject( experimentalFactor );

// do not attempt to switch on 'batch'
if ( ExperimentFactorUtils.isBatchFactor( experimentalFactor ) ) {
continue;
}

if ( factorName.contains( experimentalFactor.getName().replaceAll( " ", "_" ) ) ) {
return experimentalFactor;
} else if ( fvo.getCategory() != null && factorName
.contains( fvo.getCategory().replaceAll( " ", "_" ) ) ) {
return experimentalFactor;
}
}

throw new IllegalArgumentException( "Didn't find factor the provided factor name " );

}

ExperimentalFactor factor = efs.loadOrFail( factorId );
factor = efs.thaw( factor );
if ( factor == null ) {
throw new IllegalArgumentException( "No factor for id=" + factorId );
}
if ( !factor.getExperimentalDesign().equals( ee.getExperimentalDesign() ) ) {
throw new IllegalArgumentException( "Factor with id=" + factorId + " does not belong to " + ee );
}

if ( ExperimentFactorUtils.isBatchFactor( factor ) ) {
throw new IllegalArgumentException( "Selected factor looks like batch, split not allowed, choose another factor instead" );
}

return factor;

}

}
Loading