-
Notifications
You must be signed in to change notification settings - Fork 596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Called Legacy Segment code #5115
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the canonical order? I thought
Call
followedSegment_Mean
in previous pipelines, since it was just appended to the CBS segment file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want IGV to be able to read it properly, the segment mean must be the last column. I think the legacy format was sub-optimal and people using IGV tended to grab the seg file before the call file was appended.
No action.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're right. Sounds good.