use of com.google.common.collect.Iterators.peekingIterator in project molgenis by molgenis.
the class SnpEffRunner method getSnpEffects.
@SuppressWarnings("resource")
public Iterator<Entity> getSnpEffects(Iterator<Entity> source, final File inputVcf) {
try {
if (!source.hasNext())
return Collections.<Entity>emptyList().iterator();
// get meta data by peeking at the first entity (work-around for issue #4701)
PeekingIterator<Entity> peekingSourceIterator = Iterators.peekingIterator(source);
EntityType sourceEMD = peekingSourceIterator.peek().getEntityType();
List<String> params = Arrays.asList("-Xmx2g", getSnpEffPath(), "hg19", "-noStats", "-noLog", "-lof", "-canon", "-ud", "0", "-spliceSiteSize", "5");
File outputVcf = jarRunner.runJar(NAME, params, inputVcf);
VcfRepository repo = new VcfRepository(outputVcf, "SNPEFF_OUTPUT_VCF_" + inputVcf.getName(), vcfAttributes, entityTypeFactory, attributeFactory);
PeekingIterator<Entity> snpEffResultIterator = peekingIterator(repo.iterator());
return new Iterator<Entity>() {
final LinkedList<Entity> effects = Lists.newLinkedList();
@Override
public boolean hasNext() {
return (peekingSourceIterator.hasNext() || !effects.isEmpty());
}
@Override
public Entity next() {
if (effects.isEmpty()) {
// go to next source entity and get effects
Entity sourceEntity = peekingSourceIterator.next();
String chromosome = sourceEntity.getString(VcfAttributes.CHROM);
Integer position = sourceEntity.getInt(VcfAttributes.POS);
if (chromosome != null && position != null) {
Entity snpEffEntity = getSnpEffEntity(snpEffResultIterator, chromosome, position);
if (snpEffEntity != null) {
effects.addAll(getSnpEffectsFromSnpEffEntity(sourceEntity, snpEffEntity, getTargetEntityType(sourceEMD)));
} else {
effects.add(getEmptyEffectsEntity(sourceEntity, getTargetEntityType(sourceEMD)));
}
} else {
effects.add(getEmptyEffectsEntity(sourceEntity, getTargetEntityType(sourceEMD)));
}
}
return effects.removeFirst();
}
};
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (InterruptedException e) {
throw new MolgenisDataException("Exception running SnpEff", e);
}
}
Aggregations