Search in sources :

Example 86 with SingleEndSequenceFile

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

the class ExportUploadService method uploadSubmission.

/**
 * Upload an {@link NcbiExportSubmission}'s files and submission xml to the
 * configured ftp site
 *
 * @param submission
 *            The {@link NcbiExportSubmission} to upload
 * @param xml
 *            The submission xml to upload
 * @return true/false if upload was successful
 * @throws UploadException
 *             if the upload failed
 */
public NcbiExportSubmission uploadSubmission(NcbiExportSubmission submission, String xml) throws UploadException {
    FTPClient client = null;
    try {
        client = getFtpClient();
        // create submission directory name
        String directoryName = submission.getId().toString() + "-" + new Date().getTime();
        // cd to submission base directory
        if (!client.changeWorkingDirectory(baseDirectory)) {
            throw new UploadException("Couldn't change to base directory " + baseDirectory + " : " + client.getReplyString());
        }
        // create new submission directory
        if (!client.makeDirectory(directoryName)) {
            throw new UploadException("Couldn't create new upload directory " + directoryName + " : " + client.getReplyString());
        }
        // cd to submission directory
        if (!client.changeWorkingDirectory(directoryName)) {
            throw new UploadException("Couldn't change to upload directory " + directoryName + " : " + client.getReplyString());
        }
        // set the directory saved
        String directoryPath = baseDirectory + "/" + directoryName;
        submission.setDirectoryPath(directoryPath);
        // upload submission.xml file
        uploadString(client, "submission.xml", xml);
        // upload biosample files
        for (NcbiBioSampleFiles bsFile : submission.getBioSampleFiles()) {
            // upload single end files
            for (SingleEndSequenceFile file : bsFile.getFiles()) {
                // Just using file IDs as the basename for uploaded files to
                // avoid accidentally sending sensitive sample names to NCBI
                String filename = file.getSequenceFile().getId() + ".fastq";
                uploadPath(client, filename, file.getSequenceFile().getFile());
            }
            // upload paired end files
            for (SequenceFilePair pair : bsFile.getPairs()) {
                // upload forward
                SequenceFile file = pair.getForwardSequenceFile();
                // Just using file IDs as the basename for uploaded files to
                // avoid accidentally sending sensitive sample names to NCBI
                String filename = file.getId() + ".fastq";
                uploadPath(client, filename, file.getFile());
                // upload reverse
                file = pair.getReverseSequenceFile();
                filename = file.getId() + ".fastq";
                uploadPath(client, filename, file.getFile());
            }
        }
        // create submit.ready file
        uploadString(client, "submit.ready", "");
    } catch (IOException e) {
        logger.error("Error in upload", e);
        throw new UploadException("Could not upload run", e);
    } finally {
        disconnectFtpCient(client);
    }
    return submission;
}
Also used : SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) NcbiBioSampleFiles(ca.corefacility.bioinformatics.irida.model.export.NcbiBioSampleFiles) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) UploadException(ca.corefacility.bioinformatics.irida.exceptions.UploadException) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient) Date(java.util.Date) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)

Example 87 with SingleEndSequenceFile

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile 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 88 with SingleEndSequenceFile

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

the class SamplesController method createSequenceFileInSample.

/**
 * Create a {@link SequenceFile} and add it to a {@link Sample}
 *
 * @param file
 *            {@link MultipartFile}
 * @param sample
 *            {@link Sample} to add the file to.
 * @throws IOException
 */
private void createSequenceFileInSample(MultipartFile file, Sample sample) throws IOException {
    SequenceFile sequenceFile = createSequenceFile(file);
    sequencingObjectService.createSequencingObjectInSample(new SingleEndSequenceFile(sequenceFile), sample);
}
Also used : SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)

Example 89 with SingleEndSequenceFile

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

the class ProjectExportController method submitToNcbi.

/**
 * Save an NCBI submission to the database
 *
 * @param projectId
 *            the ID of the {@link Project} for the submission
 * @param submission
 *            A {@link SubmissionBody} describing the files to upload
 * @param principal
 *            the user submitting the upload
 * @return ID of the submission if successful
 * @throws InterruptedException
 *             if thread was not successfully put to sleep
 */
@RequestMapping(value = "/projects/{projectId}/export/ncbi", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> submitToNcbi(@PathVariable Long projectId, @RequestBody SubmissionBody submission, Principal principal) throws InterruptedException {
    Project project = projectService.read(projectId);
    User submitter = userService.getUserByUsername(principal.getName());
    List<NcbiBioSampleFiles> bioSampleFiles = new ArrayList<>();
    for (BioSampleBody sample : submission.getSamples()) {
        List<SingleEndSequenceFile> singleFiles = new ArrayList<>();
        sequencingObjectService.readMultiple(sample.getSingle()).forEach(f -> singleFiles.add((SingleEndSequenceFile) f));
        List<SequenceFilePair> paired = new ArrayList<>();
        sequencingObjectService.readMultiple(sample.getPaired()).forEach(f -> paired.add((SequenceFilePair) f));
        Builder sampleBuilder = new NcbiBioSampleFiles.Builder();
        sampleBuilder.bioSample(sample.getBioSample()).files(singleFiles).pairs(paired).instrumentModel(sample.getInstrumentModel()).libraryConstructionProtocol(sample.getLibraryConstructionProtocol()).libraryName(sample.getLibraryName()).librarySelection(sample.getLibrarySelection()).librarySource(sample.getLibrarySource()).libraryStrategy(sample.getLibraryStrategy()).namespace(submission.getNamespace());
        NcbiBioSampleFiles build = sampleBuilder.build();
        bioSampleFiles.add(build);
    }
    NcbiExportSubmission ncbiExportSubmission = new NcbiExportSubmission(project, submitter, submission.getBioProject(), submission.getOrganization(), submission.getNamespace(), submission.getReleaseDate(), bioSampleFiles);
    ncbiExportSubmission = exportSubmissionService.create(ncbiExportSubmission);
    return ImmutableMap.of("submissionId", ncbiExportSubmission.getId());
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) User(ca.corefacility.bioinformatics.irida.model.user.User) NcbiExportSubmission(ca.corefacility.bioinformatics.irida.model.NcbiExportSubmission) Builder(ca.corefacility.bioinformatics.irida.model.export.NcbiBioSampleFiles.Builder) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)

Example 90 with SingleEndSequenceFile

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

the class ProjectExportController method getUploadNcbiPage.

/**
 * Get the page for exporting a given {@link Project} and selected
 * {@link Sample}s
 *
 * @param projectId
 *            The ID of the project to export
 * @param sampleIds
 *            A List of sample ids to export
 * @param model
 *            model for the view to render
 * @return Name of the NCBI export page
 */
@RequestMapping(value = "/projects/{projectId}/export/ncbi", method = RequestMethod.GET)
public String getUploadNcbiPage(@PathVariable Long projectId, @RequestParam("ids[]") List<Long> sampleIds, Model model) {
    Project project = projectService.read(projectId);
    logger.trace("Reading " + sampleIds.size() + " samples");
    Iterable<Sample> samples = sampleService.readMultiple(sampleIds);
    logger.trace("Got samples");
    Set<Long> checkedSingles = new HashSet<>();
    Set<Long> checkedPairs = new HashSet<>();
    List<Map<String, Object>> sampleList = new ArrayList<>();
    for (Sample sample : samples) {
        Map<String, Object> sampleMap = new HashMap<>();
        sampleMap.put("name", sample.getLabel());
        sampleMap.put("id", sample.getId().toString());
        logger.trace("Doing sample " + sample.getId());
        Map<String, List<? extends Object>> files = new HashMap<>();
        Collection<SampleSequencingObjectJoin> singleEndFiles = sequencingObjectService.getSequencesForSampleOfType(sample, SingleEndSequenceFile.class);
        Collection<SampleSequencingObjectJoin> pairedEndFiles = sequencingObjectService.getSequencesForSampleOfType(sample, SequenceFilePair.class);
        List<SingleEndSequenceFile> singleEndFilesForSample = singleEndFiles.stream().map(j -> (SingleEndSequenceFile) j.getObject()).collect(Collectors.toList());
        List<SequenceFilePair> sequenceFilePairsForSample = pairedEndFiles.stream().map(j -> (SequenceFilePair) j.getObject()).collect(Collectors.toList());
        Optional<SequenceFilePair> newestPair = sequenceFilePairsForSample.stream().sorted((f1, f2) -> f2.getCreatedDate().compareTo(f1.getCreatedDate())).findFirst();
        Optional<SingleEndSequenceFile> newestSingle = singleEndFilesForSample.stream().sorted((f1, f2) -> f2.getCreatedDate().compareTo(f1.getCreatedDate())).findFirst();
        if (newestPair.isPresent() && newestSingle.isPresent()) {
            SequenceFilePair sequenceFilePair = newestPair.get();
            SingleEndSequenceFile join = newestSingle.get();
            if (sequenceFilePair.getCreatedDate().after(join.getCreatedDate())) {
                checkedPairs.add(newestPair.get().getId());
            } else {
                checkedSingles.add(newestSingle.get().getId());
            }
        } else {
            if (newestPair.isPresent()) {
                checkedPairs.add(newestPair.get().getId());
            } else if (newestSingle.isPresent()) {
                checkedSingles.add(newestSingle.get().getId());
            }
        }
        files.put("paired_end", sequenceFilePairsForSample);
        files.put("single_end", singleEndFilesForSample);
        sampleMap.put("files", files);
        sampleList.add(sampleMap);
    }
    sampleList.sort(new Comparator<Map<String, Object>>() {

        @Override
        public int compare(Map<String, Object> o1, Map<String, Object> o2) {
            String s1Name = (String) o1.get("name");
            String s2Name = (String) o2.get("name");
            return s1Name.compareTo(s2Name);
        }
    });
    model.addAttribute("project", project);
    model.addAttribute("samples", sampleList);
    model.addAttribute("newestSingles", checkedSingles);
    model.addAttribute("newestPairs", checkedPairs);
    model.addAttribute("instrument_model", NcbiInstrumentModel.values());
    model.addAttribute("library_selection", NcbiLibrarySelection.values());
    model.addAttribute("library_source", NcbiLibrarySource.values());
    model.addAttribute("library_strategy", NcbiLibraryStrategy.values());
    model.addAttribute("defaultNamespace", namespace);
    model.addAttribute("activeNav", "export");
    return NCBI_EXPORT_VIEW;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Builder(ca.corefacility.bioinformatics.irida.model.export.NcbiBioSampleFiles.Builder) java.util(java.util) DTExportSubmission(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTExportSubmission) NcbiExportSubmission(ca.corefacility.bioinformatics.irida.model.NcbiExportSubmission) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Controller(org.springframework.stereotype.Controller) ProjectService(ca.corefacility.bioinformatics.irida.service.ProjectService) Value(org.springframework.beans.factory.annotation.Value) Model(org.springframework.ui.Model) DataTablesResponseModel(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.models.DataTablesResponseModel) DataTablesResponse(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesResponse) Sort(org.springframework.data.domain.Sort) NcbiExportSubmissionService(ca.corefacility.bioinformatics.irida.service.export.NcbiExportSubmissionService) SequencingObjectService(ca.corefacility.bioinformatics.irida.service.SequencingObjectService) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) DataTablesRequest(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.config.DataTablesRequest) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) ca.corefacility.bioinformatics.irida.model.export(ca.corefacility.bioinformatics.irida.model.export) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Principal(java.security.Principal) UserService(ca.corefacility.bioinformatics.irida.service.user.UserService) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) User(ca.corefacility.bioinformatics.irida.model.user.User) SampleService(ca.corefacility.bioinformatics.irida.service.sample.SampleService) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) DataTablesParams(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesParams) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) Project(ca.corefacility.bioinformatics.irida.model.project.Project) ImmutableMap(com.google.common.collect.ImmutableMap) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)

Aggregations

SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)99 Test (org.junit.Test)72 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)61 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)44 Path (java.nio.file.Path)33 SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)24 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)22 SequenceFilePair (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair)20 WithMockUser (org.springframework.security.test.context.support.WithMockUser)18 Project (ca.corefacility.bioinformatics.irida.model.project.Project)17 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)17 AnalysisFastQC (ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisFastQC)12 Analysis (ca.corefacility.bioinformatics.irida.model.workflow.analysis.Analysis)11 IridaWorkflow (ca.corefacility.bioinformatics.irida.model.workflow.IridaWorkflow)10 ArrayList (java.util.ArrayList)10 SequencingRun (ca.corefacility.bioinformatics.irida.model.run.SequencingRun)8 HistoriesClient (com.github.jmchilton.blend4j.galaxy.HistoriesClient)8 History (com.github.jmchilton.blend4j.galaxy.beans.History)8 GZIPOutputStream (java.util.zip.GZIPOutputStream)8 ModelMap (org.springframework.ui.ModelMap)8