Skip to content

Commit

Permalink
fix: remove set functions in GtfInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
sanashah007 committed Aug 9, 2024
1 parent df07f5b commit 865ffd4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public enum Type{
TRANSCRIPT
}

private Type type;
private final Type type;
private final String geneName;
private Interval interval;
private final Interval interval;

public GtfInfo(Interval interval, Type type, String geneName) {
this.interval = interval;
Expand All @@ -23,10 +23,6 @@ public Type getType() {
return type;
}

public void setType(Type type) {
this.type = type;
}

public String getGeneName() {
return geneName;
}
Expand All @@ -43,10 +39,6 @@ public Integer getEnd() {
return interval.getEnd();
}

public void setInterval(Interval interval) {
this.interval = interval;
}

@Override
public String toString() {
return "GtfInfo{ " + "type = " + type + " geneName = " + geneName + "interval = " + interval;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public class GtfToBed extends FeatureWalker<GencodeGtfFeature> {
@Argument( fullName = SORT_BY_BASIC_LONG_NAME, doc = "Only use basic transcripts")
public boolean sortByBasic = false;

private final Map<String, GtfInfo> idToInfo = new HashMap<>();
//stores either gene or transcript ID and summary information about the feature
private final Map<String, GtfInfo> featureInfoMap = new HashMap<>();

@Override
protected boolean isAcceptableFeatureType(Class<? extends Feature> featureType) {
Expand Down Expand Up @@ -160,7 +161,7 @@ private void processGeneFeature(GencodeGtfFeature gtfFeature) {
final GtfInfo gtfInfo = new GtfInfo(interval, GtfInfo.Type.GENE, gtfFeature.getGeneName());

// store in hashmap with key as geneId
idToInfo.put(gtfFeature.getGeneId(), gtfInfo);
featureInfoMap.put(gtfFeature.getGeneId(), gtfInfo);
}

// stores the transcript ID and Interval info in hashmap
Expand All @@ -170,7 +171,7 @@ private void processTranscriptFeature(GencodeGtfFeature gtfFeature) {
final GtfInfo gtfInfo = new GtfInfo(interval, GtfInfo.Type.TRANSCRIPT, gtfFeature.getGeneName());

//store in hashmap with key as transcriptId
idToInfo.put(gtfFeature.getTranscriptId(), gtfInfo);
featureInfoMap.put(gtfFeature.getTranscriptId(), gtfInfo);

//update start/end of corresponding gene if needed
updateGeneStart(gtfFeature);
Expand All @@ -180,34 +181,34 @@ private void processTranscriptFeature(GencodeGtfFeature gtfFeature) {
// update the gene interval start position based on the transcript
private void updateGeneStart(GencodeGtfFeature gtfFeature) {
// get the start value of the gene
int geneStart = idToInfo.get(gtfFeature.getGeneId()).getStart();
int geneStart = featureInfoMap.get(gtfFeature.getGeneId()).getStart();

// if the transcript start is less than the gene start
if (gtfFeature.getStart() < geneStart) {
// set the gene start to be the transcript start
geneStart = gtfFeature.getStart();
updateGeneInterval(gtfFeature, geneStart, idToInfo.get(gtfFeature.getGeneId()).getEnd());
updateGeneInterval(gtfFeature, geneStart, featureInfoMap.get(gtfFeature.getGeneId()).getEnd());
}
}

// update the gene interval end position based on the transcript
private void updateGeneEnd(GencodeGtfFeature gtfFeature) {
// get the end value of the gene
int geneEnd = idToInfo.get(gtfFeature.getGeneId()).getEnd();
int geneEnd = featureInfoMap.get(gtfFeature.getGeneId()).getEnd();

// if the transcript start is greater than the gene start
if (gtfFeature.getEnd() > geneEnd) {
// set the gene end to be the transcript end
geneEnd = gtfFeature.getEnd();
updateGeneInterval(gtfFeature, idToInfo.get(gtfFeature.getGeneId()).getStart(), geneEnd);
updateGeneInterval(gtfFeature, featureInfoMap.get(gtfFeature.getGeneId()).getStart(), geneEnd);
}
}

// updates an interval of the gene if it needs to be changed
private void updateGeneInterval(GencodeGtfFeature gtfFeature, int geneStart, int geneEnd) {
Interval geneInterval = new Interval(gtfFeature.getContig(), geneStart, geneEnd);
GtfInfo gtfGeneInfo = new GtfInfo(geneInterval, GtfInfo.Type.GENE, gtfFeature.getGeneName());
idToInfo.put(gtfFeature.getGeneId(), gtfGeneInfo);
featureInfoMap.put(gtfFeature.getGeneId(), gtfGeneInfo);
}

// runs immediately after it has gone through each line of gtf (apply method)
Expand Down Expand Up @@ -245,8 +246,8 @@ public static class GtfInfoComparator implements Comparator<Map.Entry<String, Gt
// compare two entries of a map where key = geneId or transcriptId and value = gtfInfo object
@Override
public int compare(Map.Entry<String, GtfInfo> e1, Map.Entry<String, GtfInfo> e2) {
Interval e1Interval = e1.getValue().getInterval();
Interval e2Interval = e2.getValue().getInterval();
final Interval e1Interval = e1.getValue().getInterval();
final Interval e2Interval = e2.getValue().getInterval();

Utils.nonNull(dictionary.getSequence(e1Interval.getContig()), "could not get sequence for " + e1Interval.getContig());
Utils.nonNull(dictionary.getSequence(e2Interval.getContig()), "could not get sequence for " + e2Interval.getContig());
Expand Down Expand Up @@ -275,7 +276,7 @@ public int compare(Map.Entry<String, GtfInfo> e1, Map.Entry<String, GtfInfo> e2)
// sorts the map containing the features based on contig and start position
private LinkedHashMap<String, GtfInfo> getSortedMap(SAMSequenceDictionary sequenceDictionary) {
// create a list that has the keys and values of idToInfo and sort the list using GtfInfoComparator
List<Map.Entry<String, GtfInfo>> entries = new ArrayList<>(idToInfo.entrySet());
List<Map.Entry<String, GtfInfo>> entries = new ArrayList<>(featureInfoMap.entrySet());
entries.sort(new GtfInfoComparator(sequenceDictionary));

// put each (sorted) entry in the list into a linked hashmap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.broadinstitute.hellbender.tools.walkers.conversion;

import org.broadinstitute.hellbender.CommandLineProgramTest;
import org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions;
import org.broadinstitute.hellbender.testutils.ArgumentsBuilder;
import org.broadinstitute.hellbender.testutils.IntegrationTestSpec;
import org.testng.Assert;
Expand All @@ -18,13 +17,13 @@ public class GtfToBedIntegrationTest extends CommandLineProgramTest {
private static final File mouse = new File(ConversionTestUtils.getMouseGtf());
private static final File mouseDictionary = new File(ConversionTestUtils.getMouseDict());
private static final File dictionary = new File(ConversionTestUtils.getReferenceDict());
private static final File decoysGeneBed = new File(ConversionTestUtils.getDecoySamplesGeneBed());
private static final File decoysTranscriptBed = new File(ConversionTestUtils.getDecoySamplesTranscriptBed());
private static final File mapk1GeneBed = new File(ConversionTestUtils.getMapk1GeneBed());
private static final File mapk1TranscriptBed = new File(ConversionTestUtils.getMapk1TranscriptBed());
private static final File manyTranscriptsBed = new File (ConversionTestUtils.getManyTranscriptsBed());
private static final File notBasicBed = new File(ConversionTestUtils.getNotBasicBed());
private static final File mouseBed = new File(ConversionTestUtils.getMouseBed());
private static final File expectedDecoysGeneBed = new File(ConversionTestUtils.getDecoySamplesGeneBed());
private static final File expectedDecoysTranscriptBed = new File(ConversionTestUtils.getDecoySamplesTranscriptBed());
private static final File expectedMapk1GeneBed = new File(ConversionTestUtils.getMapk1GeneBed());
private static final File expectedMapk1TranscriptBed = new File(ConversionTestUtils.getMapk1TranscriptBed());
private static final File expectedManyTranscriptsBed = new File (ConversionTestUtils.getManyTranscriptsBed());
private static final File expectedNotBasicBed = new File(ConversionTestUtils.getNotBasicBed());
private static final File expectedMouseBed = new File(ConversionTestUtils.getMouseBed());

// tests specifically mapk1 gene
@Test
Expand Down Expand Up @@ -77,10 +76,10 @@ private void runMapk1(boolean transcript) throws IOException {

if (transcript) {
Assert.assertEquals(countLines(outputFile), 3);
IntegrationTestSpec.assertEqualTextFiles(mapk1TranscriptBed, outputFile);
IntegrationTestSpec.assertEqualTextFiles(expectedMapk1TranscriptBed, outputFile);
} else {
Assert.assertEquals(countLines(outputFile), 1);
IntegrationTestSpec.assertEqualTextFiles(mapk1GeneBed, outputFile);
IntegrationTestSpec.assertEqualTextFiles(expectedMapk1GeneBed, outputFile);
}
}

Expand All @@ -96,11 +95,11 @@ private void runDecoySample(boolean transcript) throws IOException {

if (transcript) {
Assert.assertEquals(countLines(outputFile), 20);
IntegrationTestSpec.assertEqualTextFiles(decoysTranscriptBed, outputFile);
IntegrationTestSpec.assertEqualTextFiles(expectedDecoysTranscriptBed, outputFile);

} else {
Assert.assertEquals(countLines(outputFile), 19);
IntegrationTestSpec.assertEqualTextFiles(decoysGeneBed, outputFile);
IntegrationTestSpec.assertEqualTextFiles(expectedDecoysGeneBed, outputFile);
}
}

Expand All @@ -112,7 +111,7 @@ private void runManyTranscripts() throws IOException {
.add(GtfToBed.SORT_BY_BASIC_LONG_NAME, true)
.addOutput(outputFile);
runCommandLine(argsBuilder);
IntegrationTestSpec.assertEqualTextFiles(manyTranscriptsBed, outputFile);
IntegrationTestSpec.assertEqualTextFiles(expectedManyTranscriptsBed, outputFile);

}

Expand All @@ -124,7 +123,7 @@ private void runNotBasic() throws IOException {
.add(GtfToBed.SORT_BY_BASIC_LONG_NAME, false)
.addOutput(outputFile);
runCommandLine(argsBuilder);
IntegrationTestSpec.assertEqualTextFiles(notBasicBed, outputFile);
IntegrationTestSpec.assertEqualTextFiles(expectedNotBasicBed, outputFile);
}

private void runMouse() throws IOException {
Expand All @@ -135,7 +134,7 @@ private void runMouse() throws IOException {
.add(GtfToBed.SORT_BY_BASIC_LONG_NAME, false)
.addOutput(outputFile);
runCommandLine(argsBuilder);
IntegrationTestSpec.assertEqualTextFiles(mouseBed, outputFile);
IntegrationTestSpec.assertEqualTextFiles(expectedMouseBed, outputFile);
}

private int countLines(File file) throws IOException {
Expand Down

0 comments on commit 865ffd4

Please sign in to comment.