Search in sources :

Example 1 with UploadException

use of ca.corefacility.bioinformatics.irida.exceptions.UploadException in project irida by phac-nml.

the class AnalysisCollectionServiceGalaxy method uploadSequenceFilesPaired.

/**
 * Uploads a list of paired sequence files belonging to the given samples to
 * Galaxy.
 *
 * @param sampleSequenceFilesPaired
 *            A map between {@link Sample} and {@link SequenceFilePair}.
 * @param workflowHistory
 *            The history to upload the sequence files into.
 * @param workflowLibrary
 *            A temporary library to upload files into.
 * @return A CollectionResponse for the dataset collection constructed from
 *         the given files.
 * @throws ExecutionManagerException
 *             If there was an error uploading the files.
 */
public CollectionResponse uploadSequenceFilesPaired(Map<Sample, ? extends IridaSequenceFilePair> sampleSequenceFilesPaired, History workflowHistory, Library workflowLibrary) throws ExecutionManagerException {
    CollectionDescription description = new CollectionDescription();
    description.setCollectionType(DatasetCollectionType.LIST_PAIRED.toString());
    description.setName(COLLECTION_NAME_PAIRED);
    Map<Sample, Path> samplesMapPairForward = new HashMap<>();
    Map<Sample, Path> samplesMapPairReverse = new HashMap<>();
    Set<Path> pathsToUpload = new HashSet<>();
    for (Sample sample : sampleSequenceFilesPaired.keySet()) {
        IridaSequenceFilePair sequenceFilePair = sampleSequenceFilesPaired.get(sample);
        IridaSequenceFile fileForward = sequenceFilePair.getForwardSequenceFile();
        IridaSequenceFile fileReverse = sequenceFilePair.getReverseSequenceFile();
        samplesMapPairForward.put(sample, fileForward.getFile());
        samplesMapPairReverse.put(sample, fileReverse.getFile());
        pathsToUpload.add(fileForward.getFile());
        pathsToUpload.add(fileReverse.getFile());
    }
    // upload files to library and then to a history
    Map<Path, String> pathHistoryDatasetId = galaxyHistoriesService.filesToLibraryToHistory(pathsToUpload, InputFileType.FASTQ_SANGER, workflowHistory, workflowLibrary, DataStorage.LOCAL);
    for (Sample sample : sampleSequenceFilesPaired.keySet()) {
        Path fileForward = samplesMapPairForward.get(sample);
        Path fileReverse = samplesMapPairReverse.get(sample);
        if (!pathHistoryDatasetId.containsKey(fileForward)) {
            throw new UploadException("Error, no corresponding history item found for " + fileForward);
        } else if (!pathHistoryDatasetId.containsKey(fileReverse)) {
            throw new UploadException("Error, no corresponding history item found for " + fileReverse);
        } else {
            String datasetHistoryIdForward = pathHistoryDatasetId.get(fileForward);
            String datasetHistoryIdReverse = pathHistoryDatasetId.get(fileReverse);
            CollectionElement pairedElement = new CollectionElement();
            pairedElement.setName(sample.getSampleName());
            pairedElement.setCollectionType(DatasetCollectionType.PAIRED.toString());
            HistoryDatasetElement datasetElementForward = new HistoryDatasetElement();
            datasetElementForward.setId(datasetHistoryIdForward);
            datasetElementForward.setName(FORWARD_NAME);
            pairedElement.addCollectionElement(datasetElementForward);
            HistoryDatasetElement datasetElementReverse = new HistoryDatasetElement();
            datasetElementReverse.setId(datasetHistoryIdReverse);
            datasetElementReverse.setName(REVERSE_NAME);
            pairedElement.addCollectionElement(datasetElementReverse);
            description.addDatasetElement(pairedElement);
        }
    }
    return galaxyHistoriesService.constructCollection(description, workflowHistory);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) IridaSequenceFilePair(ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFilePair) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) UploadException(ca.corefacility.bioinformatics.irida.exceptions.UploadException) CollectionDescription(com.github.jmchilton.blend4j.galaxy.beans.collection.request.CollectionDescription) HistoryDatasetElement(com.github.jmchilton.blend4j.galaxy.beans.collection.request.HistoryDatasetElement) IridaSequenceFile(ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFile) CollectionElement(com.github.jmchilton.blend4j.galaxy.beans.collection.request.CollectionElement) HashSet(java.util.HashSet)

Example 2 with UploadException

use of ca.corefacility.bioinformatics.irida.exceptions.UploadException in project irida by phac-nml.

the class ExportUploadService method uploadString.

/**
 * Upload a string to remote ftp client
 *
 * @param client
 *            {@link FTPClient} to use for upload
 * @param filename
 *            name of file to create
 * @param content
 *            content of file to create
 * @throws UploadException
 *             if file could not be uploaded
 */
private void uploadString(FTPClient client, String filename, String content) throws UploadException {
    int tries = 0;
    boolean done = false;
    do {
        tries++;
        try (ByteArrayInputStream stringStream = new ByteArrayInputStream(content.getBytes())) {
            client.storeFile(filename, stringStream);
            done = true;
        } catch (Exception e) {
            String reply = client.getReplyString();
            logger.error("Error uploading file: " + reply, e);
            if (tries >= MAX_RETRIES) {
                throw new UploadException("Could not upload file " + filename + " : " + reply, e);
            }
        }
    } while (!done);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UploadException(ca.corefacility.bioinformatics.irida.exceptions.UploadException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UploadException(ca.corefacility.bioinformatics.irida.exceptions.UploadException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) NcbiXmlParseException(ca.corefacility.bioinformatics.irida.exceptions.NcbiXmlParseException)

Example 3 with UploadException

use of ca.corefacility.bioinformatics.irida.exceptions.UploadException in project irida by phac-nml.

the class ExportUploadService method uploadPath.

/**
 * Upload a file {@link Path} to a remote ftp client
 *
 * @param client   {@link FTPClient} to upload with
 * @param filename name of file to create
 * @param path     {@link Path} to upload
 * @throws UploadException if file could not be uploaded
 */
private void uploadPath(FTPClient client, String filename, Path path) throws UploadException {
    int tries = 0;
    boolean done = false;
    do {
        tries++;
        try (InputStream stream = Files.newInputStream(path)) {
            client.storeFile(filename, stream);
            done = true;
        } catch (Exception e) {
            String reply = client.getReplyString();
            logger.error("Error uploading file: " + reply, e);
            if (tries >= MAX_RETRIES) {
                throw new UploadException("Could not upload file " + filename + " : " + reply, e);
            }
        }
    } while (!done);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UploadException(ca.corefacility.bioinformatics.irida.exceptions.UploadException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UploadException(ca.corefacility.bioinformatics.irida.exceptions.UploadException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) NcbiXmlParseException(ca.corefacility.bioinformatics.irida.exceptions.NcbiXmlParseException)

Example 4 with UploadException

use of ca.corefacility.bioinformatics.irida.exceptions.UploadException in project irida by phac-nml.

the class GalaxyLibrariesService method filesToLibraryWait.

/**
 * Uploads a set of files to a given library, waiting until all uploads are
 * complete.
 *
 * @param paths
 *            The set of paths to upload.
 * @param fileType
 *            The file type of the file to upload.
 * @param library
 *            The library to initially upload the file into.
 * @param dataStorage
 *            The type of DataStorage strategy to use.
 * @return An @{link Map} of paths and ids for each dataset object in this
 *         library.
 * @throws UploadException
 *             If there was an issue uploading the file to Galaxy.
 */
public Map<Path, String> filesToLibraryWait(Set<Path> paths, InputFileType fileType, Library library, DataStorage dataStorage) throws UploadException {
    checkNotNull(paths, "paths is null");
    final int pollingTimeMillis = libraryPollingTime * 1000;
    Map<Path, String> datasetLibraryIdsMap = new HashMap<>();
    try {
        // upload all files to library first
        for (Path path : paths) {
            String datasetLibraryId = fileToLibrary(path, fileType, library, dataStorage);
            datasetLibraryIdsMap.put(path, datasetLibraryId);
        }
        Future<Void> waitForLibraries = executor.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                // wait for uploads to finish
                for (Path path : paths) {
                    String datasetLibraryId = datasetLibraryIdsMap.get(path);
                    LibraryDataset libraryDataset = librariesClient.showDataset(library.getId(), datasetLibraryId);
                    while (!LIBRARY_OK_STATE.equals(libraryDataset.getState())) {
                        logger.trace("Waiting for library dataset " + libraryDataset.getId() + " to be finished processing, in state " + libraryDataset.getState());
                        Thread.sleep(pollingTimeMillis);
                        libraryDataset = librariesClient.showDataset(library.getId(), datasetLibraryId);
                        if (LIBRARY_FAIL_STATES.contains(libraryDataset.getState())) {
                            throw new UploadErrorException("Error: upload to Galaxy library id=" + library.getId() + " name=" + library.getName() + " for dataset id=" + datasetLibraryId + " name=" + libraryDataset.getName() + " failed with state=" + libraryDataset.getState());
                        }
                    }
                }
                return null;
            }
        });
        waitForLibraries.get(libraryUploadTimeout, TimeUnit.SECONDS);
    } catch (RuntimeException e) {
        throw new UploadException(e);
    } catch (TimeoutException e) {
        throw new UploadTimeoutException("Timeout while uploading, time limit = " + libraryUploadTimeout + " seconds", e);
    } catch (ExecutionException e) {
        if (e.getCause() instanceof UploadErrorException) {
            throw (UploadErrorException) e.getCause();
        } else {
            throw new UploadException(e);
        }
    } catch (InterruptedException e) {
        throw new UploadException(e);
    }
    return datasetLibraryIdsMap;
}
Also used : Path(java.nio.file.Path) UploadTimeoutException(ca.corefacility.bioinformatics.irida.exceptions.UploadTimeoutException) HashMap(java.util.HashMap) UploadErrorException(ca.corefacility.bioinformatics.irida.exceptions.UploadErrorException) UploadException(ca.corefacility.bioinformatics.irida.exceptions.UploadException) DeleteGalaxyObjectFailedException(ca.corefacility.bioinformatics.irida.exceptions.galaxy.DeleteGalaxyObjectFailedException) TimeoutException(java.util.concurrent.TimeoutException) UploadException(ca.corefacility.bioinformatics.irida.exceptions.UploadException) CreateLibraryException(ca.corefacility.bioinformatics.irida.exceptions.galaxy.CreateLibraryException) UploadTimeoutException(ca.corefacility.bioinformatics.irida.exceptions.UploadTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) UploadErrorException(ca.corefacility.bioinformatics.irida.exceptions.UploadErrorException) LibraryDataset(com.github.jmchilton.blend4j.galaxy.beans.LibraryDataset) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) UploadTimeoutException(ca.corefacility.bioinformatics.irida.exceptions.UploadTimeoutException)

Example 5 with UploadException

use of ca.corefacility.bioinformatics.irida.exceptions.UploadException in project irida by phac-nml.

the class GalaxyLibrariesService method fileToLibrary.

/**
 * Uploads the given file to a library with the given information.
 *
 * @param path
 *            The path of the file to upload.
 * @param fileType
 *            The type of the file to upload.
 * @param library
 *            The library to upload the file into.
 * @param dataStorage
 *            The {@link DataStorage} method to apply to this dataset.
 * @return A dataset id for the dataset in this library.
 * @throws UploadException
 *             If there was an issue uploading the file to the library.
 */
public String fileToLibrary(Path path, InputFileType fileType, Library library, DataStorage dataStorage) throws UploadException {
    checkNotNull(path, "path is null");
    checkNotNull(fileType, "fileType is null");
    checkNotNull(library, "library is null");
    checkNotNull(library.getId(), "library id is null");
    checkState(path.toFile().exists(), "path " + path + " does not exist");
    File file = path.toFile();
    try {
        LibraryContent rootContent = librariesClient.getRootFolder(library.getId());
        FilesystemPathsLibraryUpload upload = new FilesystemPathsLibraryUpload();
        upload.setFolderId(rootContent.getId());
        upload.setContent(file.getAbsolutePath());
        upload.setName(file.getName());
        upload.setLinkData(DataStorage.LOCAL.equals(dataStorage));
        upload.setFileType(fileType.toString());
        GalaxyObject uploadObject = librariesClient.uploadFilesystemPaths(library.getId(), upload);
        return uploadObject.getId();
    } catch (RuntimeException e) {
        throw new UploadException(e);
    }
}
Also used : GalaxyObject(com.github.jmchilton.blend4j.galaxy.beans.GalaxyObject) LibraryContent(com.github.jmchilton.blend4j.galaxy.beans.LibraryContent) UploadException(ca.corefacility.bioinformatics.irida.exceptions.UploadException) File(java.io.File) FilesystemPathsLibraryUpload(com.github.jmchilton.blend4j.galaxy.beans.FilesystemPathsLibraryUpload)

Aggregations

UploadException (ca.corefacility.bioinformatics.irida.exceptions.UploadException)9 Path (java.nio.file.Path)4 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 NcbiXmlParseException (ca.corefacility.bioinformatics.irida.exceptions.NcbiXmlParseException)2 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)2 CollectionDescription (com.github.jmchilton.blend4j.galaxy.beans.collection.request.CollectionDescription)2 HistoryDatasetElement (com.github.jmchilton.blend4j.galaxy.beans.collection.request.HistoryDatasetElement)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 ConnectException (java.net.ConnectException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 SAXException (org.xml.sax.SAXException)2 UploadErrorException (ca.corefacility.bioinformatics.irida.exceptions.UploadErrorException)1 UploadTimeoutException (ca.corefacility.bioinformatics.irida.exceptions.UploadTimeoutException)1 CreateLibraryException (ca.corefacility.bioinformatics.irida.exceptions.galaxy.CreateLibraryException)1 DeleteGalaxyObjectFailedException (ca.corefacility.bioinformatics.irida.exceptions.galaxy.DeleteGalaxyObjectFailedException)1 NcbiBioSampleFiles (ca.corefacility.bioinformatics.irida.model.export.NcbiBioSampleFiles)1 IridaSequenceFile (ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFile)1