Example usage for org.apache.commons.lang ArrayUtils addAll

List of usage examples for org.apache.commons.lang ArrayUtils addAll

Introduction

In this page you can find the example usage for org.apache.commons.lang ArrayUtils addAll.

Prototype

public static double[] addAll(double[] array1, double[] array2) 

Source Link

Document

Adds all the elements of the given arrays into a new array.

Usage

From source file:org.broadinstitute.gatk.tools.walkers.haplotypecaller.graphs.BaseGraph.java

/**
 * Walk along the reference path in the graph and pull out the corresponding bases
 * @param fromVertex    starting vertex//from   www.  j  a va  2s . com
 * @param toVertex      ending vertex
 * @param includeStart  should the starting vertex be included in the path
 * @param includeStop   should the ending vertex be included in the path
 * @return              byte[] array holding the reference bases, this can be null if there are no nodes between the starting and ending vertex (insertions for example)
 */
public byte[] getReferenceBytes(final V fromVertex, final V toVertex, final boolean includeStart,
        final boolean includeStop) {
    if (fromVertex == null) {
        throw new IllegalArgumentException("Starting vertex in requested path cannot be null.");
    }
    if (toVertex == null) {
        throw new IllegalArgumentException("From vertex in requested path cannot be null.");
    }

    byte[] bytes = null;
    V v = fromVertex;
    if (includeStart) {
        bytes = ArrayUtils.addAll(bytes, getAdditionalSequence(v));
    }
    v = getNextReferenceVertex(v); // advance along the reference path
    while (v != null && !v.equals(toVertex)) {
        bytes = ArrayUtils.addAll(bytes, getAdditionalSequence(v));
        v = getNextReferenceVertex(v); // advance along the reference path
    }
    if (includeStop && v != null && v.equals(toVertex)) {
        bytes = ArrayUtils.addAll(bytes, getAdditionalSequence(v));
    }
    return bytes;
}

From source file:org.broadinstitute.gatk.tools.walkers.haplotypecaller.graphs.Path.java

/**
 * The base sequence for this path. Pull the full sequence for source nodes and then the suffix for all subsequent nodes
 * @return  non-null sequence of bases corresponding to this path
 *//*from w  ww . j a va2s.c o m*/
@Ensures({ "result != null" })
public byte[] getBases() {
    if (getEdges().isEmpty()) {
        return graph.getAdditionalSequence(lastVertex);
    }

    byte[] bases = graph.getAdditionalSequence(graph.getEdgeSource(edgesInOrder.get(0)));
    for (final E e : edgesInOrder) {
        bases = ArrayUtils.addAll(bases, graph.getAdditionalSequence(graph.getEdgeTarget(e)));
    }
    return bases;
}

From source file:org.broadinstitute.gatk.tools.walkers.haplotypecaller.graphs.SharedSequenceMerger.java

/**
 * Attempt to merge the incoming vertices of v
 *
 * @param graph the graph containing the vertex v
 * @param v the vertex whose incoming vertices we want to merge
 * @return true if some useful merging was done, false otherwise
 *//* w w  w.  jav a 2s .c o m*/
public boolean merge(final SeqGraph graph, final SeqVertex v) {
    if (graph == null)
        throw new IllegalArgumentException("graph cannot be null");
    if (!graph.vertexSet().contains(v))
        throw new IllegalArgumentException("graph doesn't contain vertex " + v);

    final Set<SeqVertex> prevs = graph.incomingVerticesOf(v);
    if (!canMerge(graph, v, prevs))
        return false;
    else {
        //            graph.printGraph(new File("csm." + counter + "." + v.getSequenceString() + "_pre.dot"), 0);

        final List<BaseEdge> edgesToRemove = new LinkedList<>();
        final byte[] prevSeq = prevs.iterator().next().getSequence();
        final SeqVertex newV = new SeqVertex(ArrayUtils.addAll(prevSeq, v.getSequence()));
        graph.addVertex(newV);

        for (final SeqVertex prev : prevs) {
            for (final BaseEdge prevIn : graph.incomingEdgesOf(prev)) {
                graph.addEdge(graph.getEdgeSource(prevIn), newV, prevIn.copy());
                edgesToRemove.add(prevIn);
            }
        }

        for (final BaseEdge e : graph.outgoingEdgesOf(v)) {
            graph.addEdge(newV, graph.getEdgeTarget(e), e.copy());
        }

        graph.removeAllVertices(prevs);
        graph.removeVertex(v);
        graph.removeAllEdges(edgesToRemove);

        //            graph.printGraph(new File("csm." + counter++ + "." + v.getSequenceString() + "_post.dot"), 0);

        return true;
    }
}

From source file:org.broadinstitute.gatk.tools.walkers.haplotypecaller.LDMerger.java

protected VariantContext createMergedVariantContext(final VariantContext thisVC, final VariantContext nextVC,
        final byte[] ref, final GenomeLoc refLoc) {
    final int thisStart = thisVC.getStart();
    final int nextStart = nextVC.getStart();
    byte[] refBases = new byte[] {};
    byte[] altBases = new byte[] {};
    refBases = ArrayUtils.addAll(refBases, thisVC.getReference().getBases());
    altBases = ArrayUtils.addAll(altBases, thisVC.getAlternateAllele(0).getBases());
    int locus;//w  ww. j  a v a  2s.c o  m
    for (locus = thisStart + refBases.length; locus < nextStart; locus++) {
        final byte refByte = ref[locus - refLoc.getStart()];
        refBases = ArrayUtils.add(refBases, refByte);
        altBases = ArrayUtils.add(altBases, refByte);
    }
    refBases = ArrayUtils.addAll(refBases, ArrayUtils.subarray(nextVC.getReference().getBases(),
            locus > nextStart ? 1 : 0, nextVC.getReference().getBases().length)); // special case of deletion including the padding base of consecutive indel
    altBases = ArrayUtils.addAll(altBases, nextVC.getAlternateAllele(0).getBases());

    int iii = 0;
    if (refBases.length == altBases.length) { // insertion + deletion of same length creates an MNP --> trim common prefix bases off the beginning of the allele
        while (iii < refBases.length && refBases[iii] == altBases[iii]) {
            iii++;
        }
        if (iii == refBases.length) {
            // we've become a null allele, such as with CA/C + A/AA -> CA/CA => after trimming there's nothing left
            // so return a null variant context so we can eliminate the variants from consideration
            return null;
        }
    }

    final Allele refAllele = Allele.create(ArrayUtils.subarray(refBases, iii, refBases.length), true);
    final Allele altAllele = Allele.create(ArrayUtils.subarray(altBases, iii, altBases.length), false);
    return new VariantContextBuilder("merged", thisVC.getChr(), thisVC.getStart() + iii, nextVC.getEnd(),
            Arrays.asList(refAllele, altAllele)).make();
}

From source file:org.broadinstitute.gatk.utils.haplotype.EventMap.java

protected void processCigarForInitialEvents() {
    final Cigar cigar = haplotype.getCigar();
    final byte[] alignment = haplotype.getBases();

    int refPos = haplotype.getAlignmentStartHapwrtRef();
    if (refPos < 0) {
        return;//from w w w  . ja va2 s . co m
    } // Protection against SW failures

    final List<VariantContext> proposedEvents = new ArrayList<>();

    int alignmentPos = 0;

    for (int cigarIndex = 0; cigarIndex < cigar.numCigarElements(); cigarIndex++) {
        final CigarElement ce = cigar.getCigarElement(cigarIndex);
        final int elementLength = ce.getLength();
        switch (ce.getOperator()) {
        case I: {
            if (refPos > 0) { // protect against trying to create insertions/deletions at the beginning of a contig
                final List<Allele> insertionAlleles = new ArrayList<Allele>();
                final int insertionStart = refLoc.getStart() + refPos - 1;
                final byte refByte = ref[refPos - 1];
                if (BaseUtils.isRegularBase(refByte)) {
                    insertionAlleles.add(Allele.create(refByte, true));
                }
                if (cigarIndex == 0 || cigarIndex == cigar.getCigarElements().size() - 1) {
                    // if the insertion isn't completely resolved in the haplotype, skip it
                    // note this used to emit SYMBOLIC_UNASSEMBLED_EVENT_ALLELE but that seems dangerous
                } else {
                    byte[] insertionBases = new byte[] {};
                    insertionBases = ArrayUtils.add(insertionBases, ref[refPos - 1]); // add the padding base
                    insertionBases = ArrayUtils.addAll(insertionBases,
                            Arrays.copyOfRange(alignment, alignmentPos, alignmentPos + elementLength));
                    if (BaseUtils.isAllRegularBases(insertionBases)) {
                        insertionAlleles.add(Allele.create(insertionBases, false));
                    }
                }
                if (insertionAlleles.size() == 2) { // found a proper ref and alt allele
                    proposedEvents.add(new VariantContextBuilder(sourceNameToAdd, refLoc.getContig(),
                            insertionStart, insertionStart, insertionAlleles).make());
                }
            }
            alignmentPos += elementLength;
            break;
        }
        case S: {
            alignmentPos += elementLength;
            break;
        }
        case D: {
            if (refPos > 0) { // protect against trying to create insertions/deletions at the beginning of a contig
                final byte[] deletionBases = Arrays.copyOfRange(ref, refPos - 1, refPos + elementLength); // add padding base
                final List<Allele> deletionAlleles = new ArrayList<Allele>();
                final int deletionStart = refLoc.getStart() + refPos - 1;
                final byte refByte = ref[refPos - 1];
                if (BaseUtils.isRegularBase(refByte) && BaseUtils.isAllRegularBases(deletionBases)) {
                    deletionAlleles.add(Allele.create(deletionBases, true));
                    deletionAlleles.add(Allele.create(refByte, false));
                    proposedEvents.add(new VariantContextBuilder(sourceNameToAdd, refLoc.getContig(),
                            deletionStart, deletionStart + elementLength, deletionAlleles).make());
                }
            }
            refPos += elementLength;
            break;
        }
        case M:
        case EQ:
        case X: {
            for (int iii = 0; iii < elementLength; iii++) {
                final byte refByte = ref[refPos];
                final byte altByte = alignment[alignmentPos];
                if (refByte != altByte) { // SNP!
                    if (BaseUtils.isRegularBase(refByte) && BaseUtils.isRegularBase(altByte)) {
                        final List<Allele> snpAlleles = new ArrayList<Allele>();
                        snpAlleles.add(Allele.create(refByte, true));
                        snpAlleles.add(Allele.create(altByte, false));
                        proposedEvents.add(new VariantContextBuilder(sourceNameToAdd, refLoc.getContig(),
                                refLoc.getStart() + refPos, refLoc.getStart() + refPos, snpAlleles).make());
                    }
                }
                refPos++;
                alignmentPos++;
            }
            break;
        }
        case N:
        case H:
        case P:
        default:
            throw new ReviewedGATKException(
                    "Unsupported cigar operator created during SW alignment: " + ce.getOperator());
        }
    }

    for (final VariantContext proposedEvent : proposedEvents)
        addVC(proposedEvent, true);
}

From source file:org.broadinstitute.gatk.utils.haplotype.Haplotype.java

@Requires({ "refInsertLocation >= 0" })
public Haplotype insertAllele(final Allele refAllele, final Allele altAllele, final int refInsertLocation,
        final int genomicInsertLocation) {
    // refInsertLocation is in ref haplotype offset coordinates NOT genomic coordinates
    final int haplotypeInsertLocation = ReadUtils.getReadCoordinateForReferenceCoordinate(
            alignmentStartHapwrtRef, cigar, refInsertLocation, ReadUtils.ClippingTail.RIGHT_TAIL, true);
    final byte[] myBases = this.getBases();
    if (haplotypeInsertLocation == -1 || haplotypeInsertLocation + refAllele.length() >= myBases.length) { // desired change falls inside deletion so don't bother creating a new haplotype
        return null;
    }/*  w  w  w.j  ava2  s  .com*/

    byte[] newHaplotypeBases = new byte[] {};
    newHaplotypeBases = ArrayUtils.addAll(newHaplotypeBases,
            ArrayUtils.subarray(myBases, 0, haplotypeInsertLocation)); // bases before the variant
    newHaplotypeBases = ArrayUtils.addAll(newHaplotypeBases, altAllele.getBases()); // the alt allele of the variant
    newHaplotypeBases = ArrayUtils.addAll(newHaplotypeBases,
            ArrayUtils.subarray(myBases, haplotypeInsertLocation + refAllele.length(), myBases.length)); // bases after the variant
    return new Haplotype(newHaplotypeBases);
}

From source file:org.broadinstitute.gatk.utils.sam.AlignmentUtilsUnitTest.java

@Test(enabled = !DEBUG, dataProvider = "SoftClipsDataProvider")
public void testSoftClipsData(final byte[] qualsOfSoftClipsOnLeft, final int middleSize, final String middleOp,
        final byte[] qualOfSoftClipsOnRight, final int qualThreshold, final int numExpected) {
    final int readLength = (middleOp.equals("D") ? 0 : middleSize) + qualOfSoftClipsOnRight.length
            + qualsOfSoftClipsOnLeft.length;
    final GATKSAMRecord read = ArtificialSAMUtils.createArtificialRead(header, "myRead", 0, 1, readLength);
    final byte[] bases = Utils.dupBytes((byte) 'A', readLength);
    final byte[] matchBytes = middleOp.equals("D") ? new byte[] {} : Utils.dupBytes((byte) 30, middleSize);
    final byte[] quals = ArrayUtils.addAll(ArrayUtils.addAll(qualsOfSoftClipsOnLeft, matchBytes),
            qualOfSoftClipsOnRight);//  ww  w . j  a v a  2 s.c  o  m

    // set the read's bases and quals
    read.setReadBases(bases);
    read.setBaseQualities(quals);

    final StringBuilder cigar = new StringBuilder();
    if (qualsOfSoftClipsOnLeft.length > 0)
        cigar.append(qualsOfSoftClipsOnLeft.length + "S");
    if (middleSize > 0)
        cigar.append(middleSize + middleOp);
    if (qualOfSoftClipsOnRight.length > 0)
        cigar.append(qualOfSoftClipsOnRight.length + "S");

    read.setCigarString(cigar.toString());

    final int actual = AlignmentUtils.calcNumHighQualitySoftClips(read, (byte) qualThreshold);
    Assert.assertEquals(actual, numExpected,
            "Wrong number of soft clips detected for read " + read.getSAMString());
}

From source file:org.broadinstitute.gatk.utils.variant.GATKVariantContextUtils.java

/**
 *
 * @param refBases//  ww  w .  ja v  a 2 s .  c  om
 * @param altBases
 * @param remainingRefContext
 * @return
 * @deprecated there is still no alternative for this method but eventually there needs to be one implemented in TandemRepeatFinder (protected for now).
 */
@Deprecated
public static Pair<int[], byte[]> getNumTandemRepeatUnits(final byte[] refBases, final byte[] altBases,
        final byte[] remainingRefContext) {
    /* we can't exactly apply same logic as in basesAreRepeated() to compute tandem unit and number of repeated units.
      Consider case where ref =ATATAT and we have an insertion of ATAT. Natural description is (AT)3 -> (AT)2.
    */

    byte[] longB;
    // find first repeat unit based on either ref or alt, whichever is longer
    if (altBases.length > refBases.length)
        longB = altBases;
    else
        longB = refBases;

    // see if non-null allele (either ref or alt, whichever is longer) can be decomposed into several identical tandem units
    // for example, -*,CACA needs to first be decomposed into (CA)2
    final int repeatUnitLength = findRepeatedSubstring(longB);
    final byte[] repeatUnit = Arrays.copyOf(longB, repeatUnitLength);

    final int[] repetitionCount = new int[2];
    // look for repetitions forward on the ref bases (i.e. starting at beginning of ref bases)
    int repetitionsInRef = findNumberOfRepetitions(repeatUnit, refBases, true);
    repetitionCount[0] = findNumberOfRepetitions(repeatUnit, ArrayUtils.addAll(refBases, remainingRefContext),
            true) - repetitionsInRef;
    repetitionCount[1] = findNumberOfRepetitions(repeatUnit, ArrayUtils.addAll(altBases, remainingRefContext),
            true) - repetitionsInRef;

    return new Pair<>(repetitionCount, repeatUnit);

}

From source file:org.broadinstitute.sting.utils.haplotype.EventMap.java

protected void processCigarForInitialEvents() {
    final Cigar cigar = haplotype.getCigar();
    final byte[] alignment = haplotype.getBases();

    int refPos = haplotype.getAlignmentStartHapwrtRef();
    if (refPos < 0) {
        return;/*from w w w .j  a v  a2 s.com*/
    } // Protection against SW failures

    int alignmentPos = 0;

    for (int cigarIndex = 0; cigarIndex < cigar.numCigarElements(); cigarIndex++) {
        final CigarElement ce = cigar.getCigarElement(cigarIndex);
        final int elementLength = ce.getLength();
        switch (ce.getOperator()) {
        case I: {
            if (refPos > 0) { // protect against trying to create insertions/deletions at the beginning of a contig
                final List<Allele> insertionAlleles = new ArrayList<Allele>();
                final int insertionStart = refLoc.getStart() + refPos - 1;
                final byte refByte = ref[refPos - 1];
                if (BaseUtils.isRegularBase(refByte)) {
                    insertionAlleles.add(Allele.create(refByte, true));
                }
                if (cigarIndex == 0 || cigarIndex == cigar.getCigarElements().size() - 1) {
                    // if the insertion isn't completely resolved in the haplotype, skip it
                    // note this used to emit SYMBOLIC_UNASSEMBLED_EVENT_ALLELE but that seems dangerous
                } else {
                    byte[] insertionBases = new byte[] {};
                    insertionBases = ArrayUtils.add(insertionBases, ref[refPos - 1]); // add the padding base
                    insertionBases = ArrayUtils.addAll(insertionBases,
                            Arrays.copyOfRange(alignment, alignmentPos, alignmentPos + elementLength));
                    if (BaseUtils.isAllRegularBases(insertionBases)) {
                        insertionAlleles.add(Allele.create(insertionBases, false));
                    }
                }
                if (insertionAlleles.size() == 2) { // found a proper ref and alt allele
                    addVC(new VariantContextBuilder(sourceNameToAdd, refLoc.getContig(), insertionStart,
                            insertionStart, insertionAlleles).make());
                }
            }
            alignmentPos += elementLength;
            break;
        }
        case S: {
            alignmentPos += elementLength;
            break;
        }
        case D: {
            if (refPos > 0) { // protect against trying to create insertions/deletions at the beginning of a contig
                final byte[] deletionBases = Arrays.copyOfRange(ref, refPos - 1, refPos + elementLength); // add padding base
                final List<Allele> deletionAlleles = new ArrayList<Allele>();
                final int deletionStart = refLoc.getStart() + refPos - 1;
                final byte refByte = ref[refPos - 1];
                if (BaseUtils.isRegularBase(refByte) && BaseUtils.isAllRegularBases(deletionBases)) {
                    deletionAlleles.add(Allele.create(deletionBases, true));
                    deletionAlleles.add(Allele.create(refByte, false));
                    addVC(new VariantContextBuilder(sourceNameToAdd, refLoc.getContig(), deletionStart,
                            deletionStart + elementLength, deletionAlleles).make());
                }
            }
            refPos += elementLength;
            break;
        }
        case M:
        case EQ:
        case X: {
            for (int iii = 0; iii < elementLength; iii++) {
                final byte refByte = ref[refPos];
                final byte altByte = alignment[alignmentPos];
                if (refByte != altByte) { // SNP!
                    if (BaseUtils.isRegularBase(refByte) && BaseUtils.isRegularBase(altByte)) {
                        final List<Allele> snpAlleles = new ArrayList<Allele>();
                        snpAlleles.add(Allele.create(refByte, true));
                        snpAlleles.add(Allele.create(altByte, false));
                        addVC(new VariantContextBuilder(sourceNameToAdd, refLoc.getContig(),
                                refLoc.getStart() + refPos, refLoc.getStart() + refPos, snpAlleles).make());
                    }
                }
                refPos++;
                alignmentPos++;
            }
            break;
        }
        case N:
        case H:
        case P:
        default:
            throw new ReviewedStingException(
                    "Unsupported cigar operator created during SW alignment: " + ce.getOperator());
        }
    }
}

From source file:org.broadinstitute.sting.utils.haplotype.Haplotype.java

@Requires({ "refInsertLocation >= 0" })
public Haplotype insertAllele(final Allele refAllele, final Allele altAllele, final int refInsertLocation,
        final int genomicInsertLocation) {
    // refInsertLocation is in ref haplotype offset coordinates NOT genomic coordinates
    final int haplotypeInsertLocation = ReadUtils.getReadCoordinateForReferenceCoordinate(
            alignmentStartHapwrtRef, cigar, refInsertLocation, ReadUtils.ClippingTail.RIGHT_TAIL, true);
    final byte[] myBases = this.getBases();
    if (haplotypeInsertLocation == -1 || haplotypeInsertLocation + refAllele.length() >= myBases.length) { // desired change falls inside deletion so don't bother creating a new haplotype
        return null;
    }//  w  w w  .  ja va 2s.  com

    byte[] newHaplotypeBases = new byte[] {};
    newHaplotypeBases = ArrayUtils.addAll(newHaplotypeBases,
            ArrayUtils.subarray(myBases, 0, haplotypeInsertLocation)); // bases before the variant
    newHaplotypeBases = ArrayUtils.addAll(newHaplotypeBases, altAllele.getBases()); // the alt allele of the variant
    newHaplotypeBases = ArrayUtils.addAll(newHaplotypeBases,
            ArrayUtils.subarray(myBases, haplotypeInsertLocation + refAllele.length(), myBases.length)); // bases after the variant
    return new Haplotype(newHaplotypeBases, new Event(refAllele, altAllele, genomicInsertLocation));
}