diff --git a/src/main/java/uk/ac/ebi/eva/contigalias/datasource/NCBIAssemblyDataSource.java b/src/main/java/uk/ac/ebi/eva/contigalias/datasource/NCBIAssemblyDataSource.java index f798dd66..b6438d75 100644 --- a/src/main/java/uk/ac/ebi/eva/contigalias/datasource/NCBIAssemblyDataSource.java +++ b/src/main/java/uk/ac/ebi/eva/contigalias/datasource/NCBIAssemblyDataSource.java @@ -34,7 +34,6 @@ import uk.ac.ebi.eva.contigalias.repo.AssemblyRepository; import uk.ac.ebi.eva.contigalias.repo.ChromosomeRepository; -import javax.transaction.Transactional; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileReader; @@ -165,7 +164,6 @@ public Optional downloadAssemblyReport(String accession, NCBIBrowser ncbiB } } - @Transactional public void parseFileAndInsertAssembly(String accession, ENAAssemblyDataSource enaDataSource, AssemblyRepository assemblyRepository, ChromosomeRepository chromosomeRepository) throws IOException { Optional downloadNCBIFilePathOpt = downloadAssemblyReport(accession); diff --git a/src/main/java/uk/ac/ebi/eva/contigalias/exception/AssemblyIngestionException.java b/src/main/java/uk/ac/ebi/eva/contigalias/exception/AssemblyIngestionException.java new file mode 100644 index 00000000..c60b42ac --- /dev/null +++ b/src/main/java/uk/ac/ebi/eva/contigalias/exception/AssemblyIngestionException.java @@ -0,0 +1,8 @@ +package uk.ac.ebi.eva.contigalias.exception; + +public class AssemblyIngestionException extends RuntimeException { + + public AssemblyIngestionException(String accession) { + super("Error Ingesting assembly with accession " + accession); + } +} diff --git a/src/main/java/uk/ac/ebi/eva/contigalias/exception/ControllerExceptionHandler.java b/src/main/java/uk/ac/ebi/eva/contigalias/exception/ControllerExceptionHandler.java index 93d5c868..2d6c0d3b 100644 --- a/src/main/java/uk/ac/ebi/eva/contigalias/exception/ControllerExceptionHandler.java +++ b/src/main/java/uk/ac/ebi/eva/contigalias/exception/ControllerExceptionHandler.java @@ -30,4 +30,9 @@ public ResponseEntity handleExceptions(DownloadFailedException exception return new ResponseEntity<>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } + @ExceptionHandler(AssemblyIngestionException.class) + public ResponseEntity handleExceptions(AssemblyIngestionException exception, WebRequest webRequest){ + return new ResponseEntity<>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } + } diff --git a/src/main/java/uk/ac/ebi/eva/contigalias/repo/AssemblyRepository.java b/src/main/java/uk/ac/ebi/eva/contigalias/repo/AssemblyRepository.java index 9ffe65d8..62881539 100644 --- a/src/main/java/uk/ac/ebi/eva/contigalias/repo/AssemblyRepository.java +++ b/src/main/java/uk/ac/ebi/eva/contigalias/repo/AssemblyRepository.java @@ -20,6 +20,9 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import uk.ac.ebi.eva.contigalias.entities.AssemblyEntity; @@ -46,8 +49,17 @@ default Optional findAssemblyEntityByAccession(String accession) Page findAssemblyEntitiesByTaxid(long taxid, Pageable pageable); @Transactional - void deleteAssemblyEntityByInsdcAccession(String insdcAccession); + @Modifying + @Query("DELETE FROM AssemblyEntity a WHERE a.insdcAccession=:asmInsdcAccession") + void deleteAssemblyEntityByInsdcAccession(@Param("asmInsdcAccession") String asmInsdcAccession); @Transactional - void deleteAssemblyEntityByRefseq(String refseq); + @Modifying + @Query("DELETE FROM AssemblyEntity a WHERE a.refseq=:asmRefSeq") + void deleteAssemblyEntityByRefseq(@Param("asmRefSeq") String asmRefSeq); + + @Transactional + @Modifying + @Query("DELETE FROM AssemblyEntity a WHERE a.insdcAccession=:asmAccession OR a.refseq=:asmAccession") + void deleteAssemblyEntityByInsdcAccessionOrRefseq(@Param("asmAccession") String asmAccession); } diff --git a/src/main/java/uk/ac/ebi/eva/contigalias/repo/ChromosomeRepository.java b/src/main/java/uk/ac/ebi/eva/contigalias/repo/ChromosomeRepository.java index 2a1ae338..bf3d2784 100644 --- a/src/main/java/uk/ac/ebi/eva/contigalias/repo/ChromosomeRepository.java +++ b/src/main/java/uk/ac/ebi/eva/contigalias/repo/ChromosomeRepository.java @@ -27,6 +27,7 @@ import uk.ac.ebi.eva.contigalias.entities.AssemblyEntity; import uk.ac.ebi.eva.contigalias.entities.ChromosomeEntity; +import javax.transaction.Transactional; import java.util.List; @Repository @@ -50,6 +51,11 @@ public interface ChromosomeRepository extends JpaRepository findChromosomeEntitiesByAssembly_Refseq(String asmRefseq, Pageable request); Page findChromosomeEntitiesByGenbankSequenceNameAndAssembly_Taxid(String genbankName, long asmTaxid, Pageable request); diff --git a/src/main/java/uk/ac/ebi/eva/contigalias/scheduler/ChecksumSetter.java b/src/main/java/uk/ac/ebi/eva/contigalias/scheduler/ChecksumSetter.java index 564b50ca..f6ff960f 100644 --- a/src/main/java/uk/ac/ebi/eva/contigalias/scheduler/ChecksumSetter.java +++ b/src/main/java/uk/ac/ebi/eva/contigalias/scheduler/ChecksumSetter.java @@ -17,7 +17,6 @@ import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutionException; @Component public class ChecksumSetter { @@ -48,7 +47,7 @@ public void updateMd5CheckSumForAllAssemblies() { CompletableFuture future = updateMd5CheckSumForAssemblyAsync(assembly); try { future.get(); - } catch (InterruptedException | ExecutionException e) { + } catch (Exception e) { logger.error("Encountered an error when running MD5Checksum update for assembly: " + assembly); } finally { scheduledToRunMD5ChecksumUpdateTasks.remove(assembly); diff --git a/src/main/java/uk/ac/ebi/eva/contigalias/service/AssemblyService.java b/src/main/java/uk/ac/ebi/eva/contigalias/service/AssemblyService.java index 2365181b..ad373682 100644 --- a/src/main/java/uk/ac/ebi/eva/contigalias/service/AssemblyService.java +++ b/src/main/java/uk/ac/ebi/eva/contigalias/service/AssemblyService.java @@ -26,6 +26,7 @@ import uk.ac.ebi.eva.contigalias.datasource.NCBIAssemblyDataSource; import uk.ac.ebi.eva.contigalias.entities.AssemblyEntity; import uk.ac.ebi.eva.contigalias.entities.ChromosomeEntity; +import uk.ac.ebi.eva.contigalias.exception.AssemblyIngestionException; import uk.ac.ebi.eva.contigalias.exception.AssemblyNotFoundException; import uk.ac.ebi.eva.contigalias.exception.DuplicateAssemblyException; import uk.ac.ebi.eva.contigalias.repo.AssemblyRepository; @@ -105,12 +106,24 @@ public void fetchAndInsertAssembly(String accession) throws IOException { throw duplicateAssemblyInsertionException(accession, entity.get()); } - // download file and save assembly and chromosome data - ncbiDataSource.parseFileAndInsertAssembly(accession, enaDataSource, assemblyRepository, chromosomeRepository); - logger.info("Successfully inserted assembly for accession " + accession); + try { + // download file and save assembly and chromosome data + ncbiDataSource.parseFileAndInsertAssembly(accession, enaDataSource, assemblyRepository, chromosomeRepository); + logger.info("Successfully inserted assembly for accession " + accession); + + // submit job for retrieving and updating MD5 Checksum for assembly (asynchronously) + checksumSetter.updateMd5CheckSumForAssemblyAsync(accession); + } catch (Exception e) { + // roll back inserted entries in case of any exception or error + logger.error("Exception while inserting assembly " + accession + " Rolling back changes"); + deleteEntriesForAssembly(accession); + throw new AssemblyIngestionException(accession); + } + } - // submit job for retrieving and updating MD5 Checksum for assembly (asynchronously) - checksumSetter.updateMd5CheckSumForAssemblyAsync(accession); + public void deleteEntriesForAssembly(String accession) { + chromosomeRepository.deleteChromosomeEntitiesByAssembly_InsdcAccession(accession); + assemblyRepository.deleteAssemblyEntityByInsdcAccessionOrRefseq(accession); } public void retrieveAndInsertMd5ChecksumForAssembly(String assembly) {