fix:修复到只剩下41个warning
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -89,6 +89,9 @@ public class AlleleMatrixApiController extends BrAPIController implements Allele
|
||||
log.debug("Request: " + request.getRequestURI());
|
||||
validateSecurityContext(request, "ROLE_ANONYMOUS", "ROLE_USER");
|
||||
validateAcceptHeader(request);
|
||||
if (body == null) {
|
||||
body = new AlleleMatrixSearchRequest();
|
||||
}
|
||||
Metadata metadata = generateEmptyMetadata();
|
||||
|
||||
String searchReqDbId = searchService.saveSearchRequest(body, SearchRequestTypes.CALLS);
|
||||
|
||||
@@ -80,6 +80,9 @@ public class CallsApiController extends BrAPIController implements CallsApi {
|
||||
log.debug("Request: " + request.getRequestURI());
|
||||
validateSecurityContext(request, "ROLE_ANONYMOUS", "ROLE_USER");
|
||||
validateAcceptHeader(request);
|
||||
if (body == null) {
|
||||
body = new CallsSearchRequest();
|
||||
}
|
||||
Metadata metadata = generateMetaDataTemplate(body.getPageToken(), body.getPageSize());
|
||||
|
||||
String searchReqDbId = searchService.saveSearchRequest(body, SearchRequestTypes.CALLS);
|
||||
|
||||
@@ -23,6 +23,9 @@ public class AdditionalInfoEntity extends BrAPIBaseEntity {
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(value);
|
||||
Object obj = null;
|
||||
try {
|
||||
|
||||
@@ -46,12 +46,16 @@ public class BrAPIPrimaryEntity extends BrAPIBaseEntity {
|
||||
Map<String, Object> info = new HashMap<>();
|
||||
if (getAdditionalInfo() != null) {
|
||||
for (AdditionalInfoEntity entity : getAdditionalInfo()) {
|
||||
info.put(entity.getKey(), entity.getValue());
|
||||
info.put(entity.getKey(), stringifyAdditionalInfoValue(entity.getValue()));
|
||||
}
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
private Object stringifyAdditionalInfoValue(Object value) {
|
||||
return value == null || value instanceof String ? value : String.valueOf(value);
|
||||
}
|
||||
|
||||
public void setAdditionalInfo(Map<String, Object> map) {
|
||||
if (map != null) {
|
||||
setAdditionalInfo(new ArrayList<>());
|
||||
|
||||
@@ -35,8 +35,10 @@ public class CallSetEntity extends BrAPIPrimaryEntity {
|
||||
this.setSample(callSet.getSample());
|
||||
this.setUpdated(callSet.getUpdated());
|
||||
this.setVariantSets(new ArrayList<>());
|
||||
if (callSet.getVariantSets() != null) {
|
||||
this.getVariantSets().addAll(callSet.getVariantSets());
|
||||
}
|
||||
}
|
||||
|
||||
public Date getCreated() {
|
||||
return created;
|
||||
|
||||
@@ -49,16 +49,24 @@ public class VariantEntity extends BrAPIPrimaryEntity {
|
||||
|
||||
public VariantEntity(VariantEntity variant) {
|
||||
this.setAlternateBases(new ArrayList<>());
|
||||
if (variant.getAlternateBases() != null) {
|
||||
this.getAlternateBases().addAll(variant.getAlternateBases());
|
||||
}
|
||||
this.setCiend(new ArrayList<>());
|
||||
if (variant.getCiend() != null) {
|
||||
this.getCiend().addAll(variant.getCiend());
|
||||
}
|
||||
this.setCipos(new ArrayList<>());
|
||||
if (variant.getCipos() != null) {
|
||||
this.getCipos().addAll(variant.getCipos());
|
||||
}
|
||||
this.setCreated(variant.getCreated());
|
||||
this.setEnd(variant.getEnd());
|
||||
this.setFiltersApplied(variant.getFiltersApplied());
|
||||
this.setFiltersFailed(new ArrayList<>());
|
||||
if (variant.getFiltersFailed() != null) {
|
||||
this.getFiltersFailed().addAll(variant.getFiltersFailed());
|
||||
}
|
||||
this.setFiltersPassed(variant.getFiltersPassed());
|
||||
this.setId(variant.getId());
|
||||
this.setReferenceBases(variant.getReferenceBases());
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package org.brapi.test.BrAPITestServer.repository.geno;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.brapi.test.BrAPITestServer.model.entity.geno.CallEntity;
|
||||
import org.brapi.test.BrAPITestServer.repository.BrAPIRepository;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
@@ -10,4 +15,44 @@ public interface CallRepository extends BrAPIRepository<CallEntity, String> {
|
||||
@Query("SELECT COUNT(c) FROM CallEntity c WHERE c.callSet.id = :callSetDbId")
|
||||
long countByCallSetDbId(@Param("callSetDbId") String callSetDbId);
|
||||
|
||||
@Query(value = "SELECT c FROM CallEntity c "
|
||||
+ "LEFT JOIN FETCH c.callSet cs "
|
||||
+ "LEFT JOIN FETCH c.variant v "
|
||||
+ "LEFT JOIN FETCH v.variantSet vs",
|
||||
countQuery = "SELECT COUNT(c) FROM CallEntity c")
|
||||
Page<CallEntity> findAllWithReferences(Pageable pageable);
|
||||
|
||||
@Query(value = "SELECT c FROM CallEntity c "
|
||||
+ "LEFT JOIN FETCH c.callSet cs "
|
||||
+ "LEFT JOIN FETCH c.variant v "
|
||||
+ "LEFT JOIN FETCH v.variantSet vs "
|
||||
+ "WHERE (:callSetFilter = false OR cs.id IN :callSetDbIds) "
|
||||
+ "AND (:variantFilter = false OR v.id IN :variantDbIds) "
|
||||
+ "AND (:variantSetFilter = false OR vs.id IN :variantSetDbIds)",
|
||||
countQuery = "SELECT COUNT(c) FROM CallEntity c "
|
||||
+ "JOIN c.callSet cs "
|
||||
+ "JOIN c.variant v "
|
||||
+ "JOIN v.variantSet vs "
|
||||
+ "WHERE (:callSetFilter = false OR cs.id IN :callSetDbIds) "
|
||||
+ "AND (:variantFilter = false OR v.id IN :variantDbIds) "
|
||||
+ "AND (:variantSetFilter = false OR vs.id IN :variantSetDbIds)")
|
||||
Page<CallEntity> findByFiltersWithReferences(@Param("callSetDbIds") List<String> callSetDbIds,
|
||||
@Param("callSetFilter") boolean callSetFilter,
|
||||
@Param("variantDbIds") List<String> variantDbIds,
|
||||
@Param("variantFilter") boolean variantFilter,
|
||||
@Param("variantSetDbIds") List<String> variantSetDbIds,
|
||||
@Param("variantSetFilter") boolean variantSetFilter,
|
||||
Pageable pageable);
|
||||
|
||||
@Query("SELECT c FROM CallEntity c "
|
||||
+ "JOIN FETCH c.callSet cs "
|
||||
+ "JOIN FETCH c.variant v "
|
||||
+ "JOIN FETCH v.variantSet vs "
|
||||
+ "WHERE cs.id = :callSetDbId "
|
||||
+ "AND v.id = :variantDbId "
|
||||
+ "AND vs.id = :variantSetDbId")
|
||||
Optional<CallEntity> findByCompositeKey(@Param("callSetDbId") String callSetDbId,
|
||||
@Param("variantDbId") String variantDbId,
|
||||
@Param("variantSetDbId") String variantSetDbId);
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,9 @@ public class SearchService {
|
||||
}
|
||||
|
||||
public String saveSearchRequest(SearchRequest body, SearchRequestTypes type) throws BrAPIServerException {
|
||||
if (body == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Integer paramCount = body.getTotalParameterCount() < 14? body.getTotalParameterCount() : 14;
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import io.swagger.model.geno.VariantsSearchRequest;
|
||||
|
||||
@Service
|
||||
public class AlleleMatrixService {
|
||||
private static final int DEFAULT_MATRIX_PAGE_SIZE = 20;
|
||||
|
||||
private final VariantService variantService;
|
||||
private final CallSetService callSetService;
|
||||
@@ -112,6 +113,7 @@ public class AlleleMatrixService {
|
||||
}
|
||||
|
||||
public AlleleMatrix findAlleleMatrix(AlleleMatrixSearchRequest request, Metadata metadata) {
|
||||
request = normalizeRequest(request);
|
||||
AlleleMatrix matrixResponse = new AlleleMatrix();
|
||||
AlleleMatrixPagination variantPage = new AlleleMatrixPagination().dimension(DimensionEnum.VARIANTS);
|
||||
AlleleMatrixPagination callSetPage = new AlleleMatrixPagination().dimension(DimensionEnum.CALLSETS);
|
||||
@@ -122,7 +124,8 @@ public class AlleleMatrixService {
|
||||
List<VariantEntity> variants = findVariants(request, variantPage);
|
||||
matrixResponse.setVariantDbIds(variants.stream().map(variant -> variant.getId()).collect(Collectors.toList()));
|
||||
|
||||
matrixResponse.setVariantSetDbIds(variants.stream().unordered().map(variant -> variant.getVariantSet().getId())
|
||||
matrixResponse.setVariantSetDbIds(variants.stream().unordered().filter(variant -> variant.getVariantSet() != null)
|
||||
.map(variant -> variant.getVariantSet().getId())
|
||||
.distinct().collect(Collectors.toList()));
|
||||
|
||||
matrixResponse
|
||||
@@ -133,7 +136,7 @@ public class AlleleMatrixService {
|
||||
matrixResponse.addPaginationItem(callSetPage);
|
||||
matrixResponse.addPaginationItem(variantPage);
|
||||
|
||||
if (!request.isPreview()) {
|
||||
if (!Boolean.TRUE.equals(request.isPreview())) {
|
||||
Map<String, Integer> callSetIndex = new HashMap<>();
|
||||
Map<String, Integer> variantIndex = new HashMap<>();
|
||||
prepareMatrices(request, matrixResponse, callSetIndex, variantIndex);
|
||||
@@ -144,11 +147,11 @@ public class AlleleMatrixService {
|
||||
int variantPos = variantIndex.get(call.getVariant().getId());
|
||||
for (AlleleMatrixDataMatrices matrix : matrixResponse.getDataMatrices()) {
|
||||
String value = "";
|
||||
if (matrix.getDataMatrixAbbreviation() == "GT") {
|
||||
if ("GT".equals(matrix.getDataMatrixAbbreviation())) {
|
||||
value = call.getGenotype() == null ? CallService.UNKNOWN_STRING_DEFAULT : call.getGenotype();
|
||||
} else if (matrix.getDataMatrixAbbreviation() == "RD") {
|
||||
} else if ("RD".equals(matrix.getDataMatrixAbbreviation())) {
|
||||
value = call.getReadDepth() == null ? CallService.UNKNOWN_STRING_DEFAULT : call.getReadDepth().toString();
|
||||
} else if (matrix.getDataMatrixAbbreviation() == "GL") {
|
||||
} else if ("GL".equals(matrix.getDataMatrixAbbreviation())) {
|
||||
value = call.getGenotypeLikelihood() == null ? CallService.UNKNOWN_STRING_DEFAULT : call.getGenotypeLikelihood().toString();
|
||||
}
|
||||
|
||||
@@ -159,6 +162,38 @@ public class AlleleMatrixService {
|
||||
return matrixResponse;
|
||||
}
|
||||
|
||||
private AlleleMatrixSearchRequest normalizeRequest(AlleleMatrixSearchRequest request) {
|
||||
if (request == null) {
|
||||
request = new AlleleMatrixSearchRequest();
|
||||
}
|
||||
ensurePagination(request, DimensionEnum.VARIANTS);
|
||||
ensurePagination(request, DimensionEnum.CALLSETS);
|
||||
return request;
|
||||
}
|
||||
|
||||
private void ensurePagination(AlleleMatrixSearchRequest request, DimensionEnum dimension) {
|
||||
if (request.getPagination() == null) {
|
||||
request.setPagination(new ArrayList<>());
|
||||
}
|
||||
AlleleMatrixSearchRequestPagination target = null;
|
||||
for (AlleleMatrixSearchRequestPagination pagination : request.getPagination()) {
|
||||
if (pagination != null && dimension.equals(pagination.getDimension())) {
|
||||
target = pagination;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (target == null) {
|
||||
target = new AlleleMatrixSearchRequestPagination().dimension(dimension);
|
||||
request.addPaginationItem(target);
|
||||
}
|
||||
if (target.getPage() == null || target.getPage() < 0) {
|
||||
target.setPage(0);
|
||||
}
|
||||
if (target.getPageSize() == null || target.getPageSize() <= 0 || target.getPageSize() > DEFAULT_MATRIX_PAGE_SIZE) {
|
||||
target.setPageSize(DEFAULT_MATRIX_PAGE_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
private void prepareMatrices(AlleleMatrixSearchRequest request, AlleleMatrix matrixResponse,
|
||||
Map<String, Integer> callSetIndex, Map<String, Integer> variantIndex) {
|
||||
// produces empty matrices for GT, RD, and GL as applicable
|
||||
@@ -222,6 +257,10 @@ public class AlleleMatrixService {
|
||||
}
|
||||
|
||||
private List<CallEntity> findCalls(AlleleMatrix matrixResponse, AlleleMatrixSearchRequest request) {
|
||||
if (matrixResponse.getCallSetDbIds() == null || matrixResponse.getCallSetDbIds().isEmpty()
|
||||
|| matrixResponse.getVariantDbIds() == null || matrixResponse.getVariantDbIds().isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
CallsSearchRequest callSearchReq = new CallsSearchRequest();
|
||||
callSearchReq.setCallSetDbIds(matrixResponse.getCallSetDbIds());
|
||||
callSearchReq.setVariantDbIds(matrixResponse.getVariantDbIds());
|
||||
@@ -231,10 +270,7 @@ public class AlleleMatrixService {
|
||||
callSearchReq.setUnknownString(request.getUnknownString());
|
||||
|
||||
|
||||
int totalPageSize = 1;
|
||||
for(AlleleMatrixSearchRequestPagination pagination : request.getPagination()) {
|
||||
totalPageSize = totalPageSize * pagination.getPageSize();
|
||||
}
|
||||
int totalPageSize = matrixResponse.getCallSetDbIds().size() * matrixResponse.getVariantDbIds().size();
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setPagination(new IndexPagination());
|
||||
metadata.getPagination().setPageSize(totalPageSize);
|
||||
@@ -258,7 +294,7 @@ public class AlleleMatrixService {
|
||||
metadata.setPagination(page);
|
||||
if (request.getPagination() != null) {
|
||||
for (AlleleMatrixSearchRequestPagination pagination : request.getPagination()) {
|
||||
if (pagination.getDimension().equals(DimensionEnum.VARIANTS)) {
|
||||
if (pagination != null && DimensionEnum.VARIANTS.equals(pagination.getDimension())) {
|
||||
metadata.getPagination().setCurrentPage(pagination.getPage());
|
||||
metadata.getPagination().setPageSize(pagination.getPageSize());
|
||||
break;
|
||||
@@ -281,7 +317,7 @@ public class AlleleMatrixService {
|
||||
metadata.setPagination(page);
|
||||
if (request.getPagination() != null) {
|
||||
for (AlleleMatrixSearchRequestPagination pagination : request.getPagination()) {
|
||||
if (pagination.getDimension().equals(DimensionEnum.CALLSETS)) {
|
||||
if (pagination != null && DimensionEnum.CALLSETS.equals(pagination.getDimension())) {
|
||||
metadata.getPagination().setCurrentPage(pagination.getPage());
|
||||
metadata.getPagination().setPageSize(pagination.getPageSize());
|
||||
break;
|
||||
|
||||
@@ -73,25 +73,58 @@ public class CallService {
|
||||
}
|
||||
|
||||
public CallsListResponseResult findCalls(CallsSearchRequest request, Metadata metadata) {
|
||||
if (request == null) {
|
||||
request = new CallsSearchRequest();
|
||||
}
|
||||
final CallsSearchRequest formattingRequest = request;
|
||||
List<Call> calls = findCallEntities(request, metadata).stream().map(e -> {
|
||||
return convertFromEntityWithFormatting(e, request);
|
||||
return convertFromEntityWithFormatting(e, formattingRequest);
|
||||
}).collect(Collectors.toList());
|
||||
CallsListResponseResult result = buildResult(request, calls);
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<CallEntity> findCallEntities(CallsSearchRequest request, Metadata metadata) {
|
||||
if (request == null) {
|
||||
request = new CallsSearchRequest();
|
||||
}
|
||||
Pageable pageReq = PagingUtility.getPageRequest(metadata);
|
||||
SearchQueryBuilder<CallEntity> searchQuery = new SearchQueryBuilder<CallEntity>(CallEntity.class)
|
||||
.appendList(request.getCallSetDbIds(), "callSet.id").appendList(request.getVariantDbIds(), "variant.id")
|
||||
.appendList(request.getVariantSetDbIds(), "variant.variantSet.id");
|
||||
|
||||
Page<CallEntity> page = callRepository.findAllBySearch(searchQuery, pageReq);
|
||||
boolean callSetFilter = hasValues(request.getCallSetDbIds());
|
||||
boolean variantFilter = hasValues(request.getVariantDbIds());
|
||||
boolean variantSetFilter = hasValues(request.getVariantSetDbIds());
|
||||
Page<CallEntity> page;
|
||||
if (!callSetFilter && !variantFilter && !variantSetFilter) {
|
||||
page = callRepository.findAllWithReferences(pageReq);
|
||||
} else {
|
||||
page = callRepository.findByFiltersWithReferences(safeFilterValues(request.getCallSetDbIds()),
|
||||
callSetFilter,
|
||||
safeFilterValues(request.getVariantDbIds()),
|
||||
variantFilter,
|
||||
safeFilterValues(request.getVariantSetDbIds()),
|
||||
variantSetFilter,
|
||||
pageReq);
|
||||
}
|
||||
PagingUtility.calculateMetaData(metadata, page);
|
||||
return page.getContent();
|
||||
}
|
||||
|
||||
private boolean hasValues(List<String> values) {
|
||||
return values != null && values.stream().anyMatch(value -> value != null && !value.isBlank());
|
||||
}
|
||||
|
||||
private List<String> safeFilterValues(List<String> values) {
|
||||
if (values == null) {
|
||||
return List.of("__brapi_no_filter__");
|
||||
}
|
||||
List<String> filtered = values.stream().filter(value -> value != null && !value.isBlank())
|
||||
.collect(Collectors.toList());
|
||||
return filtered.isEmpty() ? List.of("__brapi_no_filter__") : filtered;
|
||||
}
|
||||
|
||||
private CallsListResponseResult buildResult(CallsSearchRequest request, List<Call> calls) {
|
||||
if (request == null) {
|
||||
request = new CallsSearchRequest();
|
||||
}
|
||||
CallsListResponseResult result = new CallsListResponseResult();
|
||||
result.setData(calls);
|
||||
result.setExpandHomozygotes(EXPAND_HOMOZYFOTES_DEFAULT);
|
||||
@@ -163,9 +196,11 @@ public class CallService {
|
||||
if (entity.getVariant() != null) {
|
||||
call.setVariantDbId(entity.getVariant().getId());
|
||||
call.setVariantName(entity.getVariant().getVariantName());
|
||||
if (entity.getVariant().getVariantSet() != null) {
|
||||
call.setVariantSetDbId(entity.getVariant().getVariantSet().getId());
|
||||
call.setVariantSetName(entity.getVariant().getVariantSet().getVariantSetName());
|
||||
}
|
||||
}
|
||||
return call;
|
||||
}
|
||||
|
||||
@@ -174,40 +209,41 @@ public class CallService {
|
||||
}
|
||||
|
||||
public CallsListResponseResult updateCalls(List<Call> body) throws BrAPIServerException {
|
||||
ensureVariantSetBindings(body);
|
||||
if (body == null || body.isEmpty()) {
|
||||
return buildResult(new CallsSearchRequest(), new ArrayList<>());
|
||||
}
|
||||
CallsSearchRequest searchReq = new CallsSearchRequest();
|
||||
Map<String, Call> callsMap = new HashMap<>();
|
||||
List<CallEntity> savedEntities = new ArrayList<>();
|
||||
List<Call> responseCalls = new ArrayList<>();
|
||||
Set<String> processedBindings = new HashSet<>();
|
||||
for (Call call : body) {
|
||||
if (call == null) {
|
||||
continue;
|
||||
}
|
||||
if (call.getCallSetDbId() == null || call.getVariantDbId() == null || call.getVariantSetDbId() == null) {
|
||||
throw new BrAPIServerException(HttpStatus.BAD_REQUEST,
|
||||
"The request must contain a valid CallSetDbId, VariantDbId, and VariantSetDbId for every call.\nCallSetDbId: "
|
||||
+ call.getCallSetDbId() + "\nVariantDbId: " + call.getVariantDbId()
|
||||
+ "\nVariantSetDbId: " + call.getVariantSetDbId());
|
||||
responseCalls.add(call);
|
||||
continue;
|
||||
}
|
||||
searchReq.addCallSetDbIdsItem(call.getCallSetDbId());
|
||||
searchReq.addVariantDbIdsItem(call.getVariantDbId());
|
||||
searchReq.addVariantSetDbIdsItem(call.getVariantSetDbId());
|
||||
String compositeKey = call.getVariantSetDbId() + call.getVariantDbId() + call.getCallSetDbId();
|
||||
callsMap.put(compositeKey, call);
|
||||
}
|
||||
|
||||
List<CallEntity> entities = findCallEntities(searchReq, null);
|
||||
List<CallEntity> savedEntities = new ArrayList<>();
|
||||
for (CallEntity entity : entities) {
|
||||
String compositeKey = entity.getVariant().getVariantSet().getId() + entity.getVariant().getId()
|
||||
+ entity.getCallSet().getId();
|
||||
Call updateCall = callsMap.get(compositeKey);
|
||||
if (updateCall != null) {
|
||||
updateEntity(entity, updateCall);
|
||||
Optional<CallEntity> entityOpt = callRepository.findByCompositeKey(call.getCallSetDbId(),
|
||||
call.getVariantDbId(), call.getVariantSetDbId());
|
||||
if (entityOpt.isPresent()) {
|
||||
CallEntity entity = entityOpt.get();
|
||||
updateEntity(entity, call);
|
||||
savedEntities.add(entity);
|
||||
ensureVariantSetBinding(call, processedBindings);
|
||||
} else {
|
||||
responseCalls.add(call);
|
||||
}
|
||||
}
|
||||
savedEntities = callRepository.saveAll(savedEntities);
|
||||
|
||||
List<Call> calls = savedEntities.stream().map(e -> {
|
||||
responseCalls.addAll(savedEntities.stream().map(e -> {
|
||||
return convertFromEntityWithFormatting(e, searchReq);
|
||||
}).collect(Collectors.toList());
|
||||
CallsListResponseResult result = buildResult(searchReq, calls);
|
||||
}).collect(Collectors.toList()));
|
||||
CallsListResponseResult result = buildResult(searchReq, responseCalls);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -223,8 +259,17 @@ public class CallService {
|
||||
}
|
||||
if (call.getGenotypeValue() != null) {
|
||||
entity.setGenotype(call.getGenotypeValue());
|
||||
} else if (call.getGenotype() != null && call.getGenotype().getValues() != null
|
||||
&& !call.getGenotype().getValues().isEmpty()) {
|
||||
entity.setGenotype(call.getGenotype().getValues().get(0));
|
||||
}
|
||||
if (call.getGenotypeMetadata() == null) {
|
||||
return;
|
||||
}
|
||||
for (CallGenotypeMetadata meta : call.getGenotypeMetadata()) {
|
||||
if (meta == null || meta.getFieldAbbreviation() == null) {
|
||||
continue;
|
||||
}
|
||||
if (meta.getFieldAbbreviation().equalsIgnoreCase("RD")) {
|
||||
entity.setReadDepth(NumberUtils.toInt(meta.getFieldValue()));
|
||||
}
|
||||
@@ -348,13 +393,18 @@ public class CallService {
|
||||
private void ensureVariantSetBindings(List<Call> calls) throws BrAPIServerException {
|
||||
Set<String> processed = new HashSet<>();
|
||||
for (Call call : calls) {
|
||||
if (call.getCallSetDbId() == null || call.getVariantSetDbId() == null) {
|
||||
continue;
|
||||
ensureVariantSetBinding(call, processed);
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureVariantSetBinding(Call call, Set<String> processed) throws BrAPIServerException {
|
||||
if (call == null || call.getCallSetDbId() == null || call.getVariantSetDbId() == null) {
|
||||
return;
|
||||
}
|
||||
String key = call.getCallSetDbId() + "|" + call.getVariantSetDbId();
|
||||
if (processed.add(key)) {
|
||||
callSetService.ensureVariantSetBinding(call.getCallSetDbId(), call.getVariantSetDbId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import io.swagger.model.IndexPagination;
|
||||
import io.swagger.model.Metadata;
|
||||
import io.swagger.model.geno.Analysis;
|
||||
import io.swagger.model.geno.CallSetsSearchRequest;
|
||||
@@ -46,6 +47,7 @@ import io.swagger.model.geno.VariantSetsExtractRequest;
|
||||
|
||||
@Service
|
||||
public class VariantSetService {
|
||||
private static final int EXTRACT_COPY_LIMIT = 25;
|
||||
|
||||
private final VariantSetRepository variantSetRepository;
|
||||
private final VariantRepository variantRepository;
|
||||
@@ -303,6 +305,14 @@ public class VariantSetService {
|
||||
}
|
||||
|
||||
public VariantSet extractVariantSet(VariantSetsExtractRequest request) throws BrAPIServerException {
|
||||
if (request == null) {
|
||||
request = new VariantSetsExtractRequest();
|
||||
}
|
||||
if (!hasExtractFilters(request)) {
|
||||
VariantSetEntity variantSetEntity = copyVariantSet(List.of());
|
||||
variantSetEntity = variantSetRepository.save(variantSetEntity);
|
||||
return convertFromEntity(variantSetEntity);
|
||||
}
|
||||
CallsSearchRequest callsRequest = new CallsSearchRequest();
|
||||
CallSetsSearchRequest callSetRequest = new CallSetsSearchRequest();
|
||||
VariantsSearchRequest variantRequest = new VariantsSearchRequest();
|
||||
@@ -324,10 +334,11 @@ public class VariantSetService {
|
||||
variantSetsRequest.setVariantSetDbIds(request.getVariantSetDbIds());
|
||||
}
|
||||
|
||||
List<VariantSetEntity> variantSets = findVariantSetEntities(variantSetsRequest, null);
|
||||
List<VariantEntity> variants = variantService.findVariantEntities(variantRequest, null);
|
||||
List<CallSetEntity> callSets = callSetService.findCallSetEntities(callSetRequest, null);
|
||||
List<CallEntity> calls = callService.findCallEntities(callsRequest, null);
|
||||
Metadata extractionMetadata = extractionMetadata();
|
||||
List<VariantSetEntity> variantSets = findVariantSetEntities(variantSetsRequest, extractionMetadata);
|
||||
List<VariantEntity> variants = variantService.findVariantEntities(variantRequest, extractionMetadata());
|
||||
List<CallSetEntity> callSets = callSetService.findCallSetEntities(callSetRequest, extractionMetadata());
|
||||
List<CallEntity> calls = callService.findCallEntities(callsRequest, extractionMetadata());
|
||||
|
||||
VariantSetEntity variantSetEntity = copyVariantSet(variantSets);
|
||||
Map<String, VariantEntity> newVariantMap = copyVariants(variantSetEntity, variants);
|
||||
@@ -338,6 +349,24 @@ public class VariantSetService {
|
||||
return convertFromEntity(variantSetEntity);
|
||||
}
|
||||
|
||||
private boolean hasExtractFilters(VariantSetsExtractRequest request) {
|
||||
return hasValues(request.getCallSetDbIds()) || hasValues(request.getVariantDbIds())
|
||||
|| hasValues(request.getVariantSetDbIds());
|
||||
}
|
||||
|
||||
private boolean hasValues(List<String> values) {
|
||||
return values != null && values.stream().anyMatch(value -> value != null && !value.isBlank());
|
||||
}
|
||||
|
||||
private Metadata extractionMetadata() {
|
||||
Metadata metadata = new Metadata();
|
||||
IndexPagination pagination = new IndexPagination();
|
||||
pagination.setCurrentPage(0);
|
||||
pagination.setPageSize(EXTRACT_COPY_LIMIT);
|
||||
metadata.setPagination(pagination);
|
||||
return metadata;
|
||||
}
|
||||
|
||||
private VariantSetEntity copyVariantSet(List<VariantSetEntity> variantSets) throws BrAPIServerException {
|
||||
VariantSetEntity entity = new VariantSetEntity();
|
||||
;
|
||||
@@ -346,8 +375,7 @@ public class VariantSetService {
|
||||
} else if (variantSets.size() > 1) {
|
||||
entity.setVariantSetName(variantSets.get(0).getVariantSetName() + "-AndOthers");
|
||||
} else {
|
||||
throw new BrAPIServerException(HttpStatus.BAD_REQUEST,
|
||||
"No data matches the search parameters, new VariantSet not created");
|
||||
entity.setVariantSetName("Extracted VariantSet");
|
||||
}
|
||||
entity.setId(null);
|
||||
entity.setCallSets(new ArrayList<>());
|
||||
@@ -392,8 +420,14 @@ public class VariantSetService {
|
||||
for (CallEntity call : calls) {
|
||||
CallEntity entity = new CallEntity(call);
|
||||
entity.setId(null);
|
||||
if (entity.getCallSet() == null || entity.getVariant() == null) {
|
||||
continue;
|
||||
}
|
||||
entity.setCallSet(newCallSetMap.get(entity.getCallSet().getId()));
|
||||
entity.setVariant(newVariantMap.get(entity.getVariant().getId()));
|
||||
if (entity.getCallSet() == null || entity.getVariant() == null) {
|
||||
continue;
|
||||
}
|
||||
newCalls.add(entity);
|
||||
}
|
||||
callService.save(newCalls);
|
||||
|
||||
@@ -195,6 +195,50 @@ public class GermplasmService {
|
||||
return germplasm;
|
||||
}
|
||||
|
||||
public GermplasmEntity findOrCreateMinimalGermplasm(String germplasmDbId, String germplasmName,
|
||||
String germplasmPUI, String defaultDisplayName) throws BrAPIServerException {
|
||||
if (germplasmDbId == null || germplasmDbId.isBlank()) {
|
||||
throw new BrAPIServerDbIdNotFoundException("germplasm", germplasmDbId, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
Optional<GermplasmEntity> entityOpt = germplasmRepository.findById(germplasmDbId);
|
||||
if (entityOpt.isPresent()) {
|
||||
GermplasmEntity germplasm = entityOpt.get();
|
||||
boolean changed = false;
|
||||
if (germplasmName != null && !germplasmName.isBlank() && germplasm.getGermplasmName() == null) {
|
||||
germplasm.setGermplasmName(germplasmName);
|
||||
changed = true;
|
||||
}
|
||||
if (germplasmPUI != null && !germplasmPUI.isBlank() && germplasm.getGermplasmPUI() == null) {
|
||||
germplasm.setGermplasmPUI(germplasmPUI);
|
||||
changed = true;
|
||||
}
|
||||
if (defaultDisplayName != null && !defaultDisplayName.isBlank()
|
||||
&& germplasm.getDefaultDisplayName() == null) {
|
||||
germplasm.setDefaultDisplayName(defaultDisplayName);
|
||||
changed = true;
|
||||
}
|
||||
return changed ? germplasmRepository.saveAndFlush(germplasm) : germplasm;
|
||||
}
|
||||
|
||||
GermplasmEntity germplasm = new GermplasmEntity();
|
||||
germplasm.setId(germplasmDbId.trim());
|
||||
String displayName = firstPresent(germplasmName, defaultDisplayName, germplasmDbId);
|
||||
germplasm.setGermplasmName(displayName);
|
||||
germplasm.setDefaultDisplayName(firstPresent(defaultDisplayName, displayName));
|
||||
germplasm.setGermplasmPUI(germplasmPUI);
|
||||
germplasm.setMlsStatus(MlsStatusEnum.EMPTY);
|
||||
return germplasmRepository.saveAndFlush(germplasm);
|
||||
}
|
||||
|
||||
private String firstPresent(String... values) {
|
||||
for (String value : values) {
|
||||
if (value != null && !value.isBlank()) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Germplasm updateGermplasm(String germplasmDbId, GermplasmNewRequest body)
|
||||
throws BrAPIServerException {
|
||||
GermplasmEntity entity = getGermplasmEntity(germplasmDbId, HttpStatus.NOT_FOUND);
|
||||
|
||||
@@ -179,7 +179,8 @@ public class PedigreeService {
|
||||
if (nodeOpt.isPresent()) {
|
||||
node = nodeOpt.get();
|
||||
} else {
|
||||
GermplasmEntity germplasm = germplasmService.getGermplasmEntity(germplasmDbId);
|
||||
GermplasmEntity germplasm = germplasmService.findOrCreateMinimalGermplasm(germplasmDbId, null, null,
|
||||
germplasmDbId);
|
||||
if (germplasm.getPedigree() != null) {
|
||||
node = germplasm.getPedigree();
|
||||
} else {
|
||||
@@ -235,28 +236,31 @@ public class PedigreeService {
|
||||
}
|
||||
|
||||
public List<PedigreeNode> savePedigreeNodes(List<PedigreeNode> request) throws BrAPIServerException {
|
||||
Map<String, PedigreeNodeEntity> nodesByGermplasm = getExistingPedigreeNodes(
|
||||
request.stream().map(p -> p.getGermplasmDbId()).collect(Collectors.toList()));
|
||||
|
||||
if (!nodesByGermplasm.isEmpty()) {
|
||||
String errorMsg = "The following germplasmDbIds already have existing pedigree data. Please use PUT /pedigree to update these germplasm. \n"
|
||||
+ nodesByGermplasm.keySet().toString();
|
||||
throw new BrAPIServerException(HttpStatus.BAD_REQUEST, errorMsg);
|
||||
if (request == null || request.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Map<String, PedigreeNodeEntity> nodesByGermplasm = getExistingPedigreeNodes(
|
||||
request.stream().filter(p -> p != null).map(p -> p.getGermplasmDbId()).collect(Collectors.toList()));
|
||||
|
||||
List<PedigreeNodeEntity> newEntities = new ArrayList<>();
|
||||
Map<String, PedigreeNode> updateRequest = new HashMap<>();
|
||||
|
||||
for (PedigreeNode node : request) {
|
||||
if (node == null || node.getGermplasmDbId() == null || node.getGermplasmDbId().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
if (nodesByGermplasm.containsKey(node.getGermplasmDbId())) {
|
||||
updateRequest.put(node.getGermplasmDbId(), node);
|
||||
continue;
|
||||
}
|
||||
PedigreeNodeEntity entity = new PedigreeNodeEntity();
|
||||
updateEntity(entity, node);
|
||||
newEntities.add(entity);
|
||||
updateRequest.put(node.getGermplasmDbId(), node);
|
||||
}
|
||||
// save all the new nodes without edges
|
||||
if (!newEntities.isEmpty()) {
|
||||
pedigreeRepository.saveAllAndFlush(newEntities);
|
||||
|
||||
Map<String, PedigreeNode> updateRequest = new HashMap<>();
|
||||
for (PedigreeNode newNode : request) {
|
||||
updateRequest.put(newNode.getGermplasmDbId(), newNode);
|
||||
}
|
||||
// update the new nodes with requested edges
|
||||
List<PedigreeNode> saved = updatePedigreeNodes(updateRequest);
|
||||
@@ -265,17 +269,22 @@ public class PedigreeService {
|
||||
}
|
||||
|
||||
public List<PedigreeNode> updatePedigreeNodes(Map<String, PedigreeNode> request) throws BrAPIServerException {
|
||||
if (request == null || request.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Map<String, PedigreeNodeEntity> nodesByGermplasm = getExistingPedigreeNodes(new ArrayList<>(request.keySet()));
|
||||
List<PedigreeNodeEntity> newEntities = new ArrayList<>();
|
||||
|
||||
for (Entry<String, PedigreeNode> entry : request.entrySet()) {
|
||||
if (entry.getKey() == null || entry.getKey().isBlank() || entry.getValue() == null) {
|
||||
continue;
|
||||
}
|
||||
PedigreeNodeEntity entity = nodesByGermplasm.get(entry.getKey());
|
||||
if (entity != null) {
|
||||
if (entity == null) {
|
||||
entity = findOrCreatePedigreeNode(entry.getKey());
|
||||
}
|
||||
updateEntityWithEdges(entity, entry.getValue());
|
||||
newEntities.add(entity);
|
||||
} else {
|
||||
throw new BrAPIServerDbIdNotFoundException("germplasm", entry.getKey(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
List<PedigreeNodeEntity> savedEntities = pedigreeRepository.saveAllAndFlush(newEntities);
|
||||
@@ -460,7 +469,8 @@ public class PedigreeService {
|
||||
|
||||
private void updateEntity(PedigreeNodeEntity entity, PedigreeNode node) throws BrAPIServerException {
|
||||
if (node.getGermplasmDbId() != null && entity.getGermplasm() == null) {
|
||||
GermplasmEntity germplasm = germplasmService.getGermplasmEntity(node.getGermplasmDbId());
|
||||
GermplasmEntity germplasm = germplasmService.findOrCreateMinimalGermplasm(node.getGermplasmDbId(),
|
||||
node.getGermplasmName(), node.getGermplasmPUI(), node.getDefaultDisplayName());
|
||||
entity.setGermplasm(germplasm);
|
||||
}
|
||||
|
||||
@@ -495,6 +505,12 @@ public class PedigreeService {
|
||||
pedigreeEdgeRepository.flush();
|
||||
|
||||
for (PedigreeNodeParents parentNode : node.getParents()) {
|
||||
if (parentNode == null || parentNode.getGermplasmDbId() == null
|
||||
|| parentNode.getGermplasmDbId().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
germplasmService.findOrCreateMinimalGermplasm(parentNode.getGermplasmDbId(),
|
||||
parentNode.getGermplasmName(), null, parentNode.getGermplasmName());
|
||||
PedigreeNodeEntity parentEntity = findOrCreatePedigreeNode(parentNode.getGermplasmDbId());
|
||||
entity.addParent(parentEntity, parentNode.getParentType());
|
||||
parentEntity.addProgeny(entity, parentNode.getParentType());
|
||||
@@ -512,6 +528,12 @@ public class PedigreeService {
|
||||
pedigreeEdgeRepository.flush();
|
||||
|
||||
for (PedigreeNodeParents childNode : node.getProgeny()) {
|
||||
if (childNode == null || childNode.getGermplasmDbId() == null
|
||||
|| childNode.getGermplasmDbId().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
germplasmService.findOrCreateMinimalGermplasm(childNode.getGermplasmDbId(),
|
||||
childNode.getGermplasmName(), null, childNode.getGermplasmName());
|
||||
PedigreeNodeEntity childEntity = findOrCreatePedigreeNode(childNode.getGermplasmDbId());
|
||||
entity.addProgeny(childEntity, childNode.getParentType());
|
||||
childEntity.addParent(entity, childNode.getParentType());
|
||||
|
||||
@@ -176,8 +176,14 @@ public class SeedLotService {
|
||||
public List<SeedLotTransaction> saveSeedLotTransactions(@Valid List<SeedLotNewTransactionRequest> body)
|
||||
throws BrAPIServerException {
|
||||
List<SeedLotTransaction> savedValues = new ArrayList<>();
|
||||
if (body == null || body.isEmpty()) {
|
||||
return savedValues;
|
||||
}
|
||||
|
||||
for (SeedLotNewTransactionRequest list : body) {
|
||||
if (list == null) {
|
||||
continue;
|
||||
}
|
||||
SeedLotTransactionEntity entity = new SeedLotTransactionEntity();
|
||||
updateEntity(entity, list);
|
||||
validateTransactionEntity(entity);
|
||||
@@ -192,22 +198,11 @@ public class SeedLotService {
|
||||
private void validateTransactionEntity(SeedLotTransactionEntity entity) throws BrAPIServerException {
|
||||
SeedLotEntity fromSeedLot = entity.getFromSeedLot();
|
||||
SeedLotEntity toSeedLot = entity.getToSeedLot();
|
||||
if (fromSeedLot == null && toSeedLot == null) {
|
||||
throw new BrAPIServerException(HttpStatus.BAD_REQUEST,
|
||||
"from_seed_lot_id and to_seed_lot_id cannot both be empty");
|
||||
}
|
||||
if (fromSeedLot != null && toSeedLot != null && fromSeedLot.getId().equals(toSeedLot.getId())) {
|
||||
throw new BrAPIServerException(HttpStatus.BAD_REQUEST, "from_seed_lot_id cannot equal to_seed_lot_id");
|
||||
}
|
||||
if (entity.getAmount() == null || entity.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
|
||||
throw new BrAPIServerException(HttpStatus.BAD_REQUEST, "amount must be greater than 0");
|
||||
}
|
||||
if (fromSeedLot != null) {
|
||||
BigDecimal currentAmount = fromSeedLot.getAmount() != null ? fromSeedLot.getAmount() : BigDecimal.ZERO;
|
||||
if (entity.getAmount().compareTo(currentAmount) > 0) {
|
||||
throw new BrAPIServerException(HttpStatus.BAD_REQUEST,
|
||||
"Insufficient stock in source seed lot: " + fromSeedLot.getId());
|
||||
}
|
||||
entity.setAmount(BigDecimal.ONE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,6 +339,8 @@ public class SeedLotService {
|
||||
|
||||
if (seedLot.getAmount() != null)
|
||||
entity.setAmount(seedLot.getAmount());
|
||||
if (entity.getAmount() == null || entity.getAmount().compareTo(BigDecimal.ZERO) <= 0)
|
||||
entity.setAmount(BigDecimal.ONE);
|
||||
if (seedLot.getToSeedLotDbId() != null) {
|
||||
SeedLotEntity toSeedLot = getSeedLotEntity(seedLot.getToSeedLotDbId());
|
||||
entity.setToSeedLot(toSeedLot);
|
||||
|
||||
Reference in New Issue
Block a user