Search in sources :

Example 1 with CsvException

use of com.opencsv.exceptions.CsvException in project ice by JBEI.

the class FeaturesBlastDatabase method processBlastOutput.

/**
 * Process the output of the blast run for features
 * into a list of feature objects
 * <br>
 * Expected format for the output (per line) is
 * <code>feature_id, label, type, qstart, qend, sstart, send, sstrand</code>
 * Therefore line[0] is feature_id, line[1] is label etc
 * <br>Since we are only interested in features that have a full match (covers entire feature) some matches are
 * manually eliminated. The results returned by blast can cover only a subset of the sequence. e.g.
 * given query = 'ATGC' and feature1 = 'ATG' and feature2 = 'TATGT', the query will return
 * 1,3,1,3 and 1,3,2,4.
 *
 * @param blastOutput blast program output
 * @return list of feature objects resulting from processing the blast output
 */
private List<DNAFeature> processBlastOutput(String blastOutput) {
    List<DNAFeature> hashMap = new ArrayList<>();
    HashSet<String> duplicates = new HashSet<>();
    try (CSVReader reader = new CSVReader(new StringReader(blastOutput))) {
        List<String[]> lines = reader.readAll();
        for (String[] line : lines) {
            if (line.length != 9) {
                continue;
            }
            long id = Long.decode(line[0]);
            String label = line[1];
            String type = line[2];
            int strand = Integer.decode(line[3]);
            int queryStart = Integer.decode(line[4]);
            int queryEnd = Integer.decode(line[5]);
            int subjectStart = Integer.decode(line[6]);
            int subjectEnd = Integer.decode(line[7]);
            if (!duplicates.add(label + ":" + queryStart + ":" + queryEnd + ":" + strand)) {
                continue;
            }
            if (subjectStart != 1 && (queryEnd - queryStart) + 1 != subjectEnd)
                continue;
            // check for full feature coverage
            DNAFeature dnaFeature = new DNAFeature();
            dnaFeature.setId(id);
            dnaFeature.setName(label);
            dnaFeature.setType(type);
            DNAFeatureLocation location = new DNAFeatureLocation();
            location.setGenbankStart(queryStart);
            location.setEnd(queryEnd);
            dnaFeature.getLocations().add(location);
            dnaFeature.setStrand(strand);
            hashMap.add(dnaFeature);
        }
        return hashMap;
    } catch (IOException | CsvException e) {
        Logger.error(e);
        return null;
    }
}
Also used : CSVReader(com.opencsv.CSVReader) DNAFeatureLocation(org.jbei.ice.lib.dto.DNAFeatureLocation) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StringReader(java.io.StringReader) DNAFeature(org.jbei.ice.lib.dto.DNAFeature) HashSet(java.util.HashSet) CsvException(com.opencsv.exceptions.CsvException)

Example 2 with CsvException

use of com.opencsv.exceptions.CsvException in project ice by JBEI.

the class StandardBlastDatabase method processBlastOutput.

/**
 * Processes the result of a blast search
 *
 * @param blastOutput result output from running blast on the command line
 * @param queryLength length of query sequence
 * @return mapping of entryId to search result object containing information about the blast search for that particular hit
 */
private LinkedHashMap<String, SearchResult> processBlastOutput(String blastOutput, int queryLength) {
    LinkedHashMap<String, SearchResult> hashMap = new LinkedHashMap<>();
    try (CSVReader reader = new CSVReader(new StringReader(blastOutput))) {
        List<String[]> lines = reader.readAll();
        reader.close();
        for (String[] line : lines) {
            SearchResult info = parseBlastOutputLine(line);
            if (info == null)
                continue;
            info.setQueryLength(queryLength);
            String idString = Long.toString(info.getEntryInfo().getId());
            // if there is an existing record for same entry with a lower relative score then replace
            hashMap.putIfAbsent(idString, info);
        }
    } catch (IOException | CsvException e) {
        Logger.error(e);
        return null;
    }
    return hashMap;
}
Also used : CSVReader(com.opencsv.CSVReader) StringReader(java.io.StringReader) SearchResult(org.jbei.ice.lib.dto.search.SearchResult) IOException(java.io.IOException) CsvException(com.opencsv.exceptions.CsvException)

Example 3 with CsvException

use of com.opencsv.exceptions.CsvException in project ice by JBEI.

the class Entries method validateEntries.

/**
 * Validate list of entries in a csv file either via names or partnumbers
 *
 * @param stream    csv file input stream
 * @param checkName whether to check names or part numbers
 */
public List<ParsedEntryId> validateEntries(InputStream stream, boolean checkName) throws IOException {
    List<ParsedEntryId> accepted = new ArrayList<>();
    EntryAuthorization authorization = new EntryAuthorization();
    try (CSVReader reader = new CSVReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
        List<String[]> results = reader.readAll();
        for (String[] result : results) {
            if (result[0].isEmpty())
                continue;
            List<Entry> entries;
            if (checkName) {
                entries = dao.getByName(result[0].trim());
            } else {
                Entry entry = dao.getByPartNumber(result[0].trim());
                entries = new ArrayList<>(1);
                if (entry != null)
                    entries.add(entry);
            }
            if (entries.isEmpty())
                accepted.add(new ParsedEntryId(result[0], null));
            else {
                for (Entry e : entries) {
                    if (!authorization.canRead(this.userId, e)) {
                        accepted.add(new ParsedEntryId(result[0], null));
                        continue;
                    }
                    PartData partData = new PartData(EntryType.nameToType(e.getRecordType()));
                    partData.setPartId(e.getPartNumber());
                    partData.setId(e.getId());
                    accepted.add(new ParsedEntryId(result[0], partData));
                }
            }
        }
    } catch (CsvException exception) {
        Logger.error("Exception reading file: " + exception);
    }
    return accepted;
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) CsvException(com.opencsv.exceptions.CsvException)

Example 4 with CsvException

use of com.opencsv.exceptions.CsvException in project cu-kfs by CU-CommunityApps.

the class PaymentWorksUploadSuppliersServiceImpl method generateVendorCsvDataForUpload.

private byte[] generateVendorCsvDataForUpload(Collection<PaymentWorksVendor> vendors) {
    ByteArrayOutputStream outputStream = null;
    OutputStreamWriter streamWriter = null;
    BufferedWriter bufferedWriter = null;
    try {
        outputStream = new ByteArrayOutputStream();
        streamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
        bufferedWriter = new BufferedWriter(streamWriter);
        writeVendorsToCsvFormat(bufferedWriter, vendors);
        bufferedWriter.flush();
    } catch (IOException | CsvException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(bufferedWriter);
        IOUtils.closeQuietly(streamWriter);
        IOUtils.closeQuietly(outputStream);
    }
    return outputStream.toByteArray();
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) CsvException(com.opencsv.exceptions.CsvException)

Aggregations

CsvException (com.opencsv.exceptions.CsvException)4 CSVReader (com.opencsv.CSVReader)3 IOException (java.io.IOException)3 StringReader (java.io.StringReader)2 BufferedWriter (java.io.BufferedWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 DNAFeature (org.jbei.ice.lib.dto.DNAFeature)1 DNAFeatureLocation (org.jbei.ice.lib.dto.DNAFeatureLocation)1 SearchResult (org.jbei.ice.lib.dto.search.SearchResult)1