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;
}
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);
}
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);
}
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());
}
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;
}
Aggregations