-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ALS-6467] Configurable VCF excerpt info column ordering
- Make column sorter that pulls from properties - Use it to sort and exclude columns (cherry picked from commit d3289ef)
- Loading branch information
Showing
3 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
processing/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/processing/ColumnSorter.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,41 @@ | ||
package edu.harvard.hms.dbmi.avillach.hpds.processing; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
public class ColumnSorter { | ||
private final Map<String, Integer> infoColumnsOrder; | ||
|
||
@Autowired | ||
public ColumnSorter(@Value("#{'${variant.info_column_order:}'}") String infoColumnOrderString) { | ||
if (infoColumnOrderString == null || infoColumnOrderString.isEmpty()) { | ||
infoColumnsOrder = Map.of(); | ||
} else { | ||
String[] infoColumnOrder = infoColumnOrderString.split(","); | ||
HashMap<String, Integer> order = new HashMap<>(); | ||
for (int i = 0; i < infoColumnOrder.length; i++) { | ||
order.put(infoColumnOrder[i], i); | ||
} | ||
this.infoColumnsOrder = order; | ||
} | ||
} | ||
|
||
public List<String> sortInfoColumns(Set<String> columns) { | ||
// backwards compatibility check. | ||
if (infoColumnsOrder.isEmpty()) { | ||
return new ArrayList<>(columns); | ||
} | ||
return columns.stream() | ||
.filter(infoColumnsOrder::containsKey) | ||
.sorted((a, b) -> Integer.compare( | ||
infoColumnsOrder.getOrDefault(a, Integer.MAX_VALUE), | ||
infoColumnsOrder.getOrDefault(b, Integer.MAX_VALUE) | ||
)) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
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
processing/src/test/java/edu/harvard/hms/dbmi/avillach/hpds/processing/ColumnSorterTest.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 edu.harvard.hms.dbmi.avillach.hpds.processing; | ||
|
||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import static org.hamcrest.CoreMatchers.*; | ||
|
||
public class ColumnSorterTest { | ||
|
||
@Test | ||
public void shouldSortColumns() { | ||
ColumnSorter subject = new ColumnSorter("a,b,c"); | ||
List<String> actual = subject.sortInfoColumns(Set.of("b", "c", "a")); | ||
List<String> expected = List.of("a", "b", "c"); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
public void shouldExcludeMissingColumns() { | ||
ColumnSorter subject = new ColumnSorter("a,b,c"); | ||
List<String> actual = subject.sortInfoColumns(Set.of("d", "b", "c", "a")); | ||
List<String> expected = List.of("a", "b", "c"); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
public void shouldNotBreakForMissingColumns() { | ||
ColumnSorter subject = new ColumnSorter("a,b,c,d"); | ||
List<String> actual = subject.sortInfoColumns(Set.of("d", "a")); | ||
List<String> expected = List.of("a", "d"); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
public void shouldNoOpWithoutConfig() { | ||
ColumnSorter subject = new ColumnSorter(""); | ||
List<String> actual = subject.sortInfoColumns(Set.of("b", "c", "a")); | ||
List<String> expected = List.of("b", "c", "a"); | ||
|
||
assertThat(new HashSet<>(expected), is(equalTo(new HashSet<>(actual)))); | ||
} | ||
} |