Search in sources :

Example 1 with ByteArrayWrapper

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

the class RemoteEntriesAsCSV method writeDataEntries.

protected void writeDataEntries(RemotePartner partner, List<PartData> entries, List<EntryField> 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 (EntryField 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 = SequenceController.dnaSequenceToSequence(featuredDNASequence);
                    GenbankFormatter genbankFormatter = new GenbankFormatter(name);
                    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                    genbankFormatter.format(sequence, byteStream);
                    ByteArrayWrapper wrapper = new ByteArrayWrapper(byteStream.toByteArray(), name);
                    putZipEntry(wrapper, zos);
                } else {
                    line[i + 1] = "";
                }
            } catch (Exception e) {
                line[i + 1] = "";
            }
        } else {
            line[i + 1] = "";
        }
        writer.writeNext(line);
    }
}
Also used : GenbankFormatter(org.jbei.ice.lib.entry.sequence.composers.formatters.GenbankFormatter) ByteArrayWrapper(org.jbei.ice.lib.entry.sequence.ByteArrayWrapper) PartData(org.jbei.ice.lib.dto.entry.PartData) FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) Date(java.util.Date) EntryField(org.jbei.ice.lib.dto.entry.EntryField)

Example 2 with ByteArrayWrapper

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

the class FileResource method downloadSequence.

@GET
@Path("{partId}/sequence/{type}")
public Response downloadSequence(@PathParam("partId") final long partId, @PathParam("type") final String downloadType, @DefaultValue("-1") @QueryParam("remoteId") long remoteId, @QueryParam("sid") String sid) {
    if (StringUtils.isEmpty(sessionId))
        sessionId = sid;
    final String userId = getUserId(sessionId);
    final ByteArrayWrapper wrapper;
    if (remoteId != -1) {
        RemoteSequence sequence = new RemoteSequence(remoteId, partId);
        wrapper = sequence.get(downloadType);
    } else {
        wrapper = sequenceController.getSequenceFile(userId, partId, downloadType);
    }
    StreamingOutput stream = output -> {
        final ByteArrayInputStream input = new ByteArrayInputStream(wrapper.getBytes());
        ByteStreams.copy(input, output);
    };
    return addHeaders(Response.ok(stream), wrapper.getName());
}
Also used : RemoteSequence(org.jbei.ice.lib.net.RemoteSequence) AttachmentInfo(org.jbei.ice.lib.dto.entry.AttachmentInfo) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) PartSequence(org.jbei.ice.lib.entry.sequence.PartSequence) Setting(org.jbei.ice.lib.dto.Setting) SequenceAnalysisController(org.jbei.ice.lib.entry.sequence.SequenceAnalysisController) ConfigurationKey(org.jbei.ice.lib.dto.ConfigurationKey) EntryField(org.jbei.ice.lib.dto.entry.EntryField) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Entries(org.jbei.ice.lib.entry.Entries) Sequence(org.jbei.ice.storage.model.Sequence) MediaType(javax.ws.rs.core.MediaType) ConfigurationController(org.jbei.ice.lib.config.ConfigurationController) DAOFactory(org.jbei.ice.storage.DAOFactory) Logger(org.jbei.ice.lib.common.logging.Logger) FileBulkUpload(org.jbei.ice.lib.bulkupload.FileBulkUpload) SequenceInfo(org.jbei.ice.lib.dto.entry.SequenceInfo) PigeonSBOLv(org.jbei.ice.lib.entry.sequence.composers.pigeon.PigeonSBOLv) Entry(org.jbei.ice.storage.model.Entry) EntryType(org.jbei.ice.lib.dto.entry.EntryType) URI(java.net.URI) Utils(org.jbei.ice.lib.utils.Utils) AttachmentController(org.jbei.ice.lib.entry.attachment.AttachmentController) EntrySelection(org.jbei.ice.lib.entry.EntrySelection) FileUtils(org.apache.commons.io.FileUtils) StreamingOutput(javax.ws.rs.core.StreamingOutput) SequenceController(org.jbei.ice.lib.entry.sequence.SequenceController) Collectors(java.util.stream.Collectors) 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) java.io(java.io) Paths(java.nio.file.Paths) ByteStreams(com.google.common.io.ByteStreams) ByteArrayWrapper(org.jbei.ice.lib.entry.sequence.ByteArrayWrapper) ShotgunSequenceDAO(org.jbei.ice.storage.hibernate.dao.ShotgunSequenceDAO) TraceSequence(org.jbei.ice.storage.model.TraceSequence) EntriesAsCSV(org.jbei.ice.lib.entry.EntriesAsCSV) ShotgunSequence(org.jbei.ice.storage.model.ShotgunSequence) ByteArrayWrapper(org.jbei.ice.lib.entry.sequence.ByteArrayWrapper) StreamingOutput(javax.ws.rs.core.StreamingOutput) RemoteSequence(org.jbei.ice.lib.net.RemoteSequence)

Example 3 with ByteArrayWrapper

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

the class RemoteEntriesAsCSV method writeLocalEntries.

protected void writeLocalEntries(List<Entry> entries, List<EntryField> fields, CSVWriter writer, ZipOutputStream zos) {
    if (entries == null)
        return;
    SequenceDAO sequenceDAO = DAOFactory.getSequenceDAO();
    Configuration configuration = DAOFactory.getConfigurationDAO().get(ConfigurationKey.URI_PREFIX);
    String thisUrl = configuration == null ? "" : configuration.getValue();
    for (Entry entry : entries) {
        String[] line = new String[fields.size() + 4];
        line[0] = thisUrl;
        line[1] = entry.getCreationTime().toString();
        line[2] = entry.getPartNumber();
        int i = 2;
        for (EntryField field : fields) {
            line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
            i += 1;
        }
        // write sequence to zip file
        long entryId = entry.getId();
        if (sequenceDAO.hasSequence(entryId)) {
            String name = entry.getPartNumber() + ".gb";
            try {
                Sequence sequence = sequenceDAO.getByEntry(entry);
                line[i + 1] = name;
                GenbankFormatter genbankFormatter = new GenbankFormatter(name);
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                genbankFormatter.format(sequence, byteStream);
                ByteArrayWrapper wrapper = new ByteArrayWrapper(byteStream.toByteArray(), name);
                putZipEntry(wrapper, zos);
            } catch (Exception e) {
                line[i + 1] = "";
            }
        } else {
            line[i + 1] = "";
        }
        writer.writeNext(line);
    }
}
Also used : FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) EntryField(org.jbei.ice.lib.dto.entry.EntryField) GenbankFormatter(org.jbei.ice.lib.entry.sequence.composers.formatters.GenbankFormatter) ZipEntry(java.util.zip.ZipEntry) ByteArrayWrapper(org.jbei.ice.lib.entry.sequence.ByteArrayWrapper) SequenceDAO(org.jbei.ice.storage.hibernate.dao.SequenceDAO)

Example 4 with ByteArrayWrapper

use of org.jbei.ice.lib.entry.sequence.ByteArrayWrapper 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());
        ByteArrayWrapper wrapper = new ByteArrayWrapper(IOUtils.toByteArray(fis), "entries.csv");
        putZipEntry(wrapper, zos);
        zos.close();
        csvPath = tmpZip.toPath();
        return true;
    } catch (Exception e) {
        Logger.error(e);
        return false;
    }
}
Also used : ByteArrayWrapper(org.jbei.ice.lib.entry.sequence.ByteArrayWrapper)

Example 5 with ByteArrayWrapper

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

the class RemoteSequence method get.

public ByteArrayWrapper get(String type) {
    FeaturedDNASequence featuredDNASequence = remoteContact.getPublicEntrySequence(partner.getUrl(), Long.toString(remotePartId), partner.getApiKey());
    if (featuredDNASequence == null)
        return new ByteArrayWrapper(new byte[] { '\0' }, "no_sequence");
    String name = featuredDNASequence.getName();
    try {
        Sequence sequence = SequenceController.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 "pigeoni":
                URI uri = PigeonSBOLv.generatePigeonVisual(sequence);
                byte[] bytes = IOUtils.toByteArray(uri.toURL().openStream());
                return new ByteArrayWrapper(bytes, name + ".png");
            case "pigeons":
                String sequenceString = PigeonSBOLv.generatePigeonScript(sequence);
                name = name + ".txt";
                return new ByteArrayWrapper(sequenceString.getBytes(), name);
        }
        formatter.format(sequence, byteStream);
        return new ByteArrayWrapper(byteStream.toByteArray(), name);
    } catch (Exception e) {
        Logger.error(e);
        return new ByteArrayWrapper(new byte[] { '\0' }, "no_sequence");
    }
}
Also used : 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) URI(java.net.URI) ByteArrayWrapper(org.jbei.ice.lib.entry.sequence.ByteArrayWrapper)

Aggregations

ByteArrayWrapper (org.jbei.ice.lib.entry.sequence.ByteArrayWrapper)6 FeaturedDNASequence (org.jbei.ice.lib.dto.FeaturedDNASequence)3 EntryField (org.jbei.ice.lib.dto.entry.EntryField)3 URI (java.net.URI)2 SequenceController (org.jbei.ice.lib.entry.sequence.SequenceController)2 GenbankFormatter (org.jbei.ice.lib.entry.sequence.composers.formatters.GenbankFormatter)2 ByteStreams (com.google.common.io.ByteStreams)1 java.io (java.io)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 javax.ws.rs (javax.ws.rs)1 MediaType (javax.ws.rs.core.MediaType)1 Response (javax.ws.rs.core.Response)1