Skip to content

Commit

Permalink
working better
Browse files Browse the repository at this point in the history
  • Loading branch information
alisman committed Jan 2, 2025
1 parent 336515b commit baf4516
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ public HashMap<String, AlterationCountByGene> getAlterationEnrichmentCounts(List
n.getValue().setNumberOfProfiledCases(
geneCount.get(n.getKey()).getNumberOfProfiledCases()
);
} else {
n.getValue().setNumberOfProfiledCases(0);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,35 @@ public class AlterationEnrichmentUtil<T extends AlterationCountBase> {
private GenePanelService genePanelService;
@Autowired
private MolecularProfileService molecularProfileService;


public double calculatePValue(List<CountSummary> counts){
double pValue;
if (counts.size() == 2) {
int alteredInNoneCount = counts.get(1).getProfiledCount() - counts.get(1).getAlteredCount();
int alteredOnlyInQueryGenesCount = counts.get(0).getProfiledCount()
- counts.get(0).getAlteredCount();

pValue = fisherExactTestCalculator.getTwoTailedPValue(alteredInNoneCount,
counts.get(1).getAlteredCount(), alteredOnlyInQueryGenesCount,
counts.get(0).getAlteredCount());
} else {

long[][] array = counts.stream().map(count -> {
return new long[]{count.getAlteredCount(),
count.getProfiledCount() - count.getAlteredCount()};
}).toArray(long[][]::new);

ChiSquareTest chiSquareTest = new ChiSquareTest();
pValue = chiSquareTest.chiSquareTest(array);

// set p-value to 1 when the cases in all groups are altered
if (Double.isNaN(pValue)) {
pValue = 1;
}
}
return pValue;
}

public List<AlterationEnrichment> createAlterationEnrichments(Map<String, Pair<List<AlterationCountByGene>, Long>> mutationCountsbyGroup) {

Map<String, Map<Integer, AlterationCountByGene>> mutationCountsbyEntrezGeneIdAndGroup = mutationCountsbyGroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.validation.Valid;
import org.apache.commons.math3.stat.inference.ChiSquareTest;
import org.cbioportal.model.AlterationCountByGene;
import org.cbioportal.model.AlterationEnrichment;
import org.cbioportal.model.AlterationFilter;
Expand Down Expand Up @@ -40,6 +41,7 @@
import org.cbioportal.service.ViolinPlotService;
import org.cbioportal.service.exception.MolecularProfileNotFoundException;
import org.cbioportal.service.exception.StudyNotFoundException;
import org.cbioportal.service.util.AlterationEnrichmentUtil;
import org.cbioportal.service.util.CustomDataSession;
import org.cbioportal.utils.config.annotation.ConditionalOnProperty;
import org.cbioportal.web.columnar.util.CustomDataFilterUtil;
Expand Down Expand Up @@ -151,6 +153,8 @@ public ResponseEntity<List<AlterationEnrichment>> fetchAlterationEnrichmentsNew(
@Parameter(required = true, description = "List of groups containing sample identifiers and list of Alteration Types")
@Valid @RequestBody(required = false) MolecularProfileCasesGroupAndAlterationTypeFilter groupsAndAlterationTypes) throws MolecularProfileNotFoundException {


var util = new AlterationEnrichmentUtil();

// this needs to be the response type
//List<AlterationEnrichment>
Expand Down Expand Up @@ -206,9 +210,29 @@ public ResponseEntity<List<AlterationEnrichment>> fetchAlterationEnrichmentsNew(
}).collect(Collectors.toList());
enrichment.setCounts(counts);

var filteredCounts = counts.stream().filter(groupCasesCount -> groupCasesCount.getProfiledCount() > 0)
.collect(Collectors.toList());

// groups where number of altered cases is greater than profiled cases.
// This is a temporary fix for https://github.com/cBioPortal/cbioportal/issues/7274
// and https://github.com/cBioPortal/cbioportal/issues/7418
long invalidDataGroups = filteredCounts
.stream()
.filter(groupCasesCount -> groupCasesCount.getAlteredCount() > groupCasesCount.getProfiledCount())
.count();

// calculate p-value only if more than one group have profile cases count
// greater than 0
if (filteredCounts.size() > 1 && invalidDataGroups == 0) {
enrichment.setpValue(BigDecimal.valueOf(util.calculatePValue(counts)));
}

return enrichment;

}).collect(Collectors.toList());





// List<AlterationEnrichment> alterationEnrichments = alterationEnrichmentService.getAlterationEnrichments(
Expand Down

0 comments on commit baf4516

Please sign in to comment.