-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from fscheel/AddQualityMetricCalculation
Add quality metric calculation
- Loading branch information
Showing
10 changed files
with
266 additions
and
21 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
package org.nmdp.hfcus.dao; | ||
|
||
import org.nmdp.hfcus.model.HFCuration; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.stream.Stream; | ||
|
||
public interface HFCurationRepository extends CrudRepository<HFCuration, Long> { | ||
Iterable<HFCuration> findBypopulationDataId(Long populationId); | ||
|
||
@Query("select h from HFCuration h") | ||
Stream<HFCuration> findAllStreamable(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.nmdp.hfcus.quality; | ||
|
||
import org.nmdp.hfcus.model.HFCuration; | ||
import org.nmdp.hfcus.model.HaplotypeFrequency; | ||
import org.nmdp.hfcus.model.Quality; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
@Component | ||
public class Div50Calculator implements IQualityMetricCalculator { | ||
@Override | ||
public boolean calculationNeeded(HFCuration frequency) { | ||
List<Quality> qualityList = frequency.getHaplotypeFrequencyData().getQualityList(); | ||
if (qualityList == null){ | ||
return true; | ||
} | ||
return qualityList | ||
.stream() | ||
.noneMatch(quality -> quality.getTypeOfQuality() == io.swagger.model.Quality.TypeOfQualityEnum.DIV_50); | ||
} | ||
|
||
@Override | ||
public void calculateMetric(HFCuration curation) { | ||
List<HaplotypeFrequency> frequencyList = curation.getHaplotypeFrequencyData().getFrequencyList(); | ||
AtomicInteger result = new AtomicInteger(); | ||
AtomicReference<Double> frequencies = new AtomicReference<>((double) 0); | ||
frequencyList | ||
.stream() | ||
.sorted(Comparator.comparing(HaplotypeFrequency::getFrequency).reversed()) | ||
.forEachOrdered(frequency -> { | ||
if (frequencies.get() < 0.5){ | ||
frequencies.updateAndGet(currentValue -> currentValue + frequency.getFrequency()); | ||
result.getAndIncrement(); | ||
} | ||
}); | ||
List<Quality> qualities = curation.getHaplotypeFrequencyData().getQualityList(); | ||
if (qualities == null){ | ||
qualities = new LinkedList<Quality>(); | ||
curation.getHaplotypeFrequencyData().setQualityList(qualities); | ||
} | ||
Quality newQuality = new Quality(); | ||
newQuality.setValue(result.doubleValue()); | ||
newQuality.setTypeOfQuality(io.swagger.model.Quality.TypeOfQualityEnum.DIV_50); | ||
qualities.add(newQuality); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/org/nmdp/hfcus/quality/IQualityMetricCalculator.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,10 @@ | ||
package org.nmdp.hfcus.quality; | ||
|
||
import org.nmdp.hfcus.model.HFCuration; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public interface IQualityMetricCalculator { | ||
boolean calculationNeeded(HFCuration frequency); | ||
void calculateMetric(HFCuration frequency); | ||
} |
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,68 @@ | ||
package org.nmdp.hfcus.quality; | ||
|
||
import org.nmdp.hfcus.dao.HFCurationRepository; | ||
import org.nmdp.hfcus.model.HFCuration; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.stream.Stream; | ||
|
||
@Service | ||
@EnableAsync | ||
public class QualityService { | ||
|
||
private static final LinkedBlockingQueue<Long> queue = new LinkedBlockingQueue<>(); | ||
private static Logger logger = LoggerFactory.getLogger(QualityService.class); | ||
private List<IQualityMetricCalculator> metricCalculators; | ||
private HFCurationRepository curationRepository; | ||
private QualityWorker qualityWorker; | ||
|
||
@Autowired | ||
public QualityService( | ||
List<IQualityMetricCalculator> metricCalculators, | ||
HFCurationRepository curationRepository, | ||
QualityWorker qualityWorker | ||
) { | ||
this.metricCalculators = metricCalculators; | ||
this.curationRepository = curationRepository; | ||
this.qualityWorker = qualityWorker; | ||
} | ||
|
||
@Async | ||
public void run() throws InterruptedException { | ||
try (Stream<HFCuration> stream = curationRepository.findAllStreamable()) { | ||
stream | ||
.filter( | ||
hfCuration -> | ||
hfCuration.getHaplotypeFrequencyData().getNumberOfCalculatedQualities() < metricCalculators.size() | ||
) | ||
.forEach(hfCuration -> { | ||
try { | ||
queue.put(hfCuration.getId()); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
}); | ||
} | ||
if (Thread.currentThread().isInterrupted()) { | ||
throw new InterruptedException(); | ||
} | ||
while (true) { | ||
Long nextUp = queue.take(); | ||
for (IQualityMetricCalculator calculator : metricCalculators) { | ||
qualityWorker.handleSingleMetric(nextUp, calculator); | ||
} | ||
Thread.sleep(5000); | ||
} | ||
} | ||
|
||
public void addToQueue(HFCuration value) throws InterruptedException { | ||
queue.put(value.getId()); | ||
} | ||
} |
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,35 @@ | ||
package org.nmdp.hfcus.quality; | ||
|
||
import org.nmdp.hfcus.dao.HFCurationRepository; | ||
import org.nmdp.hfcus.model.HFCuration; | ||
import org.nmdp.hfcus.model.HaplotypeFrequencySet; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
public class QualityWorker { | ||
|
||
|
||
private HFCurationRepository curationRepository; | ||
|
||
@Autowired | ||
public QualityWorker( | ||
HFCurationRepository curationRepository | ||
) { | ||
this.curationRepository = curationRepository; | ||
} | ||
|
||
@Transactional | ||
public void handleSingleMetric(Long nextUp, IQualityMetricCalculator calculator) { | ||
HFCuration curation = curationRepository.findOne(nextUp); | ||
if (calculator.calculationNeeded(curation)) { | ||
calculator.calculateMetric(curation); | ||
HaplotypeFrequencySet freqData = curation.getHaplotypeFrequencyData(); | ||
freqData.setNumberOfCalculatedQualities(freqData.getNumberOfCalculatedQualities() + 1); | ||
curationRepository.save(curation); | ||
} | ||
} | ||
} |
Oops, something went wrong.