Added a more efficient implementation of the KBest haplotype finder code.
Story: https://www.pivotaltracker.com/story/show/66238286 Changes: 1. Created a new k-best haplotype search implementation in class KBestHaplotypeFinder. 2. Changed HC code to use the new implementation. This seems to fix the original problem without causing significant changes in outputs using some empirical data test cases 3. Moved haplotype's cigar calculation code from Path to CigarUtils; need that in order to gain independence from Path in some parts of the code. In any case that seems like a more natural location for that functionality.
This commit is contained in:
parent
09d2415bea
commit
7acf2eb0e7
|
|
@ -54,8 +54,9 @@ import net.sf.samtools.CigarOperator;
|
|||
import org.apache.log4j.Logger;
|
||||
import org.broadinstitute.sting.gatk.walkers.haplotypecaller.graphs.*;
|
||||
import org.broadinstitute.sting.utils.GenomeLoc;
|
||||
import org.broadinstitute.sting.utils.haplotype.Haplotype;
|
||||
import org.broadinstitute.sting.utils.activeregion.ActiveRegion;
|
||||
import org.broadinstitute.sting.utils.haplotype.Haplotype;
|
||||
import org.broadinstitute.sting.utils.sam.CigarUtils;
|
||||
import org.broadinstitute.sting.utils.sam.GATKSAMRecord;
|
||||
import org.broadinstitute.variant.variantcontext.Allele;
|
||||
import org.broadinstitute.variant.variantcontext.VariantContext;
|
||||
|
|
@ -220,12 +221,13 @@ public abstract class LocalAssemblyEngine {
|
|||
final SeqVertex source = graph.getReferenceSourceVertex();
|
||||
final SeqVertex sink = graph.getReferenceSinkVertex();
|
||||
if ( source == null || sink == null ) throw new IllegalArgumentException("Both source and sink cannot be null but got " + source + " and sink " + sink + " for graph "+ graph);
|
||||
|
||||
final KBestPaths<SeqVertex,BaseEdge> pathFinder = new KBestPaths<>(allowCyclesInKmerGraphToGeneratePaths);
|
||||
for ( final Path<SeqVertex,BaseEdge> path : pathFinder.getKBestPaths(graph, numBestHaplotypesPerGraph, source, sink) ) {
|
||||
Haplotype h = new Haplotype( path.getBases() );
|
||||
final KBestHaplotypeFinder haplotypeFinder = new KBestHaplotypeFinder(graph,source,sink);
|
||||
final Iterator<KBestHaplotype> bestHaplotypes = haplotypeFinder.iterator(numBestHaplotypesPerGraph);
|
||||
while (bestHaplotypes.hasNext()) {
|
||||
final KBestHaplotype kBestHaplotype = bestHaplotypes.next();
|
||||
final Haplotype h = kBestHaplotype.haplotype();
|
||||
if( !returnHaplotypes.contains(h) ) {
|
||||
final Cigar cigar = path.calculateCigar(refHaplotype.getBases());
|
||||
final Cigar cigar = CigarUtils.calculateCigar(refHaplotype.getBases(),h.getBases());
|
||||
|
||||
if ( cigar == null ) {
|
||||
// couldn't produce a meaningful alignment of haplotype to reference, fail quietly
|
||||
|
|
@ -239,12 +241,11 @@ public abstract class LocalAssemblyEngine {
|
|||
} else if( cigar.getReferenceLength() != refHaplotype.getCigar().getReferenceLength() ) { // SW failure
|
||||
throw new IllegalStateException("Smith-Waterman alignment failure. Cigar = " + cigar + " with reference length "
|
||||
+ cigar.getReferenceLength() + " but expecting reference length of " + refHaplotype.getCigar().getReferenceLength()
|
||||
+ " ref = " + refHaplotype + " path " + new String(path.getBases()));
|
||||
+ " ref = " + refHaplotype + " path " + new String(h.getBases()));
|
||||
}
|
||||
|
||||
h.setCigar(cigar);
|
||||
h.setAlignmentStartHapwrtRef(activeRegionStart);
|
||||
h.setScore(path.getScore());
|
||||
h.setGenomeLocation(activeRegionWindow);
|
||||
returnHaplotypes.add(h);
|
||||
assemblyResultSet.add(h, assemblyResultByGraph.get(graph));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* By downloading the PROGRAM you agree to the following terms of use:
|
||||
*
|
||||
* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY
|
||||
*
|
||||
* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE).
|
||||
*
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions.
|
||||
* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows:
|
||||
*
|
||||
* 1. DEFINITIONS
|
||||
* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE.
|
||||
*
|
||||
* 2. LICENSE
|
||||
* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM.
|
||||
* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement.
|
||||
* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement.
|
||||
* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM.
|
||||
*
|
||||
* 3. OWNERSHIP OF INTELLECTUAL PROPERTY
|
||||
* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication.
|
||||
* Copyright 2012 Broad Institute, Inc.
|
||||
* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc.
|
||||
* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes.
|
||||
*
|
||||
* 4. INDEMNIFICATION
|
||||
* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement.
|
||||
*
|
||||
* 5. NO REPRESENTATIONS OR WARRANTIES
|
||||
* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME.
|
||||
* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING.
|
||||
*
|
||||
* 6. ASSIGNMENT
|
||||
* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void.
|
||||
*
|
||||
* 7. MISCELLANEOUS
|
||||
* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries.
|
||||
* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes.
|
||||
* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4.
|
||||
* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt.
|
||||
* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter.
|
||||
* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement.
|
||||
* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles.
|
||||
*/
|
||||
package org.broadinstitute.sting.gatk.walkers.haplotypecaller.graphs;
|
||||
|
||||
/**
|
||||
* Represent a trivial k-best sub haplotype finder with no solutions.
|
||||
*
|
||||
* <p>To be used at vertices that do not have any valid path to the requested sink node</p>
|
||||
*
|
||||
* @author Valentin Ruano-Rubio <valentin@broadinstitute.org>
|
||||
*/
|
||||
final class DeadEndKBestSubHaplotypeFinder implements KBestSubHaplotypeFinder {
|
||||
|
||||
/**
|
||||
* Sole instance of this class.
|
||||
*/
|
||||
public static DeadEndKBestSubHaplotypeFinder INSTANCE = new DeadEndKBestSubHaplotypeFinder();
|
||||
|
||||
/**
|
||||
* Prevents instantiation of more than one instance; please use {@link #INSTANCE}.
|
||||
*/
|
||||
protected DeadEndKBestSubHaplotypeFinder() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KBestHaplotype getKBest(int k) {
|
||||
if (k < 0)
|
||||
throw new IllegalArgumentException("k cannot be negative");
|
||||
else
|
||||
throw new IllegalArgumentException("k cannot be equal or greater to the haplotype count");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
* By downloading the PROGRAM you agree to the following terms of use:
|
||||
*
|
||||
* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY
|
||||
*
|
||||
* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE).
|
||||
*
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions.
|
||||
* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows:
|
||||
*
|
||||
* 1. DEFINITIONS
|
||||
* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE.
|
||||
*
|
||||
* 2. LICENSE
|
||||
* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM.
|
||||
* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement.
|
||||
* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement.
|
||||
* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM.
|
||||
*
|
||||
* 3. OWNERSHIP OF INTELLECTUAL PROPERTY
|
||||
* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication.
|
||||
* Copyright 2012 Broad Institute, Inc.
|
||||
* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc.
|
||||
* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes.
|
||||
*
|
||||
* 4. INDEMNIFICATION
|
||||
* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement.
|
||||
*
|
||||
* 5. NO REPRESENTATIONS OR WARRANTIES
|
||||
* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME.
|
||||
* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING.
|
||||
*
|
||||
* 6. ASSIGNMENT
|
||||
* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void.
|
||||
*
|
||||
* 7. MISCELLANEOUS
|
||||
* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries.
|
||||
* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes.
|
||||
* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4.
|
||||
* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt.
|
||||
* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter.
|
||||
* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement.
|
||||
* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles.
|
||||
*/
|
||||
package org.broadinstitute.sting.gatk.walkers.haplotypecaller.graphs;
|
||||
|
||||
/**
|
||||
* Trivial k-best sub-haplotype finder where the source and sink vertex are the same one.
|
||||
*
|
||||
* @author Valentin Ruano-Rubio <valentin@broadinstitute.org>
|
||||
*/
|
||||
class EmptyPathHaplotypeFinderNode implements KBestSubHaplotypeFinder {
|
||||
|
||||
|
||||
/**
|
||||
* Caches the only solution returned by this finder.
|
||||
*/
|
||||
private final KBestHaplotype singleHaplotypePath;
|
||||
|
||||
public EmptyPathHaplotypeFinderNode(final SeqGraph graph, final SeqVertex sink) {
|
||||
singleHaplotypePath = new MyBestHaplotypePath(graph,sink);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KBestHaplotype getKBest(int k) {
|
||||
if (k < 0)
|
||||
throw new IllegalArgumentException("k cannot be negative");
|
||||
if (k > 0)
|
||||
throw new IllegalArgumentException("k cannot greater than the possible haplotype count");
|
||||
return singleHaplotypePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom extension of {@link KBestHaplotype} that implements the single solution behaviour.
|
||||
*/
|
||||
private class MyBestHaplotypePath extends KBestHaplotype {
|
||||
|
||||
private final SeqVertex vertex;
|
||||
|
||||
private final SeqGraph graph;
|
||||
|
||||
private Boolean isReference;
|
||||
|
||||
public MyBestHaplotypePath(final SeqGraph graph, final SeqVertex vertex) {
|
||||
this.vertex = vertex;
|
||||
this.graph = graph;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SeqGraph graph() {
|
||||
return graph;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int score() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int rank() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SeqVertex head() {
|
||||
return vertex;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected KBestHaplotype tail() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReference() {
|
||||
return (isReference != null) ? isReference: (isReference = graph.isReferenceNode(vertex));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,161 @@
|
|||
/*
|
||||
* By downloading the PROGRAM you agree to the following terms of use:
|
||||
*
|
||||
* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY
|
||||
*
|
||||
* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE).
|
||||
*
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions.
|
||||
* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows:
|
||||
*
|
||||
* 1. DEFINITIONS
|
||||
* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE.
|
||||
*
|
||||
* 2. LICENSE
|
||||
* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM.
|
||||
* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement.
|
||||
* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement.
|
||||
* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM.
|
||||
*
|
||||
* 3. OWNERSHIP OF INTELLECTUAL PROPERTY
|
||||
* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication.
|
||||
* Copyright 2012 Broad Institute, Inc.
|
||||
* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc.
|
||||
* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes.
|
||||
*
|
||||
* 4. INDEMNIFICATION
|
||||
* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement.
|
||||
*
|
||||
* 5. NO REPRESENTATIONS OR WARRANTIES
|
||||
* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME.
|
||||
* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING.
|
||||
*
|
||||
* 6. ASSIGNMENT
|
||||
* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void.
|
||||
*
|
||||
* 7. MISCELLANEOUS
|
||||
* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries.
|
||||
* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes.
|
||||
* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4.
|
||||
* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt.
|
||||
* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter.
|
||||
* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement.
|
||||
* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles.
|
||||
*/
|
||||
package org.broadinstitute.sting.gatk.walkers.haplotypecaller.graphs;
|
||||
|
||||
import org.broadinstitute.sting.utils.haplotype.Haplotype;
|
||||
|
||||
/**
|
||||
* Represents a result from a K-best haplotype search.
|
||||
*
|
||||
* @author Valentin Ruano-Rubio <valentin@broadinstitute.org>
|
||||
*/
|
||||
public abstract class KBestHaplotype {
|
||||
|
||||
/**
|
||||
* Returns the original graph searched.
|
||||
*
|
||||
* @return never {@code null}
|
||||
*/
|
||||
public abstract SeqGraph graph();
|
||||
|
||||
/**
|
||||
* Returns the result haplotype score.
|
||||
*
|
||||
* <p>Currently, the score is the multiplicity total sum of edges along the haplotype path</p>
|
||||
*
|
||||
* @return 0 or greater.
|
||||
*/
|
||||
public abstract int score();
|
||||
|
||||
/**
|
||||
* Indicates whether this result is the reference haplotype.
|
||||
*
|
||||
* @return {@code true} if it is the reference haplotype, {@code false} otherwise.
|
||||
*/
|
||||
public abstract boolean isReference();
|
||||
|
||||
/**
|
||||
* The rank of this solution within the list of solutions that resulted from the same search.
|
||||
*
|
||||
* <p>0 would correspond to the best solution, 1 with the second best and so on</p>
|
||||
*
|
||||
* @return 0 or greater.
|
||||
*/
|
||||
public abstract int rank();
|
||||
|
||||
private byte[] bases;
|
||||
|
||||
private Haplotype haplotype;
|
||||
|
||||
private Path<SeqVertex,BaseEdge> path;
|
||||
|
||||
/**
|
||||
* Returns the result haplotype bases.
|
||||
*
|
||||
* @return never {@code null}.
|
||||
*/
|
||||
public byte[] bases() {
|
||||
if (bases != null) return bases;
|
||||
final KBestHaplotype tail = tail();
|
||||
final SeqVertex head = head();
|
||||
if (tail == null)
|
||||
bases = head.getSequence();
|
||||
else {
|
||||
final byte[] tailBases = tail.bases();
|
||||
final byte[] headBases = head.getSequence();
|
||||
final int length = tailBases.length + headBases.length;
|
||||
bases = new byte[length];
|
||||
System.arraycopy(headBases,0,bases,0,headBases.length);
|
||||
System.arraycopy(tailBases,0,bases,headBases.length,tailBases.length);
|
||||
}
|
||||
return bases;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the solution haplotype.
|
||||
*
|
||||
* @return never {@code null}.
|
||||
*/
|
||||
public Haplotype haplotype() {
|
||||
if (haplotype != null) return haplotype;
|
||||
haplotype = new Haplotype(bases(),isReference());
|
||||
haplotype.setScore(score());
|
||||
return haplotype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path across the original graph that correspond to the solution haplotype.
|
||||
*
|
||||
* @return never {@code null}, although perhaps a zero-length path (only one vertex).
|
||||
*/
|
||||
public Path<SeqVertex,BaseEdge> path() {
|
||||
if (path != null) return path;
|
||||
final KBestHaplotype tail = tail();
|
||||
if (tail == null)
|
||||
path = new Path<>(head(),graph());
|
||||
else {
|
||||
final Path<SeqVertex,BaseEdge> tailPath = tail.path();
|
||||
path = new Path<>(graph().getEdge(head(),tailPath.getFirstVertex()),tailPath);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* The first vertex on the haplotype path.
|
||||
*
|
||||
* @return never {@code null}.
|
||||
*/
|
||||
protected abstract SeqVertex head();
|
||||
|
||||
/**
|
||||
* Returns the sub-haplotype from the second vertex involved in the haplotype until the end.
|
||||
*
|
||||
* @return {@code null} if there are no more vertices in the solution path a part from the one returned by {@link #head}.
|
||||
*/
|
||||
protected abstract KBestHaplotype tail();
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,245 @@
|
|||
/*
|
||||
* By downloading the PROGRAM you agree to the following terms of use:
|
||||
*
|
||||
* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY
|
||||
*
|
||||
* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE).
|
||||
*
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions.
|
||||
* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows:
|
||||
*
|
||||
* 1. DEFINITIONS
|
||||
* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE.
|
||||
*
|
||||
* 2. LICENSE
|
||||
* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM.
|
||||
* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement.
|
||||
* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement.
|
||||
* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM.
|
||||
*
|
||||
* 3. OWNERSHIP OF INTELLECTUAL PROPERTY
|
||||
* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication.
|
||||
* Copyright 2012 Broad Institute, Inc.
|
||||
* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc.
|
||||
* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes.
|
||||
*
|
||||
* 4. INDEMNIFICATION
|
||||
* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement.
|
||||
*
|
||||
* 5. NO REPRESENTATIONS OR WARRANTIES
|
||||
* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME.
|
||||
* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING.
|
||||
*
|
||||
* 6. ASSIGNMENT
|
||||
* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void.
|
||||
*
|
||||
* 7. MISCELLANEOUS
|
||||
* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries.
|
||||
* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes.
|
||||
* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4.
|
||||
* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt.
|
||||
* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter.
|
||||
* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement.
|
||||
* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles.
|
||||
*/
|
||||
package org.broadinstitute.sting.gatk.walkers.haplotypecaller.graphs;
|
||||
|
||||
import org.jgrapht.alg.CycleDetector;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Efficient algorithm to obtain the list of best haplotypes given the {@link SeqGraph instace}.
|
||||
*
|
||||
* @author Valentin Ruano-Rubio <valentin@broadinstitute.org>
|
||||
*/
|
||||
public class KBestHaplotypeFinder extends AbstractList<KBestHaplotype> implements Iterable<KBestHaplotype> {
|
||||
|
||||
|
||||
private final SeqGraph graph;
|
||||
protected Map<SeqVertex,KBestSubHaplotypeFinder> nodeBySource;
|
||||
|
||||
protected SeqVertex sink;
|
||||
protected SeqVertex source;
|
||||
|
||||
/**
|
||||
* Constructs a new best haplotypes finder.
|
||||
*
|
||||
* @param graph the seq-graph to search.
|
||||
* @param source the source vertex for all haplotypes.
|
||||
* @param sink the sink vertex for all haplotypes.
|
||||
*
|
||||
* @throws IllegalArgumentException if <ul>
|
||||
* <li>any of {@code graph}, {@code source} or {@code sink} is {@code null} or</li>
|
||||
* <li>either {@code source} or {@code sink} is not a vertex of {@code graph}'s.</li>
|
||||
* </ul>
|
||||
*/
|
||||
public KBestHaplotypeFinder(final SeqGraph graph, final SeqVertex source, final SeqVertex sink) {
|
||||
if (graph == null) throw new IllegalArgumentException("graph cannot be null");
|
||||
if (source == null) throw new IllegalArgumentException("source cannot be null");
|
||||
if (sink == null) throw new IllegalArgumentException("sink cannot be null");
|
||||
if (!graph.containsVertex(source)) throw new IllegalArgumentException("source does not belong to the graph");
|
||||
if (!graph.containsVertex(sink)) throw new IllegalArgumentException("sink does not belong to the graph");
|
||||
//TODO dealing with cycles here due to a bug in some of the graph transformations that produces cycles.
|
||||
//TODO Once that is solve, the conditional above should be removed in order the save time:
|
||||
//this.graph = graph;
|
||||
if (new CycleDetector<>(graph).detectCycles())
|
||||
this.graph = removeCycles(graph,source,sink);
|
||||
else
|
||||
this.graph = graph;
|
||||
nodeBySource = new HashMap<>(graph.vertexSet().size());
|
||||
this.sink = sink;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
private static SeqGraph removeCycles(final SeqGraph original, final SeqVertex source, final SeqVertex sink) {
|
||||
final Set<BaseEdge> edgesToRemove = new HashSet<>(original.edgeSet().size());
|
||||
final Set<SeqVertex> vertexToRemove = new HashSet<>(original.vertexSet().size());
|
||||
|
||||
if (!findGuiltyVerticesAndEdgesToRemoveCycles(original, source, sink, edgesToRemove, vertexToRemove, new HashSet<SeqVertex>(original.vertexSet().size())))
|
||||
throw new IllegalStateException("could not find any path from the source vertex to the sink vertex: " + source + " -> " + sink);
|
||||
|
||||
if (edgesToRemove.isEmpty() && vertexToRemove.isEmpty())
|
||||
throw new IllegalStateException("cannot find a way to remove the cycles");
|
||||
|
||||
final SeqGraph result = (SeqGraph) original.clone();
|
||||
result.removeAllEdges(edgesToRemove);
|
||||
result.removeAllVertices(vertexToRemove);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean findGuiltyVerticesAndEdgesToRemoveCycles(final SeqGraph graph, final SeqVertex currentVertex, final SeqVertex sink,
|
||||
final Set<BaseEdge> edgesToRemove, final Set<SeqVertex> verticesToRemove,
|
||||
final Set<SeqVertex> parentVertices) {
|
||||
if (currentVertex.equals(sink)) return true;
|
||||
|
||||
final Set<BaseEdge> outgoingEdges = graph.outgoingEdgesOf(currentVertex);
|
||||
boolean reachsSink = false;
|
||||
parentVertices.add(currentVertex);
|
||||
|
||||
for (final BaseEdge edge : outgoingEdges) {
|
||||
final SeqVertex child = graph.getEdgeTarget(edge);
|
||||
if (parentVertices.contains(child))
|
||||
edgesToRemove.add(edge);
|
||||
else {
|
||||
final boolean childReachSink = findGuiltyVerticesAndEdgesToRemoveCycles(graph, child, sink, edgesToRemove, verticesToRemove, parentVertices);
|
||||
reachsSink = reachsSink || childReachSink;
|
||||
}
|
||||
}
|
||||
parentVertices.remove(currentVertex);
|
||||
if (!reachsSink) verticesToRemove.add(currentVertex);
|
||||
return reachsSink;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public KBestHaplotype get(int index) {
|
||||
final KBestSubHaplotypeFinder sourceNode = createNode(source);
|
||||
if (index < 0 || index >= size())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return sourceNode.getKBest(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<KBestHaplotype> iterator() {
|
||||
final KBestSubHaplotypeFinder sourceFinder = createNode(source);
|
||||
|
||||
return new Iterator<KBestHaplotype>() {
|
||||
private int nextK = 0;
|
||||
private final int maxK = sourceFinder.getCount();
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextK < maxK;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KBestHaplotype next() {
|
||||
if (nextK >= maxK) throw new NoSuchElementException();
|
||||
return sourceFinder.getKBest(nextK++);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return createNode(source).getCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator on the first k best haplotypes.
|
||||
* <p>
|
||||
* It might return less than k haplotypes if the total number of possible haplotypes is smaller.
|
||||
* </p>
|
||||
*
|
||||
* @param k the maximum number of haplotypes to return.
|
||||
* @return never {@code null}, but perhaps a iterator that return no haplotype.
|
||||
*/
|
||||
public Iterator<KBestHaplotype> iterator(final int k) {
|
||||
final KBestSubHaplotypeFinder sourceFinder = createNode(source);
|
||||
|
||||
return new Iterator<KBestHaplotype>() {
|
||||
private int nextK = 0;
|
||||
private final int maxK = Math.min(sourceFinder.getCount(), k);
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextK < maxK;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KBestHaplotype next() {
|
||||
if (nextK >= maxK) throw new NoSuchElementException();
|
||||
return sourceFinder.getKBest(nextK++);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected KBestSubHaplotypeFinder createNode(final SeqVertex source) {
|
||||
KBestSubHaplotypeFinder node = nodeBySource.get(source);
|
||||
if (node == null) {
|
||||
if (source.equals(sink))
|
||||
node = new EmptyPathHaplotypeFinderNode(graph,sink);
|
||||
else {
|
||||
final Set<BaseEdge> outgoingEdges = graph.outgoingEdgesOf(source);
|
||||
if (outgoingEdges.isEmpty())
|
||||
node = DeadEndKBestSubHaplotypeFinder.INSTANCE;
|
||||
else {
|
||||
final Map<BaseEdge,KBestSubHaplotypeFinder> undeadChildren = createChildrenNodes(outgoingEdges);
|
||||
if (undeadChildren.isEmpty())
|
||||
node = DeadEndKBestSubHaplotypeFinder.INSTANCE;
|
||||
else
|
||||
node = new RecursiveSubHaplotypeFinder(graph,source,undeadChildren);
|
||||
|
||||
}
|
||||
}
|
||||
nodeBySource.put(source,node);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private Map<BaseEdge, KBestSubHaplotypeFinder> createChildrenNodes(Set<BaseEdge> baseEdges) {
|
||||
final Map<BaseEdge,KBestSubHaplotypeFinder> result = new LinkedHashMap<>(baseEdges.size());
|
||||
for (final BaseEdge edge : baseEdges)
|
||||
result.put(edge,createNode(graph.getEdgeTarget(edge)));
|
||||
final Iterator<Map.Entry<BaseEdge,KBestSubHaplotypeFinder>> childrenIterator = result.entrySet().iterator();
|
||||
while (childrenIterator.hasNext())
|
||||
if (childrenIterator.next().getValue().getCount() == 0)
|
||||
childrenIterator.remove();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* By downloading the PROGRAM you agree to the following terms of use:
|
||||
*
|
||||
* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY
|
||||
*
|
||||
* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE).
|
||||
*
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions.
|
||||
* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows:
|
||||
*
|
||||
* 1. DEFINITIONS
|
||||
* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE.
|
||||
*
|
||||
* 2. LICENSE
|
||||
* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM.
|
||||
* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement.
|
||||
* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement.
|
||||
* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM.
|
||||
*
|
||||
* 3. OWNERSHIP OF INTELLECTUAL PROPERTY
|
||||
* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication.
|
||||
* Copyright 2012 Broad Institute, Inc.
|
||||
* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc.
|
||||
* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes.
|
||||
*
|
||||
* 4. INDEMNIFICATION
|
||||
* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement.
|
||||
*
|
||||
* 5. NO REPRESENTATIONS OR WARRANTIES
|
||||
* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME.
|
||||
* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING.
|
||||
*
|
||||
* 6. ASSIGNMENT
|
||||
* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void.
|
||||
*
|
||||
* 7. MISCELLANEOUS
|
||||
* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries.
|
||||
* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes.
|
||||
* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4.
|
||||
* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt.
|
||||
* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter.
|
||||
* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement.
|
||||
* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles.
|
||||
*/
|
||||
package org.broadinstitute.sting.gatk.walkers.haplotypecaller.graphs;
|
||||
|
||||
/**
|
||||
* Common interface for K-Best sub-haplotype finders.
|
||||
*
|
||||
* @author Valentin Ruano-Rubio <valentin@broadinstitute.org>
|
||||
*/
|
||||
interface KBestSubHaplotypeFinder {
|
||||
|
||||
/**
|
||||
* Returns the total number of possible sub-haplotypes.
|
||||
* @return 0 or greater.
|
||||
*/
|
||||
public abstract int getCount();
|
||||
|
||||
/**
|
||||
* Return the k-best sub-haplotype solution.
|
||||
*
|
||||
*
|
||||
* @param k the requested solution rank.
|
||||
* @throws IllegalArgumentException if {@code k} is outside bounds [0 .. {@link #getCount()}).
|
||||
*
|
||||
* @return never {@code null}.
|
||||
*/
|
||||
public abstract KBestHaplotype getKBest(int k);
|
||||
|
||||
}
|
||||
|
|
@ -48,12 +48,8 @@ package org.broadinstitute.sting.gatk.walkers.haplotypecaller.graphs;
|
|||
|
||||
import com.google.java.contract.Ensures;
|
||||
import net.sf.samtools.Cigar;
|
||||
import net.sf.samtools.CigarElement;
|
||||
import net.sf.samtools.CigarOperator;
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.broadinstitute.sting.utils.smithwaterman.*;
|
||||
import org.broadinstitute.sting.utils.sam.AlignmentUtils;
|
||||
import org.broadinstitute.sting.utils.sam.CigarUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
|
@ -68,8 +64,6 @@ import java.util.*;
|
|||
*
|
||||
*/
|
||||
public class Path<T extends BaseVertex, E extends BaseEdge> {
|
||||
private final static String SW_PAD = "NNNNNNNNNN";
|
||||
private final static Logger logger = Logger.getLogger(Path.class);
|
||||
|
||||
// the last vertex seen in the path
|
||||
protected final T lastVertex;
|
||||
|
|
@ -84,10 +78,6 @@ public class Path<T extends BaseVertex, E extends BaseEdge> {
|
|||
// the graph from which this path originated
|
||||
protected final BaseGraph<T, E> graph;
|
||||
|
||||
// used in the bubble state machine to apply Smith-Waterman to the bubble sequence
|
||||
// these values were chosen via optimization against the NA12878 knowledge base
|
||||
public static final Parameters NEW_SW_PARAMETERS = new Parameters(20.0, -15.0, -26.0, -1.1);
|
||||
|
||||
/**
|
||||
* Create a new Path containing no edges and starting at initialVertex
|
||||
* @param initialVertex the starting vertex of the path
|
||||
|
|
@ -348,96 +338,10 @@ public class Path<T extends BaseVertex, E extends BaseEdge> {
|
|||
* @param refSeq the reference sequence that all of the bases in this path should align to
|
||||
* @return a Cigar mapping this path to refSeq, or null if no reasonable alignment could be found
|
||||
*/
|
||||
public Cigar calculateCigar(final byte[] refSeq) {
|
||||
if ( getBases().length == 0 ) {
|
||||
// horrible edge case from the unit tests, where this path has no bases
|
||||
return new Cigar(Arrays.asList(new CigarElement(refSeq.length, CigarOperator.D)));
|
||||
}
|
||||
|
||||
final byte[] bases = getBases();
|
||||
final Cigar nonStandard;
|
||||
|
||||
final String paddedRef = SW_PAD + new String(refSeq) + SW_PAD;
|
||||
final String paddedPath = SW_PAD + new String(bases) + SW_PAD;
|
||||
final SmithWaterman alignment = new SWPairwiseAlignment( paddedRef.getBytes(), paddedPath.getBytes(), NEW_SW_PARAMETERS );
|
||||
|
||||
if ( isSWFailure(alignment) )
|
||||
return null;
|
||||
|
||||
// cut off the padding bases
|
||||
final int baseStart = SW_PAD.length();
|
||||
final int baseEnd = paddedPath.length() - SW_PAD.length() - 1; // -1 because it's inclusive
|
||||
nonStandard = AlignmentUtils.trimCigarByBases(alignment.getCigar(), baseStart, baseEnd);
|
||||
|
||||
if ( nonStandard.getReferenceLength() != refSeq.length ) {
|
||||
nonStandard.add(new CigarElement(refSeq.length - nonStandard.getReferenceLength(), CigarOperator.D));
|
||||
}
|
||||
|
||||
// finally, return the cigar with all indels left aligned
|
||||
return leftAlignCigarSequentially(nonStandard, refSeq, getBases(), 0, 0);
|
||||
public Cigar calculateCigar(final byte[] refSeq) {
|
||||
return CigarUtils.calculateCigar(refSeq,getBases());
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that the SW didn't fail in some terrible way, and throw exception if it did
|
||||
*/
|
||||
private boolean isSWFailure(final SmithWaterman alignment) {
|
||||
// check that the alignment starts at the first base, which it should given the padding
|
||||
if ( alignment.getAlignmentStart2wrt1() > 0 ) {
|
||||
return true;
|
||||
// throw new IllegalStateException("SW failure ref " + paddedRef + " vs. " + paddedPath + " should always start at 0, but got " + alignment.getAlignmentStart2wrt1() + " with cigar " + alignment.getCigar());
|
||||
}
|
||||
|
||||
// check that we aren't getting any S operators (which would be very bad downstream)
|
||||
for ( final CigarElement ce : alignment.getCigar().getCigarElements() ) {
|
||||
if ( ce.getOperator() == CigarOperator.S )
|
||||
return true;
|
||||
// soft clips at the end of the alignment are really insertions
|
||||
// throw new IllegalStateException("SW failure ref " + paddedRef + " vs. " + paddedPath + " should never contain S operators but got cigar " + alignment.getCigar());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Left align the given cigar sequentially. This is needed because AlignmentUtils doesn't accept cigars with more than one indel in them.
|
||||
* This is a target of future work to incorporate and generalize into AlignmentUtils for use by others.
|
||||
* @param cigar the cigar to left align
|
||||
* @param refSeq the reference byte array
|
||||
* @param readSeq the read byte array
|
||||
* @param refIndex 0-based alignment start position on ref
|
||||
* @param readIndex 0-based alignment start position on read
|
||||
* @return the left-aligned cigar
|
||||
*/
|
||||
@Ensures({"cigar != null", "refSeq != null", "readSeq != null", "refIndex >= 0", "readIndex >= 0"})
|
||||
protected static Cigar leftAlignCigarSequentially(final Cigar cigar, final byte[] refSeq, final byte[] readSeq, int refIndex, int readIndex) {
|
||||
final Cigar cigarToReturn = new Cigar();
|
||||
Cigar cigarToAlign = new Cigar();
|
||||
for (int i = 0; i < cigar.numCigarElements(); i++) {
|
||||
final CigarElement ce = cigar.getCigarElement(i);
|
||||
if (ce.getOperator() == CigarOperator.D || ce.getOperator() == CigarOperator.I) {
|
||||
cigarToAlign.add(ce);
|
||||
final Cigar leftAligned = AlignmentUtils.leftAlignSingleIndel(cigarToAlign, refSeq, readSeq, refIndex, readIndex, false);
|
||||
for ( final CigarElement toAdd : leftAligned.getCigarElements() ) { cigarToReturn.add(toAdd); }
|
||||
refIndex += cigarToAlign.getReferenceLength();
|
||||
readIndex += cigarToAlign.getReadLength();
|
||||
cigarToAlign = new Cigar();
|
||||
} else {
|
||||
cigarToAlign.add(ce);
|
||||
}
|
||||
}
|
||||
if( !cigarToAlign.isEmpty() ) {
|
||||
for( final CigarElement toAdd : cigarToAlign.getCigarElements() ) {
|
||||
cigarToReturn.add(toAdd);
|
||||
}
|
||||
}
|
||||
|
||||
final Cigar result = AlignmentUtils.consolidateCigar(cigarToReturn);
|
||||
if( result.getReferenceLength() != cigar.getReferenceLength() )
|
||||
throw new IllegalStateException("leftAlignCigarSequentially failed to produce a valid CIGAR. Reference lengths differ. Initial cigar " + cigar + " left aligned into " + result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests that this and other have the same score and vertices in the same order with the same seq
|
||||
* @param other the other path to consider. Cannot be null
|
||||
|
|
@ -463,4 +367,5 @@ public class Path<T extends BaseVertex, E extends BaseEdge> {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,242 @@
|
|||
/*
|
||||
* By downloading the PROGRAM you agree to the following terms of use:
|
||||
*
|
||||
* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY
|
||||
*
|
||||
* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE).
|
||||
*
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and
|
||||
* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions.
|
||||
* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows:
|
||||
*
|
||||
* 1. DEFINITIONS
|
||||
* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE.
|
||||
*
|
||||
* 2. LICENSE
|
||||
* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM.
|
||||
* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement.
|
||||
* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement.
|
||||
* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM.
|
||||
*
|
||||
* 3. OWNERSHIP OF INTELLECTUAL PROPERTY
|
||||
* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication.
|
||||
* Copyright 2012 Broad Institute, Inc.
|
||||
* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc.
|
||||
* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes.
|
||||
*
|
||||
* 4. INDEMNIFICATION
|
||||
* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement.
|
||||
*
|
||||
* 5. NO REPRESENTATIONS OR WARRANTIES
|
||||
* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME.
|
||||
* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING.
|
||||
*
|
||||
* 6. ASSIGNMENT
|
||||
* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void.
|
||||
*
|
||||
* 7. MISCELLANEOUS
|
||||
* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries.
|
||||
* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes.
|
||||
* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4.
|
||||
* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt.
|
||||
* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter.
|
||||
* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement.
|
||||
* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles.
|
||||
*/
|
||||
package org.broadinstitute.sting.gatk.walkers.haplotypecaller.graphs;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
/**
|
||||
* General recursive sub-haplotype finder.
|
||||
* <p>
|
||||
* Provides the k-best sub-haplotypes looking into the outgoing set of vertices (that contain at least one solution).
|
||||
* </p>
|
||||
* <p>
|
||||
* This is done efficiently by keeping an priority-queue on best subhaplotype solutions and pulling them on demand
|
||||
* as needed.
|
||||
* </p>
|
||||
* <p>
|
||||
* Solutions are cached for repeated retrieval so that we save compute at vertices that share sub-haplotypes
|
||||
* (share descendant vertices). This aspect is controlled by {@link KBestSubHaplotypeFinder} that instantiate
|
||||
* a unique {@link KBestSubHaplotypeFinder} for each vertex in the graph that belongs to a valid path
|
||||
* between the source and sink node.
|
||||
* </p>
|
||||
*
|
||||
* @author Valentin Ruano-Rubio <valentin@broadinstitute.org>
|
||||
*/
|
||||
class RecursiveSubHaplotypeFinder implements KBestSubHaplotypeFinder {
|
||||
|
||||
private final SeqGraph graph;
|
||||
|
||||
private final SeqVertex vertex;
|
||||
|
||||
private final Map<BaseEdge,KBestSubHaplotypeFinder> children;
|
||||
|
||||
private boolean childrenWereProcessed = false;
|
||||
|
||||
/**
|
||||
* Holds the number of possible paths from this source node vertex to the sink vertex.
|
||||
*
|
||||
* <p>Updated by {@link #processChildrenIfNeeded()}</p>
|
||||
*/
|
||||
private int possibleHaplotypeCount;
|
||||
|
||||
/**
|
||||
* Holds the best {@code i} paths to the sink so far calculated where {@code i+1} is the length of rankedResults.
|
||||
*
|
||||
* <p>As more results are requested the array will grow. All positions and solutions are calculated up to {@code i}</p>.
|
||||
*/
|
||||
private ArrayList<KBestHaplotype> rankedResults;
|
||||
|
||||
/**
|
||||
* Priority queue with best sub-haplotype solutions that haven't been calculated and cached on {@link #rankedResults} yet.
|
||||
*/
|
||||
private PriorityQueue<ChildKBestSubHaplotype> nextChildrenKBestHaplotypePath;
|
||||
|
||||
/**
|
||||
* Creates a recursive sub-haplotype finder give the target graph, first vertex and all possible outgoing edges
|
||||
* with the corresponding sub-sub-haplotype finders.
|
||||
*
|
||||
* <p>For efficiency shake, it will not verify the content of {@code children} map; i.e. that indeed all keys
|
||||
* are outgoing edges from {@code vertex} on {@code graph} and that the value sub-haplotype resolver have as
|
||||
* the first vertex the adjacent vertex through that key edge.</p>
|
||||
*
|
||||
* @param graph the search graph.
|
||||
* @param vertex first vertex for all sub-haplotype solutions provided by this finder
|
||||
* @param children map from outgoing edge to the corresponding sub-sub-haplotype finder.
|
||||
*/
|
||||
public RecursiveSubHaplotypeFinder(final SeqGraph graph, final SeqVertex vertex,
|
||||
final Map<BaseEdge, KBestSubHaplotypeFinder> children) {
|
||||
if (vertex == null) throw new IllegalArgumentException("the vertex provided cannot be null");
|
||||
if (graph == null) throw new IllegalArgumentException("the graph provided cannot be null");
|
||||
this.vertex = vertex;
|
||||
this.children = children;
|
||||
this.graph = graph;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
processChildrenIfNeeded();
|
||||
return possibleHaplotypeCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process children and initialize structures if not done before.
|
||||
*/
|
||||
private void processChildrenIfNeeded() {
|
||||
if (childrenWereProcessed) return;
|
||||
long possibleHaplotypeCount = 0;
|
||||
|
||||
nextChildrenKBestHaplotypePath = new PriorityQueue<>(children.size());
|
||||
|
||||
for (final Map.Entry<BaseEdge,KBestSubHaplotypeFinder> entry : children.entrySet()) {
|
||||
final KBestSubHaplotypeFinder child = entry.getValue();
|
||||
final BaseEdge edge = entry.getKey();
|
||||
final int childPossibleHaplotypePathCount = child.getCount();
|
||||
if (childPossibleHaplotypePathCount != 0) // paranoia check, should not happen at this point.
|
||||
nextChildrenKBestHaplotypePath.add(new ChildKBestSubHaplotype(-1,edge,child,0));
|
||||
possibleHaplotypeCount += childPossibleHaplotypePathCount;
|
||||
}
|
||||
|
||||
// Just make sure we won't incur in overflow here for very large graphs; who is ever going to ask for more than 2G paths!!!)
|
||||
this.possibleHaplotypeCount = (int) Math.min(Integer.MAX_VALUE,possibleHaplotypeCount);
|
||||
|
||||
// 10 is a bit arbitrary as it is difficult to anticipate what would be the number of requested
|
||||
// best sub-haplotypes for any node. It shouldn't be too large so that it does not waste space
|
||||
// but not too small so that there is no need to resize when just a few best solutions are requested.
|
||||
rankedResults = new ArrayList<>(Math.min(this.possibleHaplotypeCount,10));
|
||||
|
||||
childrenWereProcessed = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KBestHaplotype getKBest(int k) {
|
||||
if (k < 0)
|
||||
throw new IllegalArgumentException("the rank requested cannot be negative");
|
||||
processChildrenIfNeeded();
|
||||
if (k >= possibleHaplotypeCount)
|
||||
throw new IllegalArgumentException("the rank requested cannot be equal or greater to the number of possible haplotypes");
|
||||
if (rankedResults.size() > k)
|
||||
return rankedResults.get(k);
|
||||
|
||||
rankedResults.ensureCapacity(k+1);
|
||||
for (int i = rankedResults.size(); i <= k; i++) {
|
||||
// since k < possibleHaplotypeCount is guarantee no to be empty.
|
||||
if (nextChildrenKBestHaplotypePath.isEmpty())
|
||||
throw new IllegalStateException("what the heck " + k + " " + possibleHaplotypeCount);
|
||||
final ChildKBestSubHaplotype nextResult = nextChildrenKBestHaplotypePath.remove();
|
||||
nextResult.rank = i;
|
||||
rankedResults.add(nextResult);
|
||||
final int childRank = nextResult.subpath.rank();
|
||||
final KBestSubHaplotypeFinder child = nextResult.child;
|
||||
|
||||
// if there is no further solution from the same child we cannot add another solution from that child.
|
||||
if (childRank + 1 >= nextResult.child.getCount())
|
||||
continue;
|
||||
nextChildrenKBestHaplotypePath.add(new ChildKBestSubHaplotype(-1,nextResult.edge, child, childRank + 1));
|
||||
}
|
||||
return rankedResults.get(k);
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom extension of the {@link KBestHaplotype} used for solutions generated by this class.
|
||||
*/
|
||||
private class ChildKBestSubHaplotype extends KBestHaplotype implements Comparable<ChildKBestSubHaplotype>{
|
||||
private final int score;
|
||||
private int rank;
|
||||
private final KBestSubHaplotypeFinder child;
|
||||
private final BaseEdge edge;
|
||||
private final KBestHaplotype subpath;
|
||||
private final boolean isReference;
|
||||
|
||||
public ChildKBestSubHaplotype(final int rank, final BaseEdge edge,
|
||||
final KBestSubHaplotypeFinder child, final int childRank) {
|
||||
this.child = child;
|
||||
this.edge = edge;
|
||||
this.rank = rank;
|
||||
this.subpath = child.getKBest(childRank);
|
||||
this.score = edge.getMultiplicity() + subpath.score();
|
||||
this.isReference = edge.isRef() && subpath.isReference();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SeqGraph graph() {
|
||||
return graph;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(final ChildKBestSubHaplotype other) {
|
||||
if (other == null) throw new IllegalArgumentException("the other object cannot be null");
|
||||
return - Integer.compare(this.score,other.score);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int score() {
|
||||
return score;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int rank() {
|
||||
return rank;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected SeqVertex head() {
|
||||
return vertex;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected KBestHaplotype tail() {
|
||||
return subpath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReference() {
|
||||
return isReference;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -50,16 +50,19 @@ import net.sf.samtools.Cigar;
|
|||
import net.sf.samtools.SAMFileHeader;
|
||||
import net.sf.samtools.SAMTag;
|
||||
import org.broadinstitute.sting.gatk.io.StingSAMFileWriter;
|
||||
import org.broadinstitute.sting.gatk.walkers.haplotypecaller.graphs.Path;
|
||||
import org.broadinstitute.sting.utils.GenomeLoc;
|
||||
import org.broadinstitute.sting.utils.Utils;
|
||||
import org.broadinstitute.sting.utils.genotyper.PerReadAlleleLikelihoodMap;
|
||||
import org.broadinstitute.sting.utils.haplotype.Haplotype;
|
||||
import org.broadinstitute.sting.utils.sam.AlignmentUtils;
|
||||
import org.broadinstitute.sting.utils.sam.CigarUtils;
|
||||
import org.broadinstitute.sting.utils.sam.GATKSAMRecord;
|
||||
import org.broadinstitute.sting.utils.smithwaterman.SWPairwiseAlignment;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A BAMWriter that aligns reads to haplotypes and emits their best alignments to a BAM file
|
||||
|
|
@ -220,7 +223,7 @@ public abstract class HaplotypeBAMWriter {
|
|||
|
||||
try {
|
||||
// compute the smith-waterman alignment of read -> haplotype
|
||||
final SWPairwiseAlignment swPairwiseAlignment = new SWPairwiseAlignment(haplotype.getBases(), originalRead.getReadBases(), Path.NEW_SW_PARAMETERS);
|
||||
final SWPairwiseAlignment swPairwiseAlignment = new SWPairwiseAlignment(haplotype.getBases(), originalRead.getReadBases(), CigarUtils.NEW_SW_PARAMETERS);
|
||||
//swPairwiseAlignment.printAlignment(haplotype.getBases(), originalRead.getReadBases());
|
||||
if ( swPairwiseAlignment.getAlignmentStart2wrt1() == -1 )
|
||||
// sw can fail (reasons not clear) so if it happens just don't write the read
|
||||
|
|
|
|||
|
|
@ -53,6 +53,7 @@ import net.sf.samtools.TextCigarCodec;
|
|||
import org.broadinstitute.sting.BaseTest;
|
||||
import org.broadinstitute.sting.utils.Utils;
|
||||
import org.broadinstitute.sting.utils.sam.AlignmentUtils;
|
||||
import org.broadinstitute.sting.utils.sam.CigarUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
|
@ -132,7 +133,7 @@ public class KBestPathsUnitTest extends BaseTest {
|
|||
Assert.assertTrue(justOne.get(0).pathsAreTheSame(best), "Best path from complete enumerate " + best + " not the same as from k = 1 search " + justOne.get(0));
|
||||
}
|
||||
|
||||
@Test(enabled = !DEBUG)
|
||||
@Test(enabled = false) // No longer supported, but no longer needed.
|
||||
public void testPathFindingComplexCycle() {
|
||||
SeqGraph graph = new SeqGraph(11);
|
||||
|
||||
|
|
@ -152,7 +153,7 @@ public class KBestPathsUnitTest extends BaseTest {
|
|||
Assert.assertEquals(paths.size(), 1, "Didn't find the expected number of paths");
|
||||
}
|
||||
|
||||
@Test(enabled = !DEBUG)
|
||||
@Test(enabled = false) // No longer supported, but no longer needed.
|
||||
public void testPathFindingCycleLastNode() {
|
||||
SeqGraph graph = new SeqGraph(11);
|
||||
|
||||
|
|
@ -539,7 +540,7 @@ public class KBestPathsUnitTest extends BaseTest {
|
|||
String theRef = preRefString + refString + Utils.dupString(indelString1, refIndel1) + refString + Utils.dupString(indelString2, refIndel2) + refString + postRefString;
|
||||
String theRead = refString + Utils.dupString(indelString1, refIndel1 + indelOp1 * indelSize1) + refString + Utils.dupString(indelString2, refIndel2 + indelOp2 * indelSize2) + refString;
|
||||
|
||||
Cigar calculatedCigar = Path.leftAlignCigarSequentially(AlignmentUtils.consolidateCigar(givenCigar), theRef.getBytes(), theRead.getBytes(), preRefString.length(), 0);
|
||||
Cigar calculatedCigar = CigarUtils.leftAlignCigarSequentially(AlignmentUtils.consolidateCigar(givenCigar), theRef.getBytes(), theRead.getBytes(), preRefString.length(), 0);
|
||||
Assert.assertEquals(AlignmentUtils.consolidateCigar(calculatedCigar).toString(), AlignmentUtils.consolidateCigar(expectedCigar).toString(), "Cigar strings do not match!");
|
||||
}
|
||||
}
|
||||
|
|
@ -553,7 +554,7 @@ public class KBestPathsUnitTest extends BaseTest {
|
|||
final String hap = "GTCTCTCTCTCTCTCTCTCTCTATATATATATATTT";
|
||||
final Cigar originalCigar = TextCigarCodec.getSingleton().decode("18M4I12M4D2M");
|
||||
|
||||
final Cigar result = Path.leftAlignCigarSequentially(originalCigar, ref.getBytes(), hap.getBytes(), 0, 0);
|
||||
final Cigar result = CigarUtils.leftAlignCigarSequentially(originalCigar, ref.getBytes(), hap.getBytes(), 0, 0);
|
||||
logger.warn("Result is " + result);
|
||||
Assert.assertEquals(originalCigar.getReferenceLength(), result.getReferenceLength(), "Reference lengths are different");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,12 +25,17 @@
|
|||
|
||||
package org.broadinstitute.sting.utils.sam;
|
||||
|
||||
import com.google.java.contract.Ensures;
|
||||
import net.sf.samtools.Cigar;
|
||||
import net.sf.samtools.CigarElement;
|
||||
import net.sf.samtools.CigarOperator;
|
||||
import net.sf.samtools.TextCigarCodec;
|
||||
import org.broadinstitute.sting.utils.exceptions.ReviewedStingException;
|
||||
import org.broadinstitute.sting.utils.smithwaterman.Parameters;
|
||||
import org.broadinstitute.sting.utils.smithwaterman.SWPairwiseAlignment;
|
||||
import org.broadinstitute.sting.utils.smithwaterman.SmithWaterman;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
|
|
@ -164,4 +169,104 @@ public class CigarUtils {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// used in the bubble state machine to apply Smith-Waterman to the bubble sequence
|
||||
// these values were chosen via optimization against the NA12878 knowledge base
|
||||
public static final Parameters NEW_SW_PARAMETERS = new Parameters(20.0, -15.0, -26.0, -1.1);
|
||||
|
||||
private final static String SW_PAD = "NNNNNNNNNN";
|
||||
|
||||
/**
|
||||
* Calculate the cigar elements for this path against the reference sequence
|
||||
*
|
||||
* @param refSeq the reference sequence that all of the bases in this path should align to
|
||||
* @return a Cigar mapping this path to refSeq, or null if no reasonable alignment could be found
|
||||
*/
|
||||
public static Cigar calculateCigar(final byte[] refSeq, final byte[] altSeq) {
|
||||
if ( altSeq.length == 0 ) {
|
||||
// horrible edge case from the unit tests, where this path has no bases
|
||||
return new Cigar(Arrays.asList(new CigarElement(refSeq.length, CigarOperator.D)));
|
||||
}
|
||||
|
||||
final Cigar nonStandard;
|
||||
|
||||
final String paddedRef = SW_PAD + new String(refSeq) + SW_PAD;
|
||||
final String paddedPath = SW_PAD + new String(altSeq) + SW_PAD;
|
||||
final SmithWaterman alignment = new SWPairwiseAlignment( paddedRef.getBytes(), paddedPath.getBytes(), NEW_SW_PARAMETERS );
|
||||
|
||||
if ( isSWFailure(alignment) )
|
||||
return null;
|
||||
|
||||
// cut off the padding bases
|
||||
final int baseStart = SW_PAD.length();
|
||||
final int baseEnd = paddedPath.length() - SW_PAD.length() - 1; // -1 because it's inclusive
|
||||
nonStandard = AlignmentUtils.trimCigarByBases(alignment.getCigar(), baseStart, baseEnd);
|
||||
|
||||
if ( nonStandard.getReferenceLength() != refSeq.length ) {
|
||||
nonStandard.add(new CigarElement(refSeq.length - nonStandard.getReferenceLength(), CigarOperator.D));
|
||||
}
|
||||
|
||||
// finally, return the cigar with all indels left aligned
|
||||
return leftAlignCigarSequentially(nonStandard, refSeq, altSeq, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that the SW didn't fail in some terrible way, and throw exception if it did
|
||||
*/
|
||||
private static boolean isSWFailure(final SmithWaterman alignment) {
|
||||
// check that the alignment starts at the first base, which it should given the padding
|
||||
if ( alignment.getAlignmentStart2wrt1() > 0 ) {
|
||||
return true;
|
||||
// throw new IllegalStateException("SW failure ref " + paddedRef + " vs. " + paddedPath + " should always start at 0, but got " + alignment.getAlignmentStart2wrt1() + " with cigar " + alignment.getCigar());
|
||||
}
|
||||
|
||||
// check that we aren't getting any S operators (which would be very bad downstream)
|
||||
for ( final CigarElement ce : alignment.getCigar().getCigarElements() ) {
|
||||
if ( ce.getOperator() == CigarOperator.S )
|
||||
return true;
|
||||
// soft clips at the end of the alignment are really insertions
|
||||
// throw new IllegalStateException("SW failure ref " + paddedRef + " vs. " + paddedPath + " should never contain S operators but got cigar " + alignment.getCigar());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Left align the given cigar sequentially. This is needed because AlignmentUtils doesn't accept cigars with more than one indel in them.
|
||||
* This is a target of future work to incorporate and generalize into AlignmentUtils for use by others.
|
||||
* @param cigar the cigar to left align
|
||||
* @param refSeq the reference byte array
|
||||
* @param readSeq the read byte array
|
||||
* @param refIndex 0-based alignment start position on ref
|
||||
* @param readIndex 0-based alignment start position on read
|
||||
* @return the left-aligned cigar
|
||||
*/
|
||||
@Ensures({"cigar != null", "refSeq != null", "readSeq != null", "refIndex >= 0", "readIndex >= 0"})
|
||||
public static Cigar leftAlignCigarSequentially(final Cigar cigar, final byte[] refSeq, final byte[] readSeq, int refIndex, int readIndex) {
|
||||
final Cigar cigarToReturn = new Cigar();
|
||||
Cigar cigarToAlign = new Cigar();
|
||||
for (int i = 0; i < cigar.numCigarElements(); i++) {
|
||||
final CigarElement ce = cigar.getCigarElement(i);
|
||||
if (ce.getOperator() == CigarOperator.D || ce.getOperator() == CigarOperator.I) {
|
||||
cigarToAlign.add(ce);
|
||||
final Cigar leftAligned = AlignmentUtils.leftAlignSingleIndel(cigarToAlign, refSeq, readSeq, refIndex, readIndex, false);
|
||||
for ( final CigarElement toAdd : leftAligned.getCigarElements() ) { cigarToReturn.add(toAdd); }
|
||||
refIndex += cigarToAlign.getReferenceLength();
|
||||
readIndex += cigarToAlign.getReadLength();
|
||||
cigarToAlign = new Cigar();
|
||||
} else {
|
||||
cigarToAlign.add(ce);
|
||||
}
|
||||
}
|
||||
if( !cigarToAlign.isEmpty() ) {
|
||||
for( final CigarElement toAdd : cigarToAlign.getCigarElements() ) {
|
||||
cigarToReturn.add(toAdd);
|
||||
}
|
||||
}
|
||||
|
||||
final Cigar result = AlignmentUtils.consolidateCigar(cigarToReturn);
|
||||
if( result.getReferenceLength() != cigar.getReferenceLength() )
|
||||
throw new IllegalStateException("leftAlignCigarSequentially failed to produce a valid CIGAR. Reference lengths differ. Initial cigar " + cigar + " left aligned into " + result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue