Search in sources :

Example 6 with EntryFieldLabel

use of org.jbei.ice.lib.dto.entry.EntryFieldLabel in project ice by JBEI.

the class EntriesAsCSV method writeList.

/**
 * Iterate through list of entries and extract values
 *
 * @throws IOException on Exception write values to file
 */
private void writeList(EntryFieldLabel... fields) throws IOException {
    // filter entries based on what the user is allowed to see if the user is not an admin
    Account account = this.accountDAO.getByEmail(userId);
    if (account.getType() != AccountType.ADMIN) {
        List<Group> accountGroups = new GroupController().getAllGroups(account);
        entries = permissionDAO.getCanReadEntries(account, accountGroups, entries);
    }
    if (entries == null) {
        Logger.warn("No entries to convert to csv format");
        return;
    }
    // write headers
    Path tmpPath = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY));
    File tmpFile = File.createTempFile("ice-", ".csv", tmpPath.toFile());
    csvPath = tmpFile.toPath();
    FileWriter fileWriter = new FileWriter(tmpFile);
    if (fields == null || fields.length == 0)
        fields = getEntryFields();
    String[] headers = getCSVHeaders(fields);
    Set<Long> sequenceSet = new HashSet<>();
    try (CSVWriter writer = new CSVWriter(fileWriter)) {
        writer.writeNext(headers);
        // write entry fields
        for (long entryId : entries) {
            Entry entry = dao.get(entryId);
            // get contents and write data out
            String[] line = new String[fields.length + 4];
            line[0] = entry.getCreationTime().toString();
            line[1] = entry.getPartNumber();
            // write field values
            int i = 1;
            for (EntryFieldLabel field : fields) {
                line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
                i += 1;
            }
            // write sequence information
            if (this.includeSequences && sequenceDAO.hasSequence(entryId)) {
                line[i + 1] = getSequenceName(entry);
                sequenceSet.add(entryId);
            } else {
                line[i + 1] = "";
            }
            // get parents for entry
            EntryLinks links = new EntryLinks(this.userId, entry.getPartNumber());
            StringBuilder parents = new StringBuilder();
            for (PartData data : links.getParents()) {
                parents.append(data.getPartId()).append(",");
            }
            if (!StringUtils.isEmpty(parents.toString()))
                parents = new StringBuilder(parents.substring(0, parents.length() - 1));
            line[i + 2] = ("\"" + parents + "\"");
            // write line
            writer.writeNext(line);
        }
    }
    if (!sequenceSet.isEmpty())
        writeZip(sequenceSet);
}
Also used : Path(java.nio.file.Path) Account(org.jbei.ice.storage.model.Account) Group(org.jbei.ice.storage.model.Group) GroupController(org.jbei.ice.lib.group.GroupController) CSVWriter(com.opencsv.CSVWriter) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel) Entry(org.jbei.ice.storage.model.Entry) ZipEntry(java.util.zip.ZipEntry) PartData(org.jbei.ice.lib.dto.entry.PartData) HashSet(java.util.HashSet)

Example 7 with EntryFieldLabel

use of org.jbei.ice.lib.dto.entry.EntryFieldLabel in project ice by JBEI.

the class EntriesAsCSV method getEntryFields.

private EntryFieldLabel[] getEntryFields() {
    Set<String> recordTypes = new HashSet<>(dao.getRecordTypes(entries));
    List<EntryFieldLabel> fields = EntryFields.getCommonFields();
    for (String recordType : recordTypes) {
        EntryType type = EntryType.nameToType(recordType);
        if (type == null) {
            Logger.error("Could not convert entry type " + recordType);
            continue;
        }
        switch(type) {
            case SEED:
                EntryFields.addArabidopsisSeedHeaders(fields);
                break;
            case STRAIN:
                EntryFields.addStrainHeaders(fields);
                break;
            case PLASMID:
                EntryFields.addPlasmidHeaders(fields);
                break;
            case PROTEIN:
                EntryFields.addProteinHeaders(fields);
                break;
        }
    }
    return fields.toArray(new EntryFieldLabel[0]);
}
Also used : EntryType(org.jbei.ice.lib.dto.entry.EntryType) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel) HashSet(java.util.HashSet)

Example 8 with EntryFieldLabel

use of org.jbei.ice.lib.dto.entry.EntryFieldLabel in project ice by JBEI.

the class EntriesAsCSV method customize.

public ByteArrayOutputStream customize(EntrySelection selection, SequenceFormat format, boolean onePerFolder) throws IOException {
    Entries retriever = new Entries(this.userId);
    this.entries = retriever.getEntriesFromSelectionContext(selection);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ZipOutputStream zos = new ZipOutputStream(baos)) {
        for (long entryId : this.entries) {
            // get the entry
            Entry entry = DAOFactory.getEntryDAO().get(entryId);
            if (entry == null) {
                // write to csv file
                Logger.error("ERROR : no entry " + entryId);
                continue;
            }
            if (!sequenceDAO.hasSequence(entry.getId())) {
                continue;
            }
            // get the sequence
            InputStreamWrapper wrapper = new PartSequence(userId, Long.toString(entryId)).toFile(format, onePerFolder);
            if (wrapper == null) {
                Logger.error("ERROR : no sequence " + entryId);
                continue;
            }
            if (onePerFolder)
                wrapper.setName(entry.getPartNumber() + File.separatorChar + wrapper.getName());
            else
                wrapper.setName(wrapper.getName());
            putZipEntry(wrapper, zos);
        }
        this.includeSequences = false;
        writeList(selection.getFields().toArray(new EntryFieldLabel[0]));
        // write the csv file to zip file
        FileInputStream fis = new FileInputStream(csvPath.toFile());
        InputStreamWrapper wrapper = new InputStreamWrapper(fis, "entries.csv");
        putZipEntry(wrapper, zos);
    }
    return baos;
}
Also used : Entry(org.jbei.ice.storage.model.Entry) ZipEntry(java.util.zip.ZipEntry) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) ZipOutputStream(java.util.zip.ZipOutputStream) PartSequence(org.jbei.ice.lib.entry.sequence.PartSequence) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel)

Example 9 with EntryFieldLabel

use of org.jbei.ice.lib.dto.entry.EntryFieldLabel in project ice by JBEI.

the class EntriesAsCSV method getCSVHeaders.

protected String[] getCSVHeaders(EntryFieldLabel[] fields) {
    // get headers
    String[] headers = new String[fields.length + 4];
    headers[0] = "Created";
    headers[1] = "Part ID";
    int i = 1;
    for (EntryFieldLabel field : fields) {
        i += 1;
        headers[i] = field.getLabel();
    }
    headers[i + 1] = "Sequence File";
    headers[i + 2] = "Parent IDs";
    return headers;
}
Also used : EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel)

Example 10 with EntryFieldLabel

use of org.jbei.ice.lib.dto.entry.EntryFieldLabel 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)

Aggregations

EntryFieldLabel (org.jbei.ice.lib.dto.entry.EntryFieldLabel)12 PartData (org.jbei.ice.lib.dto.entry.PartData)5 ZipEntry (java.util.zip.ZipEntry)4 HashSet (java.util.HashSet)3 InputStreamWrapper (org.jbei.ice.lib.entry.sequence.InputStreamWrapper)3 Entry (org.jbei.ice.storage.model.Entry)3 CSVWriter (com.opencsv.CSVWriter)2 Path (java.nio.file.Path)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 FeaturedDNASequence (org.jbei.ice.lib.dto.FeaturedDNASequence)2 GenbankFormatter (org.jbei.ice.lib.entry.sequence.composers.formatters.GenbankFormatter)2 GroupController (org.jbei.ice.lib.group.GroupController)2 Account (org.jbei.ice.storage.model.Account)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 ZipInputStream (java.util.zip.ZipInputStream)1 Setting (org.jbei.ice.lib.dto.Setting)1