/*******************************************************************************
* GenPlay, Einstein Genome Analyzer
* Copyright (C) 2009, 2014 Albert Einstein College of Medicine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* Authors: Julien Lajugie <julien.lajugie@einstein.yu.edu>
* Nicolas Fourel <nicolas.fourel@einstein.yu.edu>
* Eric Bouhassira <eric.bouhassira@einstein.yu.edu>
*
* Website: <http://genplay.einstein.yu.edu>
******************************************************************************/
package edu.yu.einstein.genplay.core.operation.geneList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import edu.yu.einstein.genplay.core.operation.Operation;
import edu.yu.einstein.genplay.core.operationPool.OperationPool;
import edu.yu.einstein.genplay.dataStructure.enums.Strand;
import edu.yu.einstein.genplay.dataStructure.gene.Gene;
import edu.yu.einstein.genplay.dataStructure.list.chromosomeWideList.geneListView.GeneListViewBuilder;
import edu.yu.einstein.genplay.dataStructure.list.genomeWideList.geneList.GeneList;
import edu.yu.einstein.genplay.dataStructure.list.genomeWideList.geneList.SimpleGeneList;
import edu.yu.einstein.genplay.dataStructure.list.listView.ListView;
import edu.yu.einstein.genplay.dataStructure.list.listView.ListViewBuilder;
/**
* Creates a new GeneList containing only the genes on the selected strand
* @author Julien Lajugie
*/
public class GLOFilterStrand implements Operation<GeneList> {
private final GeneList geneList; // input list
private final Strand strandToKeep; // strand with the genes we want to keep
private boolean stopped = false; // true if the operation must be stopped
/**
* Creates an instance of {@link GLOFilterStrand}
* @param geneList input list
* @param strandToKeep strand with the genes we want to keep
*/
public GLOFilterStrand(GeneList geneList, Strand strandToKeep) {
this.geneList = geneList;
this.strandToKeep = strandToKeep;
}
@Override
public GeneList compute() throws Exception {
final OperationPool op = OperationPool.getInstance();
final Collection<Callable<ListView<Gene>>> threadList = new ArrayList<Callable<ListView<Gene>>>();
for(short i = 0; i < geneList.size(); i++) {
final ListView<Gene> currentList = geneList.get(i);
Callable<ListView<Gene>> currentThread = new Callable<ListView<Gene>>() {
@Override
public ListView<Gene> call() throws Exception {
if (currentList == null) {
return null;
}
ListViewBuilder<Gene> resultLVBuilder = new GeneListViewBuilder();
for (int j = 0; (j < currentList.size()) && !stopped; j++) {
Gene currentGene = currentList.get(j);
if (currentGene.getStrand().equals(strandToKeep)) {
resultLVBuilder.addElementToBuild(currentGene);
}
}
// tell the operation pool that a chromosome is done
op.notifyDone();
return resultLVBuilder.getListView();
}
};
threadList.add(currentThread);
}
List<ListView<Gene>> result = op.startPool(threadList);
if (result == null) {
return null;
} else {
return new SimpleGeneList(result, geneList.getGeneScoreType(), geneList.getGeneDBURL());
}
}
@Override
public String getDescription() {
return "Operation: Filter Strand, only genes on \""+ strandToKeep + "\" strand kept";
}
@Override
public String getProcessingDescription() {
return "Filtering Genes";
}
@Override
public int getStepCount() {
return 1;
}
@Override
public void stop() {
stopped = true;
}
}