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