-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Called Legacy Segment code (#5115)
- Added functionality to generate legacy format (IGV-compatible) seg files automatically when calling copy ratio segments. Closes #5114 - Updates the WDL as well to expose the new output files.
- Loading branch information
Showing
6 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...titute/hellbender/tools/copynumber/formats/collections/CalledLegacySegmentCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package org.broadinstitute.hellbender.tools.copynumber.formats.collections; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.broadinstitute.hellbender.exceptions.UserException; | ||
import org.broadinstitute.hellbender.tools.copynumber.formats.metadata.SampleLocatableMetadata; | ||
import org.broadinstitute.hellbender.tools.copynumber.formats.records.CalledCopyRatioSegment; | ||
import org.broadinstitute.hellbender.tools.copynumber.formats.records.CalledLegacySegment; | ||
import org.broadinstitute.hellbender.utils.SimpleInterval; | ||
import org.broadinstitute.hellbender.utils.tsv.DataLine; | ||
import org.broadinstitute.hellbender.utils.tsv.TableColumnCollection; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Represents a CBS-style segmentation to enable IGV-compatible plotting. | ||
* | ||
* IGV ignores column headers and requires that no other headers are present. | ||
* We use the conventional CBS-style column headers (which includes the sample name) | ||
* and suppress the SAM-style metadata header (which breaks the contract for construction from input files). | ||
* See <a href="http://software.broadinstitute.org/cancer/software/genepattern/file-formats-guide#CBS"> | ||
* http://software.broadinstitute.org/cancer/software/genepattern/file-formats-guide#CBS</a> | ||
* and <a href="https://software.broadinstitute.org/software/igv/SEG"> | ||
* https://software.broadinstitute.org/software/igv/SEG</a>. | ||
*/ | ||
public final class CalledLegacySegmentCollection extends AbstractSampleLocatableCollection<CalledLegacySegment> { | ||
//note to developers: repeat the column headers in Javadoc so that they are viewable when linked | ||
/** | ||
* Sample, Chromosome, Start, End, Num_Probes, Call, Segment_Mean | ||
*/ | ||
enum CalledLegacySegmentTableColumn { | ||
SAMPLE("Sample"), | ||
CHROMOSOME("Chromosome"), | ||
START("Start"), | ||
END("End"), | ||
NUM_PROBES("Num_Probes"), | ||
CALL("Call"), | ||
SEGMENT_MEAN("Segment_Mean"); | ||
|
||
private final String columnName; | ||
|
||
CalledLegacySegmentTableColumn(final String columnName) { | ||
this.columnName = columnName; | ||
} | ||
|
||
static final TableColumnCollection COLUMNS = new TableColumnCollection( | ||
Stream.of(values()).map(c -> c.columnName).toArray()); | ||
} | ||
|
||
private static final Function<DataLine, CalledLegacySegment> CALLED_LEGACY_SEGMENT_DATA_LINE_TO_RECORD_FUNCTION = dataLine -> { | ||
final String sampleName = dataLine.get(CalledLegacySegmentTableColumn.SAMPLE.columnName); | ||
final String contig = dataLine.get(CalledLegacySegmentTableColumn.CHROMOSOME.columnName); | ||
final int start = dataLine.getInt(CalledLegacySegmentTableColumn.START.columnName); | ||
final int end = dataLine.getInt(CalledLegacySegmentTableColumn.END.columnName); | ||
final int numProbes = dataLine.getInt(CalledLegacySegmentTableColumn.NUM_PROBES.columnName); | ||
final double segmentMean = dataLine.getDouble(CalledLegacySegmentTableColumn.SEGMENT_MEAN.columnName); | ||
final String callOutputString = dataLine.get(CalledCopyRatioSegmentCollection.CalledCopyRatioSegmentTableColumn.CALL); | ||
final CalledCopyRatioSegment.Call call = Arrays.stream(CalledCopyRatioSegment.Call.values()) | ||
.filter(c -> c.getOutputString().equals(callOutputString)).findFirst().orElseThrow( | ||
() -> new UserException("Attempting to read an invalid value for " + | ||
CalledLegacySegmentTableColumn.CALL +": " + callOutputString + | ||
". Valid values are " + StringUtils.join(CalledCopyRatioSegment.Call.values(), ", ") | ||
)); | ||
final SimpleInterval interval = new SimpleInterval(contig, start, end); | ||
return new CalledLegacySegment(sampleName, interval, numProbes, segmentMean, call); | ||
}; | ||
|
||
private static final BiConsumer<CalledLegacySegment, DataLine> CALLED_LEGACY_SEGMENT_RECORD_AND_DATA_LINE_BI_CONSUMER = (calledLegacySegment, dataLine) -> | ||
dataLine.append(calledLegacySegment.getSampleName()) | ||
.append(calledLegacySegment.getContig()) | ||
.append(calledLegacySegment.getStart()) | ||
.append(calledLegacySegment.getEnd()) | ||
.append(calledLegacySegment.getNumProbes()) | ||
.append(calledLegacySegment.getCall().getOutputString()) | ||
.append(formatDouble(calledLegacySegment.getSegmentMean())); | ||
|
||
public CalledLegacySegmentCollection(final SampleLocatableMetadata metadata, | ||
final List<CalledLegacySegment> calledLegacySegments) { | ||
super(metadata, calledLegacySegments, CalledLegacySegmentTableColumn.COLUMNS, CALLED_LEGACY_SEGMENT_DATA_LINE_TO_RECORD_FUNCTION, CALLED_LEGACY_SEGMENT_RECORD_AND_DATA_LINE_BI_CONSUMER); | ||
} | ||
|
||
|
||
public CalledLegacySegmentCollection(final File inputFile) { | ||
super(inputFile, CopyRatioSegmentCollection.CopyRatioSegmentTableColumn.COLUMNS, CALLED_LEGACY_SEGMENT_DATA_LINE_TO_RECORD_FUNCTION, CALLED_LEGACY_SEGMENT_RECORD_AND_DATA_LINE_BI_CONSUMER); | ||
} | ||
|
||
// output of SAM-style header is suppressed | ||
@Override | ||
public void write(final File outputFile) { | ||
try (final RecordWriter recordWriter = new RecordWriter(new FileWriter(outputFile, true))) { | ||
recordWriter.writeAllRecords(getRecords()); | ||
} catch (final IOException e) { | ||
throw new UserException.CouldNotCreateOutputFile(outputFile, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...a/org/broadinstitute/hellbender/tools/copynumber/formats/records/CalledLegacySegment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.broadinstitute.hellbender.tools.copynumber.formats.records; | ||
|
||
import org.broadinstitute.hellbender.utils.SimpleInterval; | ||
import org.broadinstitute.hellbender.utils.Utils; | ||
|
||
public class CalledLegacySegment extends LegacySegment { | ||
|
||
private final CalledCopyRatioSegment.Call call; | ||
|
||
public CalledLegacySegment(final String sampleName, final SimpleInterval interval, final int numProbes, | ||
final double segmentMean, | ||
final CalledCopyRatioSegment.Call call) { | ||
super(sampleName, interval, numProbes, segmentMean); | ||
Utils.nonNull(call, "Cannot initialize a called legacy segment with a null call."); | ||
this.call = call; | ||
} | ||
|
||
public CalledCopyRatioSegment.Call getCall() { | ||
return call; | ||
} | ||
|
||
@Override | ||
public final boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
if (!super.equals(o)) { | ||
return false; | ||
} | ||
final CalledLegacySegment that = (CalledLegacySegment) o; | ||
return call == that.call; | ||
} | ||
|
||
@Override | ||
public final int hashCode() { | ||
int result = super.hashCode(); | ||
result = 31 * result + call.hashCode(); | ||
return result; | ||
} | ||
|
||
@Override | ||
public final String toString() { | ||
return "CalledLegacySegment{" + | ||
"interval=" + getInterval() + | ||
", numPoints=" + getNumProbes() + | ||
", meanLog2CopyRatio=" + getSegmentMean() + | ||
", call=" + call + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters