Search in sources :

Example 51 with SequencingObject

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject in project irida by phac-nml.

the class SequencingRunServiceImpl method delete.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void delete(Long id) {
    Set<Sample> referencedSamples = new HashSet<>();
    logger.trace("Getting samples for SequencingRun " + id);
    // Get the Files from the SequencingRun to delete
    SequencingRun read = read(id);
    Set<SequencingObject> findSequencingObjectsForSequencingRun = objectRepository.findSequencingObjectsForSequencingRun(read);
    // For each file in the run
    for (SequencingObject sequencingObject : findSequencingObjectsForSequencingRun) {
        // get the sample the file is in. If the sample is empty when this
        // is complete it will be removed
        SampleSequencingObjectJoin sampleForSequencingObject = ssoRepository.getSampleForSequencingObject(sequencingObject);
        if (sampleForSequencingObject != null) {
            logger.trace("Sample " + sampleForSequencingObject.getSubject().getId() + " is used in this run");
            referencedSamples.add(sampleForSequencingObject.getSubject());
        }
        // Get the analysis submissions this file is included in
        Set<AnalysisSubmission> submissions = submissionRepository.findAnalysisSubmissionsForSequecingObject(sequencingObject);
        // If there are no submissions, we can delete the pair and file
        if (submissions.isEmpty()) {
            logger.trace("Deleting file " + sequencingObject.getId());
            objectRepository.delete(sequencingObject);
        } else {
            logger.trace("Keeping file " + sequencingObject.getId() + " because it's used in an analysis");
            if (sampleForSequencingObject != null) {
                // otherwise we'll just remove it from the sample
                ssoRepository.delete(sampleForSequencingObject);
            }
            sequencingObject.setSequencingRun(null);
            objectRepository.save(sequencingObject);
        }
    }
    // Delete the run
    logger.trace("Deleting SequencingRun " + id);
    super.delete(id);
    // Search if samples are empty. If they are, delete the sample.
    for (Sample sample : referencedSamples) {
        List<SampleSequencingObjectJoin> sequencesForSample = ssoRepository.getSequencesForSample(sample);
        if (sequencesForSample.isEmpty()) {
            logger.trace("Sample " + sample.getId() + " is empty.  Deleting sample");
            sampleRepository.delete(sample.getId());
        }
    }
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) SequencingRun(ca.corefacility.bioinformatics.irida.model.run.SequencingRun) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) AnalysisSubmission(ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) HashSet(java.util.HashSet) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 52 with SequencingObject

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject in project irida by phac-nml.

the class ProjectSynchronizationService method syncSample.

/**
 * Synchronize a given {@link Sample} to the local installation.
 *
 * @param sample          the {@link Sample} to synchronize. This should have been read
 *                        from a remote api.
 * @param project         The {@link Project} the {@link Sample} belongs in.
 * @param existingSamples A map of samples that have already been synchronized.  These will be checked to see if they've been updated
 */
public void syncSample(Sample sample, Project project, Map<String, Sample> existingSamples) {
    Sample localSample;
    if (existingSamples.containsKey(sample.getRemoteStatus().getURL())) {
        // if the sample already exists check if it's been updated
        localSample = existingSamples.get(sample.getRemoteStatus().getURL());
        // if there's changes, update the sample
        if (checkForChanges(localSample.getRemoteStatus(), sample)) {
            logger.debug("found changes for sample " + sample.getSelfHref());
            // ensure the ids are properly set
            sample = updateIds(localSample, sample);
            sample.getRemoteStatus().setSyncStatus(SyncStatus.UPDATING);
            localSample = sampleService.update(sample);
        }
    } else {
        // if the sample doesn't already exist create it
        sample.getRemoteStatus().setSyncStatus(SyncStatus.UPDATING);
        localSample = sampleService.create(sample);
        projectService.addSampleToProject(project, sample, true);
    }
    // get the local files and organize by their url
    Collection<SampleSequencingObjectJoin> localObjects = objectService.getSequencingObjectsForSample(localSample);
    Map<String, SequencingObject> objectsByUrl = new HashMap<>();
    localObjects.forEach(j -> {
        SequencingObject pair = j.getObject();
        // concatenated it
        if (pair.getRemoteStatus() != null) {
            String url = pair.getRemoteStatus().getURL();
            objectsByUrl.put(url, pair);
        }
    });
    List<SequenceFilePair> sequenceFilePairsForSample = pairRemoteService.getSequenceFilePairsForSample(sample);
    List<ProjectSynchronizationException> syncErrors = new ArrayList<>();
    for (SequenceFilePair pair : sequenceFilePairsForSample) {
        if (!objectsByUrl.containsKey(pair.getRemoteStatus().getURL())) {
            pair.setId(null);
            try {
                syncSequenceFilePair(pair, localSample);
            } catch (ProjectSynchronizationException e) {
                syncErrors.add(e);
            }
        }
    }
    List<SingleEndSequenceFile> unpairedFilesForSample = singleEndRemoteService.getUnpairedFilesForSample(sample);
    for (SingleEndSequenceFile file : unpairedFilesForSample) {
        if (!objectsByUrl.containsKey(file.getRemoteStatus().getURL())) {
            file.setId(null);
            try {
                syncSingleEndSequenceFile(file, localSample);
            } catch (ProjectSynchronizationException e) {
                syncErrors.add(e);
            }
        }
    }
    if (syncErrors.isEmpty()) {
        localSample.getRemoteStatus().setSyncStatus(SyncStatus.SYNCHRONIZED);
    } else {
        localSample.getRemoteStatus().setSyncStatus(SyncStatus.ERROR);
        logger.error("Setting sample " + localSample.getId() + "sync status to ERROR due to sync errors with files");
    }
    sampleService.update(localSample);
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) HashMap(java.util.HashMap) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ProjectSynchronizationException(ca.corefacility.bioinformatics.irida.exceptions.ProjectSynchronizationException) ArrayList(java.util.ArrayList) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)

Example 53 with SequencingObject

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject in project irida by phac-nml.

the class SamplesController method getSampleFiles.

/**
 * Get the page that shows the files belonging to that sample.
 *
 * @param model
 *            Spring {@link Model}
 * @param projectId
 *            the id of the {@link Project} the sample is in
 * @param sampleId
 *            Sample id
 * @return a Map representing all files (pairs and singles) for the sample.
 */
@RequestMapping(value = { "/projects/{projectId}/samples/{sampleId}/sequenceFiles" })
public String getSampleFiles(final Model model, @PathVariable Long projectId, @PathVariable Long sampleId) {
    Sample sample = sampleService.read(sampleId);
    model.addAttribute("sampleId", sampleId);
    Collection<SampleSequencingObjectJoin> filePairJoins = sequencingObjectService.getSequencesForSampleOfType(sample, SequenceFilePair.class);
    Collection<SampleSequencingObjectJoin> singleFileJoins = sequencingObjectService.getSequencesForSampleOfType(sample, SingleEndSequenceFile.class);
    Collection<SampleGenomeAssemblyJoin> genomeAssemblyJoins = sampleService.getAssembliesForSample(sample);
    logger.trace("Assembly joins " + genomeAssemblyJoins);
    List<GenomeAssembly> genomeAssemblies = genomeAssemblyJoins.stream().map(SampleGenomeAssemblyJoin::getObject).collect(Collectors.toList());
    List<SequencingObject> filePairs = filePairJoins.stream().map(SampleSequencingObjectJoin::getObject).collect(Collectors.toList());
    // get the project if available
    Project project = null;
    if (projectId != null) {
        project = projectService.read(projectId);
    }
    // add project to qc entries and filter any unavailable entries
    for (SequencingObject f : filePairs) {
        enhanceQcEntries(f, project);
    }
    for (SampleSequencingObjectJoin f : singleFileJoins) {
        enhanceQcEntries(f.getObject(), project);
    }
    // SequenceFile
    model.addAttribute("paired_end", filePairs);
    model.addAttribute("single_end", singleFileJoins);
    // assemblies
    model.addAttribute("assemblies", genomeAssemblies);
    model.addAttribute(MODEL_ATTR_SAMPLE, sample);
    model.addAttribute(MODEL_ATTR_CAN_MANAGE_SAMPLE, isProjectManagerForSample(sample));
    model.addAttribute(MODEL_ATTR_ACTIVE_NAV, ACTIVE_NAV_FILES);
    return SAMPLE_FILES_PAGE;
}
Also used : SampleGenomeAssemblyJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.SampleGenomeAssemblyJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) GenomeAssembly(ca.corefacility.bioinformatics.irida.model.assembly.GenomeAssembly) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)

Example 54 with SequencingObject

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject in project irida by phac-nml.

the class SamplesController method removeSequencingObjectFromSample.

/**
 **********************************************************************************************
 * AJAX REQUESTS
 ***********************************************************************************************
 */
/**
 * Remove a given {@link SequencingObject} from a sample
 *
 * @param attributes
 *            the redirect attributes where we can add flash-scoped messages
 *            for the client.
 * @param sampleId
 *            the {@link Sample} id
 * @param fileId
 *            The {@link SequencingObject} id
 * @param request
 *            {@link HttpServletRequest}
 * @param locale
 *            the locale specified by the browser.
 * @return map stating the request was successful
 */
@RequestMapping(value = "/samples/{sampleId}/files/delete", method = RequestMethod.POST)
public String removeSequencingObjectFromSample(RedirectAttributes attributes, @PathVariable Long sampleId, @RequestParam Long fileId, HttpServletRequest request, Locale locale) {
    Sample sample = sampleService.read(sampleId);
    SequencingObject sequencingObject = sequencingObjectService.readSequencingObjectForSample(sample, fileId);
    try {
        sampleService.removeSequencingObjectFromSample(sample, sequencingObject);
        attributes.addFlashAttribute("fileDeleted", true);
        attributes.addFlashAttribute("fileDeletedMessage", messageSource.getMessage("samples.files.removed.message", new Object[] { sequencingObject.getLabel() }, locale));
    } catch (Exception e) {
        logger.error("Could not remove sequence file from sample: ", e);
        attributes.addFlashAttribute("fileDeleted", true);
        attributes.addFlashAttribute("fileDeletedError", messageSource.getMessage("samples.files.remove.error", new Object[] { sequencingObject.getLabel() }, locale));
    }
    return "redirect:" + request.getHeader("referer");
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) ConcatenateException(ca.corefacility.bioinformatics.irida.exceptions.ConcatenateException) IOException(java.io.IOException) ConstraintViolationException(javax.validation.ConstraintViolationException)

Example 55 with SequencingObject

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject in project irida by phac-nml.

the class PipelineController method ajaxStartPipeline.

// ************************************************************************************************
// AJAX
// ************************************************************************************************
/**
 * Launch a pipeline
 *
 * @param locale     the locale that the browser is using for the current request.
 * @param parameters DTO of pipeline start parameters
 * @return a JSON response with the status and any messages.
 */
@RequestMapping(value = "/ajax/start", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> ajaxStartPipeline(Locale locale, @RequestBody final PipelineStartParameters parameters) {
    try {
        IridaWorkflow flow = workflowsService.getIridaWorkflow(parameters.getWorkflowId());
        IridaWorkflowDescription description = flow.getWorkflowDescription();
        // The pipeline needs to have a name.
        String name = parameters.getName();
        if (Strings.isNullOrEmpty(name)) {
            return ImmutableMap.of("error", messageSource.getMessage("workflow.no-name-provided", null, locale));
        }
        // Check to see if a reference file is required.
        Long ref = parameters.getRef();
        if (description.requiresReference() && ref == null) {
            return ImmutableMap.of("error", messageSource.getMessage("pipeline.error.no-reference.pipeline-start", null, locale));
        }
        // Get a list of the files to submit
        List<SingleEndSequenceFile> singleEndFiles = new ArrayList<>();
        List<SequenceFilePair> sequenceFilePairs = new ArrayList<>();
        List<Long> single = parameters.getSingle();
        if (single != null) {
            Iterable<SequencingObject> readMultiple = sequencingObjectService.readMultiple(single);
            readMultiple.forEach(f -> {
                if (!(f instanceof SingleEndSequenceFile)) {
                    throw new IllegalArgumentException("file " + f.getId() + " not a SingleEndSequenceFile");
                }
                singleEndFiles.add((SingleEndSequenceFile) f);
            });
            // Check the single files for duplicates in a sample, throws SampleAnalysisDuplicateException
            sequencingObjectService.getUniqueSamplesForSequencingObjects(Sets.newHashSet(singleEndFiles));
        }
        List<Long> paired = parameters.getPaired();
        if (paired != null) {
            Iterable<SequencingObject> readMultiple = sequencingObjectService.readMultiple(paired);
            readMultiple.forEach(f -> {
                if (!(f instanceof SequenceFilePair)) {
                    throw new IllegalArgumentException("file " + f.getId() + " not a SequenceFilePair");
                }
                sequenceFilePairs.add((SequenceFilePair) f);
            });
            // Check the pair files for duplicates in a sample, throws SampleAnalysisDuplicateException
            sequencingObjectService.getUniqueSamplesForSequencingObjects(Sets.newHashSet(sequenceFilePairs));
        }
        // Get the pipeline parameters
        Map<String, String> params = new HashMap<>();
        IridaWorkflowNamedParameters namedParameters = null;
        Map<String, Object> selectedParameters = parameters.getSelectedParameters();
        if (selectedParameters != null) {
            try {
                final String selectedParametersId = selectedParameters.get("id").toString();
                if (!DEFAULT_WORKFLOW_PARAMETERS_ID.equals(selectedParametersId) && !CUSTOM_UNSAVED_WORKFLOW_PARAMETERS_ID.equals(selectedParametersId)) {
                    // this means that a named parameter set was selected
                    // and unmodified, so load up that named parameter set
                    // to pass along.
                    namedParameters = namedParameterService.read(Long.valueOf(selectedParametersId));
                } else {
                    @SuppressWarnings("unchecked") final List<Map<String, String>> unnamedParameters = (List<Map<String, String>>) selectedParameters.get("parameters");
                    for (final Map<String, String> parameter : unnamedParameters) {
                        params.put(parameter.get("name"), parameter.get("value"));
                    }
                }
            } catch (Exception e) {
                return ImmutableMap.of("parameterError", messageSource.getMessage("pipeline.parameters.error", null, locale));
            }
        }
        List<Project> projectsToShare = new ArrayList<>();
        List<Long> sharedProjects = parameters.getSharedProjects();
        if (sharedProjects != null && !sharedProjects.isEmpty()) {
            projectsToShare = Lists.newArrayList(projectService.readMultiple(sharedProjects));
        }
        String analysisDescription = parameters.getDescription();
        Boolean writeResultsToSamples = parameters.getWriteResultsToSamples();
        if (description.getInputs().requiresSingleSample()) {
            analysisSubmissionService.createSingleSampleSubmission(flow, ref, singleEndFiles, sequenceFilePairs, params, namedParameters, name, analysisDescription, projectsToShare, writeResultsToSamples);
        } else {
            analysisSubmissionService.createMultipleSampleSubmission(flow, ref, singleEndFiles, sequenceFilePairs, params, namedParameters, name, analysisDescription, projectsToShare, writeResultsToSamples);
        }
    } catch (IridaWorkflowNotFoundException e) {
        logger.error("Cannot find IridaWorkflow [" + parameters.getWorkflowId() + "]", e);
        return ImmutableMap.of("pipelineError", messageSource.getMessage("pipeline.error.invalid-pipeline", null, locale));
    } catch (DuplicateSampleException e) {
        logger.error("Multiple files for Sample found", e);
        return ImmutableMap.of("pipelineError", messageSource.getMessage("pipeline.error.duplicate-samples", null, locale));
    }
    return ImmutableMap.of("success", true);
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) HashMap(java.util.HashMap) IridaWorkflow(ca.corefacility.bioinformatics.irida.model.workflow.IridaWorkflow) ArrayList(java.util.ArrayList) IridaWorkflowNamedParameters(ca.corefacility.bioinformatics.irida.model.workflow.submission.IridaWorkflowNamedParameters) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) DuplicateSampleException(ca.corefacility.bioinformatics.irida.exceptions.DuplicateSampleException) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) List(java.util.List) ArrayList(java.util.ArrayList) IridaWorkflowNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.IridaWorkflowNotFoundException) IridaWorkflowParameterException(ca.corefacility.bioinformatics.irida.exceptions.IridaWorkflowParameterException) IOException(java.io.IOException) DuplicateSampleException(ca.corefacility.bioinformatics.irida.exceptions.DuplicateSampleException) IridaWorkflowNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.IridaWorkflowNotFoundException) Project(ca.corefacility.bioinformatics.irida.model.project.Project) IridaWorkflowDescription(ca.corefacility.bioinformatics.irida.model.workflow.description.IridaWorkflowDescription) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)61 SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)29 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)25 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)24 Test (org.junit.Test)24 SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)16 SequenceFilePair (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair)14 WithMockUser (org.springframework.security.test.context.support.WithMockUser)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 Project (ca.corefacility.bioinformatics.irida.model.project.Project)11 AnalysisFastQC (ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisFastQC)11 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)10 SequencingRun (ca.corefacility.bioinformatics.irida.model.run.SequencingRun)8 Path (java.nio.file.Path)8 ModelMap (org.springframework.ui.ModelMap)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 Transactional (org.springframework.transaction.annotation.Transactional)6 IOException (java.io.IOException)5 List (java.util.List)5 Logger (org.slf4j.Logger)5