Search in sources :

Example 1 with Iterators.peekingIterator

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);
    }
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) VcfRepository(org.molgenis.data.vcf.VcfRepository) EntityType(org.molgenis.data.meta.model.EntityType) MolgenisDataException(org.molgenis.data.MolgenisDataException) PeekingIterator(com.google.common.collect.PeekingIterator) Iterators.peekingIterator(com.google.common.collect.Iterators.peekingIterator) File.createTempFile(java.io.File.createTempFile)

Aggregations

Iterators.peekingIterator (com.google.common.collect.Iterators.peekingIterator)1 PeekingIterator (com.google.common.collect.PeekingIterator)1 File.createTempFile (java.io.File.createTempFile)1 Entity (org.molgenis.data.Entity)1 MolgenisDataException (org.molgenis.data.MolgenisDataException)1 EntityType (org.molgenis.data.meta.model.EntityType)1 DynamicEntity (org.molgenis.data.support.DynamicEntity)1 VcfRepository (org.molgenis.data.vcf.VcfRepository)1