Search in sources :

Example 6 with InputStreamWrapper

use of org.jbei.ice.lib.entry.sequence.InputStreamWrapper in project ice by JBEI.

the class FileResource method getAttachment.

@GET
@Path("attachment/{fileId}")
public Response getAttachment(@PathParam("fileId") String fileId) {
    String userId = requireUserId();
    try {
        InputStreamWrapper wrapper = attachments.getAttachmentByFileId(userId, fileId);
        if (wrapper == null) {
            return respond(Response.Status.NOT_FOUND);
        }
        StreamingOutput stream = output -> IOUtils.copy(wrapper.getInputStream(), output);
        return addHeaders(Response.ok(stream), wrapper.getName());
    } catch (IOException e) {
        Logger.error(e);
        throw new WebApplicationException(e);
    }
}
Also used : RemoteSequence(org.jbei.ice.lib.net.RemoteSequence) AttachmentInfo(org.jbei.ice.lib.dto.entry.AttachmentInfo) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) ConfigurationSettings(org.jbei.ice.lib.config.ConfigurationSettings) Sequences(org.jbei.ice.lib.entry.sequence.Sequences) PartSequence(org.jbei.ice.lib.entry.sequence.PartSequence) Setting(org.jbei.ice.lib.dto.Setting) TraceSequences(org.jbei.ice.lib.entry.sequence.analysis.TraceSequences) ConfigurationKey(org.jbei.ice.lib.dto.ConfigurationKey) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Entries(org.jbei.ice.lib.entry.Entries) MediaType(javax.ws.rs.core.MediaType) DAOFactory(org.jbei.ice.storage.DAOFactory) Logger(org.jbei.ice.lib.common.logging.Logger) ByteArrayInputStream(java.io.ByteArrayInputStream) FileBulkUpload(org.jbei.ice.lib.bulkupload.FileBulkUpload) SequenceInfo(org.jbei.ice.lib.dto.entry.SequenceInfo) SequenceFormat(org.jbei.ice.lib.entry.sequence.SequenceFormat) EntryType(org.jbei.ice.lib.dto.entry.EntryType) Utils(org.jbei.ice.lib.utils.Utils) EntrySelection(org.jbei.ice.lib.entry.EntrySelection) Files(java.nio.file.Files) FileUtils(org.apache.commons.io.FileUtils) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) Collectors(java.util.stream.Collectors) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) Attachments(org.jbei.ice.lib.entry.attachment.Attachments) RemoteEntries(org.jbei.ice.lib.net.RemoteEntries) FormDataParam(org.glassfish.jersey.media.multipart.FormDataParam) List(java.util.List) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) Paths(java.nio.file.Paths) ShotgunSequenceDAO(org.jbei.ice.storage.hibernate.dao.ShotgunSequenceDAO) TraceSequence(org.jbei.ice.storage.model.TraceSequence) EntriesAsCSV(org.jbei.ice.lib.entry.EntriesAsCSV) InvalidFormatParserException(org.jbei.ice.lib.parsers.InvalidFormatParserException) ShotgunSequence(org.jbei.ice.storage.model.ShotgunSequence) InputStream(java.io.InputStream) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException)

Example 7 with InputStreamWrapper

use of org.jbei.ice.lib.entry.sequence.InputStreamWrapper in project ice by JBEI.

the class RemoteEntriesAsCSV method writeDataEntries.

protected void writeDataEntries(RemotePartner partner, List<PartData> entries, List<EntryFieldLabel> fields, CSVWriter writer, ZipOutputStream zos) {
    if (entries == null)
        return;
    for (PartData partData : entries) {
        String[] line = new String[fields.size() + 4];
        line[0] = partner.getUrl();
        line[1] = new Date(partData.getCreationTime()).toString();
        line[2] = partData.getPartId();
        int i = 2;
        for (EntryFieldLabel field : fields) {
            line[i + 1] = PartDataUtil.entryFieldToValue(partData, field);
            i += 1;
        }
        // write sequence to zip file
        if (partData.isHasSequence()) {
            try {
                // get remote sequence
                FeaturedDNASequence featuredDNASequence = remoteEntries.getPublicEntrySequence(partner.getId(), Long.toString(partData.getId()));
                if (featuredDNASequence != null) {
                    String name = partData.getPartId() + ".gb";
                    // write sequence to zip
                    line[i + 1] = name;
                    Sequence sequence = SequenceUtil.dnaSequenceToSequence(featuredDNASequence);
                    GenbankFormatter genbankFormatter = new GenbankFormatter(name);
                    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                    genbankFormatter.format(sequence, byteStream);
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteStream.toByteArray());
                    InputStreamWrapper wrapper = new InputStreamWrapper(inputStream, name);
                    putZipEntry(wrapper, zos);
                } else {
                    line[i + 1] = "";
                }
            } catch (Exception e) {
                line[i + 1] = "";
            }
        } else {
            line[i + 1] = "";
        }
        writer.writeNext(line);
    }
}
Also used : FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) Date(java.util.Date) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel) GenbankFormatter(org.jbei.ice.lib.entry.sequence.composers.formatters.GenbankFormatter) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) PartData(org.jbei.ice.lib.dto.entry.PartData)

Example 8 with InputStreamWrapper

use of org.jbei.ice.lib.entry.sequence.InputStreamWrapper in project ice by JBEI.

the class RemoteEntriesAsCSV method writeZip.

private boolean writeZip(File tmpZip, ZipOutputStream zos) {
    try {
        // write the csv file
        FileInputStream fis = new FileInputStream(csvPath.toFile());
        InputStreamWrapper wrapper = new InputStreamWrapper(fis, "entries.csv");
        putZipEntry(wrapper, zos);
        zos.close();
        csvPath = tmpZip.toPath();
        return true;
    } catch (Exception e) {
        Logger.error(e);
        return false;
    }
}
Also used : InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper)

Example 9 with InputStreamWrapper

use of org.jbei.ice.lib.entry.sequence.InputStreamWrapper in project ice by JBEI.

the class RemoteSequence method get.

public InputStreamWrapper get(String type) {
    FeaturedDNASequence featuredDNASequence = remoteContact.getPublicEntrySequence(partner.getUrl(), Long.toString(remotePartId), partner.getApiKey());
    if (featuredDNASequence == null)
        return new InputStreamWrapper(new byte[] { '\0' }, "no_sequence");
    String name = featuredDNASequence.getName();
    try {
        Sequence sequence = SequenceUtil.dnaSequenceToSequence(featuredDNASequence);
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        AbstractFormatter formatter;
        switch(type.toLowerCase()) {
            case "genbank":
            default:
                name = name + ".gb";
                formatter = new GenbankFormatter(name);
                break;
            case "fasta":
                formatter = new FastaFormatter();
                break;
            case "sbol1":
                formatter = new SBOLFormatter();
                name = name + ".xml";
                break;
            case "sbol2":
                formatter = new SBOL2Formatter();
                name = name + ".xml";
                break;
            case "gff3":
                formatter = new GFF3Formatter();
                name = name + ".gff3";
                break;
        }
        formatter.format(sequence, byteStream);
        return new InputStreamWrapper(byteStream.toByteArray(), name);
    } catch (Exception e) {
        Logger.error(e);
        return new InputStreamWrapper(new byte[] { '\0' }, "no_sequence");
    }
}
Also used : InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) Sequence(org.jbei.ice.storage.model.Sequence) FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence)

Example 10 with InputStreamWrapper

use of org.jbei.ice.lib.entry.sequence.InputStreamWrapper in project ice by JBEI.

the class RemoteTransfer method performTransfer.

/**
 * Transfers the sequence file for the part and any parts that are linked to it.
 * If the attached sequence was uploaded as a file or pasted, the system
 * transfers that. If not if attempts to convert the attached sequence to genbank format
 * and transfers that
 *
 * @param partner destination for the sequence transfer
 * @param data    data for part whose sequences are to be transferred
 */
protected void performTransfer(RemotePartner partner, PartData data) {
    String url = partner.getUrl();
    // check main entry for sequence
    if (sequenceDAO.hasSequence(data.getId())) {
        InputStreamWrapper wrapper = new SequenceAsString(SequenceFormat.GENBANK, data.getId(), true).get();
        if (wrapper != null && wrapper.getInputStream() != null) {
            try {
                String sequenceString = new String(wrapper.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
                remoteContact.transferSequence(url, data.getRecordId(), data.getType(), sequenceString);
            } catch (IOException e) {
                Logger.error("Cannot transfer sequence", e);
            }
        }
    }
    // check child entries
    if (data.getLinkedParts() == null)
        return;
    for (PartData linked : data.getLinkedParts()) {
        performTransfer(partner, linked);
    }
}
Also used : InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) PartData(org.jbei.ice.lib.dto.entry.PartData) SequenceAsString(org.jbei.ice.lib.entry.sequence.SequenceAsString) IOException(java.io.IOException) SequenceAsString(org.jbei.ice.lib.entry.sequence.SequenceAsString)

Aggregations

InputStreamWrapper (org.jbei.ice.lib.entry.sequence.InputStreamWrapper)10 EntryFieldLabel (org.jbei.ice.lib.dto.entry.EntryFieldLabel)5 PartSequence (org.jbei.ice.lib.entry.sequence.PartSequence)4 IOException (java.io.IOException)3 FeaturedDNASequence (org.jbei.ice.lib.dto.FeaturedDNASequence)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ZipEntry (java.util.zip.ZipEntry)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 javax.ws.rs (javax.ws.rs)2 MediaType (javax.ws.rs.core.MediaType)2 Response (javax.ws.rs.core.Response)2 StreamingOutput (javax.ws.rs.core.StreamingOutput)2 FileUtils (org.apache.commons.io.FileUtils)2