Search in sources :

Example 6 with NcbiBioSampleFiles

use of ca.corefacility.bioinformatics.irida.model.export.NcbiBioSampleFiles in project irida by phac-nml.

the class ExportUploadService method updateSubmissionForXml.

/**
 * Get the updates from the result.#.xml file for the given submission and
 * update the object. XML will look like the following:
 *
 * <pre>
 * <?xml version='1.0' encoding='utf-8'?>
 * <SubmissionStatus submission_id="SUB1234" status="processed-ok">
 *   <Action action_id="SUB1234-submission12345" target_db="SRA" status="processed-ok" notify_submitter="true">
 *     <Response status="processed-ok">
 *       <Object target_db="SRA" object_id="RUN:4567" spuid_namespace="NML" spuid="submission12345" accession="SRR6789" status="updated">
 *         <Meta>
 *           <SRAStudy>SRP012345</SRAStudy>
 *         </Meta>
 *       </Object>
 *     </Response>
 *   </Action>
 * </SubmissionStatus>
 * </pre>
 *
 * @param submission
 *            {@link NcbiExportSubmission} to update
 * @param xml
 *            {@link InputStream} of xml
 * @return Updated {@link NcbiExportSubmission}
 * @throws NcbiXmlParseException
 *             if the xml couldn't be parsed
 */
private NcbiExportSubmission updateSubmissionForXml(NcbiExportSubmission submission, InputStream xml) throws NcbiXmlParseException {
    try {
        // read the incoming xml file
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(xml);
        XPath xPath = XPathFactory.newInstance().newXPath();
        // get the submission status and set it in the submission
        String submissionStatusString = xPath.compile("SubmissionStatus/@status").evaluate(doc);
        if (submissionStatusString == null) {
            throw new NcbiXmlParseException("result file should have 1 SubmissionStatus element with a status");
        }
        ExportUploadState submissionStatus = ExportUploadState.fromString(submissionStatusString);
        submission.setUploadState(submissionStatus);
        logger.trace("Root export state is " + submissionStatus);
        // get all the sample files objects by name
        Map<String, NcbiBioSampleFiles> sampleMap = getSampleNameMap(submission);
        // get the actions
        NodeList actions = (NodeList) xPath.compile("SubmissionStatus/Action").evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < actions.getLength(); i++) {
            if (actions.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element action = (Element) actions.item(i);
                // get the status and action id
                String status = action.getAttribute("status");
                String actionId = action.getAttribute("action_id");
                // action id is of the form SUBMISSIONID-sampleid
                String sampleId = actionId.substring(actionId.indexOf("-") + 1);
                // get the sample for this action
                NcbiBioSampleFiles ncbiBioSampleFiles = sampleMap.get(sampleId);
                ExportUploadState sampleStatus = ExportUploadState.fromString(status);
                ncbiBioSampleFiles.setSubmissionStatus(sampleStatus);
                logger.trace("Sample export state for sample " + ncbiBioSampleFiles.getId() + " is " + sampleStatus);
                String accession = xPath.compile("Response/Object/@accession").evaluate(action);
                if (accession != null && !accession.isEmpty()) {
                    logger.trace("Found accession " + accession);
                    ncbiBioSampleFiles.setAccession(accession);
                }
            }
        }
    } catch (XPathExpressionException | ParserConfigurationException | SAXException | IOException e) {
        logger.error("Couldn't parse response XML", e);
        throw new NcbiXmlParseException("Error parsing NCBI response", e);
    }
    return submission;
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) NcbiXmlParseException(ca.corefacility.bioinformatics.irida.exceptions.NcbiXmlParseException) SAXException(org.xml.sax.SAXException) NcbiBioSampleFiles(ca.corefacility.bioinformatics.irida.model.export.NcbiBioSampleFiles) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ExportUploadState(ca.corefacility.bioinformatics.irida.model.enums.ExportUploadState) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 7 with NcbiBioSampleFiles

use of ca.corefacility.bioinformatics.irida.model.export.NcbiBioSampleFiles in project irida by phac-nml.

the class NcbiExportSubmissionServceTest method testCreate.

@Test
public void testCreate() {
    SingleEndSequenceFile sequenceFile = new SingleEndSequenceFile(new SequenceFile());
    NcbiBioSampleFiles ncbiBioSampleFiles = new NcbiBioSampleFiles("sample", Lists.newArrayList(sequenceFile), Lists.newArrayList(), null, "library_name", null, null, null, "library_construction_protocol", "namespace");
    NcbiExportSubmission submission = new NcbiExportSubmission(null, null, "bioProjectId", "organization", "ncbiNamespace", new Date(), Lists.newArrayList(ncbiBioSampleFiles));
    service.create(submission);
    verify(repository).save(submission);
}
Also used : NcbiBioSampleFiles(ca.corefacility.bioinformatics.irida.model.export.NcbiBioSampleFiles) NcbiExportSubmission(ca.corefacility.bioinformatics.irida.model.NcbiExportSubmission) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) Date(java.util.Date) Test(org.junit.Test)

Example 8 with NcbiBioSampleFiles

use of ca.corefacility.bioinformatics.irida.model.export.NcbiBioSampleFiles 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)

Aggregations

NcbiBioSampleFiles (ca.corefacility.bioinformatics.irida.model.export.NcbiBioSampleFiles)8 NcbiExportSubmission (ca.corefacility.bioinformatics.irida.model.NcbiExportSubmission)6 Test (org.junit.Test)5 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)4 SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)4 Date (java.util.Date)4 SequenceFilePair (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair)2 TestEmailController (ca.corefacility.bioinformatics.irida.service.impl.TestEmailController)2 IOException (java.io.IOException)2 Set (java.util.Set)2 FakeFtpServer (org.mockftpserver.fake.FakeFtpServer)2 UserAccount (org.mockftpserver.fake.UserAccount)2 DirectoryEntry (org.mockftpserver.fake.filesystem.DirectoryEntry)2 FileEntry (org.mockftpserver.fake.filesystem.FileEntry)2 FileSystem (org.mockftpserver.fake.filesystem.FileSystem)2 UnixFakeFileSystem (org.mockftpserver.fake.filesystem.UnixFakeFileSystem)2 NcbiXmlParseException (ca.corefacility.bioinformatics.irida.exceptions.NcbiXmlParseException)1 UploadException (ca.corefacility.bioinformatics.irida.exceptions.UploadException)1 ExportUploadState (ca.corefacility.bioinformatics.irida.model.enums.ExportUploadState)1 Path (java.nio.file.Path)1