Search in sources :

Example 1 with CSVReader

use of com.opencsv.CSVReader in project airpal by airbnb.

the class ResultsPreviewResource method getS3Preview.

private Response getS3Preview(URI fileURI, int numLines) {
    val filename = getFilename(fileURI);
    val outputKey = getOutputKey(filename);
    // download ~100 kb (depending on your definition) of the file
    val request = new GetObjectRequest(outputBucket, outputKey).withRange(0, 100 * 1024);
    val object = s3Client.getObject(request);
    ObjectMetadata objectMetadata = object.getObjectMetadata();
    boolean gzip = "gzip".equalsIgnoreCase(objectMetadata.getContentEncoding());
    try (InputStream input = object.getObjectContent()) {
        InputStreamReader reader;
        if (gzip) {
            reader = new InputStreamReader(new GZIPInputStream(input));
        } else {
            reader = new InputStreamReader(input);
        }
        return getPreviewFromCSV(new CSVReader(reader), numLines);
    } catch (IOException e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : lombok.val(lombok.val) GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 2 with CSVReader

use of com.opencsv.CSVReader in project ice by JBEI.

the class BlastPlus method processFeaturesBlastOutput.

/**
     * 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
     */
public static List<DNAFeature> processFeaturesBlastOutput(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 e) {
        Logger.error(e);
        return null;
    }
}
Also used : CSVReader(com.opencsv.CSVReader) DNAFeatureLocation(org.jbei.ice.lib.dto.DNAFeatureLocation) DNAFeature(org.jbei.ice.lib.dto.DNAFeature)

Example 3 with CSVReader

use of com.opencsv.CSVReader in project ice by JBEI.

the class BlastPlus 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 static 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);
            info.setQueryLength(queryLength);
            String idString = Long.toString(info.getEntryInfo().getId());
            SearchResult currentResult = hashMap.get(idString);
            // if there is an existing record for same entry with a lower relative score then replace
            if (currentResult == null)
                hashMap.put(idString, info);
        }
    } catch (IOException e) {
        Logger.error(e);
        return null;
    }
    return hashMap;
}
Also used : CSVReader(com.opencsv.CSVReader) SearchResult(org.jbei.ice.lib.dto.search.SearchResult)

Example 4 with CSVReader

use of com.opencsv.CSVReader in project pictureapp by EyeSeeTea.

the class PopulateDB method addOptionTextSize.

public static void addOptionTextSize(Context context) throws IOException {
    List<Option> options = Option.getAllOptions();
    //Reset inner references
    cleanInnerLists();
    CSVReader reader = new CSVReader(new InputStreamReader(context.openFileInput(OPTION_ATTRIBUTES_CSV)), SEPARATOR, QUOTECHAR);
    CSVReader readerOptions = new CSVReader(new InputStreamReader(context.openFileInput(OPTIONS_CSV)), SEPARATOR, QUOTECHAR);
    //Remove bad optionAttributes.
    Delete.tables(OptionAttribute.class);
    String[] line;
    //save new optionattributes
    while ((line = reader.readNext()) != null) {
        OptionAttribute optionAttribute = new OptionAttribute();
        optionAttribute.setBackground_colour(line[1]);
        optionAttribute.setPath(line[2]);
        if (line.length > 3 && !line[3].equals("")) {
            optionAttribute.setHorizontal_alignment(Integer.valueOf(line[3]));
        } else {
            optionAttribute.setHorizontal_alignment(OptionAttribute.DEFAULT_HORIZONTAL_ALIGNMENT);
        }
        if (line.length > 4 && !line[4].equals("")) {
            optionAttribute.setVertical_alignment(Integer.valueOf(line[4]));
        } else {
            optionAttribute.setVertical_alignment(OptionAttribute.DEFAULT_VERTICAL_ALIGNMENT);
        }
        if (line.length > 5 && !line[5].equals("")) {
            optionAttribute.setText_size(Integer.valueOf(line[5]));
        } else {
            optionAttribute.setText_size(Integer.parseInt(PreferencesState.getInstance().getContext().getResources().getString(R.string.default_option_text_size)));
        }
        if (line.length > 6 && !line[6].equals("")) {
            optionAttribute.setDefaultOption(Integer.valueOf(line[6]));
        } else {
            optionAttribute.setDefaultOption(0);
        }
        optionAttribute.save();
        optionAttributeList.put(Integer.valueOf(line[0]), optionAttribute);
    }
    line = null;
    //Save new optionattributes for each question
    while ((line = readerOptions.readNext()) != null) {
        for (Option option : options) {
            if (String.valueOf(option.getId_option()).equals(line[0])) {
                if (!line[5].equals("")) {
                    option.setOptionAttribute(optionAttributeList.get(Integer.valueOf(line[5])));
                    option.save();
                }
                break;
            }
        }
    }
    reader.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) QuestionOption(org.eyeseetea.malariacare.data.database.model.QuestionOption) Option(org.eyeseetea.malariacare.data.database.model.Option) OptionAttribute(org.eyeseetea.malariacare.data.database.model.OptionAttribute)

Example 5 with CSVReader

use of com.opencsv.CSVReader in project pictureapp by EyeSeeTea.

the class PopulateDB method updateOptionNames.

public static void updateOptionNames(Context context) throws IOException {
    List<Option> options = Option.getAllOptions();
    //Reset inner references
    cleanInnerLists();
    CSVReader reader = new CSVReader(new InputStreamReader(context.openFileInput(OPTIONS_CSV)), SEPARATOR, QUOTECHAR);
    String[] line;
    //Save new option name for each option
    while ((line = reader.readNext()) != null) {
        for (Option option : options) {
            if (String.valueOf(option.getId_option()).equals(line[0])) {
                option.setCode(line[1]);
                option.setName(line[2]);
                option.save();
                break;
            }
        }
    }
    reader.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) QuestionOption(org.eyeseetea.malariacare.data.database.model.QuestionOption) Option(org.eyeseetea.malariacare.data.database.model.Option)

Aggregations

CSVReader (com.opencsv.CSVReader)57 InputStreamReader (java.io.InputStreamReader)50 ArrayList (java.util.ArrayList)23 HashMap (java.util.HashMap)18 Question (org.eyeseetea.malariacare.data.database.model.Question)11 QuestionOption (org.eyeseetea.malariacare.data.database.model.QuestionOption)9 Match (org.eyeseetea.malariacare.data.database.model.Match)8 Option (org.eyeseetea.malariacare.data.database.model.Option)8 Answer (org.eyeseetea.malariacare.data.database.model.Answer)6 OptionAttribute (org.eyeseetea.malariacare.data.database.model.OptionAttribute)6 TreatmentMatch (org.eyeseetea.malariacare.data.database.model.TreatmentMatch)6 FileReader (java.io.FileReader)5 IOException (java.io.IOException)5 QuestionRelation (org.eyeseetea.malariacare.data.database.model.QuestionRelation)5 Treatment (org.eyeseetea.malariacare.data.database.model.Treatment)5 Drug (org.eyeseetea.malariacare.data.database.model.Drug)4 Organisation (org.eyeseetea.malariacare.data.database.model.Organisation)4 StringKey (org.eyeseetea.malariacare.data.database.model.StringKey)4 PopulateRow.populateMatch (org.eyeseetea.malariacare.data.database.utils.populatedb.PopulateRow.populateMatch)4 File (java.io.File)3