Search in sources :

Example 41 with LabeledCSVParser

use of com.Ostermiller.util.LabeledCSVParser in project eol-globi-data by jhpoelen.

the class StudyImporterForBrose method importStudy.

@Override
public void importStudy() throws StudyImporterException {
    LabeledCSVParser dataParser;
    try {
        dataParser = parserFactory.createParser(RESOURCE_PATH, CharsetConstant.UTF8);
    } catch (IOException e) {
        throw new StudyImporterException("failed to read resource [" + RESOURCE_PATH + "]", e);
    }
    dataParser.changeDelimiter('\t');
    Map<String, String> refMap = ReferenceUtil.buildRefMap(parserFactory, REFERENCE_PATH);
    try {
        while (dataParser.getLine() != null) {
            if (importFilter.shouldImportRecord((long) dataParser.getLastLineNumber())) {
                importLine(dataParser, refMap);
            }
        }
    } catch (IOException e) {
        throw new StudyImporterException("problem importing study at line [" + dataParser.lastLineNumber() + "]", e);
    }
}
Also used : LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) IOException(java.io.IOException)

Example 42 with LabeledCSVParser

use of com.Ostermiller.util.LabeledCSVParser in project eol-globi-data by jhpoelen.

the class StudyImporterForBaremore method importStudy.

@Override
public void importStudy() throws StudyImporterException {
    Study study;
    try {
        LabeledCSVParser parser = parserFactory.createParser(DATA_SOURCE, CharsetConstant.UTF8);
        String[] line;
        study = nodeFactory.getOrCreateStudy(new StudyImpl("Baremore 2010", StudyImporterForGoMexSI2.GOMEXI_SOURCE_DESCRIPTION, "doi:10.3354/ab00214", ExternalIdUtil.toCitation("Ivy E. Baremore", "Prey Selection By The Atlantic Angel Shark Squatina Dumeril In The Northeastern Gulf Of Mexico.", "2010")));
        Location collectionLocation = nodeFactory.getOrCreateLocation(new LocationImpl(29.219302, -87.06665, null, null));
        Map<Integer, Specimen> specimenMap = new HashMap<Integer, Specimen>();
        while ((line = parser.getLine()) != null) {
            Integer sharkId = Integer.parseInt(line[0]);
            String collectionDateString = line[1];
            if (isBlank(collectionDateString)) {
                getLogger().warn(study, "line [" + parser.getLastLineNumber() + "] in [" + DATA_SOURCE + "]: missing collection date");
            } else {
                Specimen predatorSpecimen = specimenMap.get(sharkId);
                if (predatorSpecimen == null) {
                    predatorSpecimen = nodeFactory.createSpecimen(study, new TaxonImpl("Squatina dumeril", null));
                    predatorSpecimen.caughtIn(collectionLocation);
                    addLifeStage(parser, predatorSpecimen);
                    addCollectionDate(collectionDateString, predatorSpecimen);
                }
                specimenMap.put(sharkId, predatorSpecimen);
                String totalLengthInCm = line[3];
                try {
                    Double lengthInMm = Double.parseDouble(totalLengthInCm) * 10.0;
                    predatorSpecimen.setLengthInMm(lengthInMm);
                } catch (NumberFormatException ex) {
                    throw new StudyImporterException("failed to parse length [" + totalLengthInCm);
                }
                String preySpeciesDescription = line[7];
                if (StringUtils.isBlank(preySpeciesDescription)) {
                    getLogger().info(study, "found blank prey species description [" + preySpeciesDescription + "] on line [" + parser.lastLineNumber() + "]");
                } else {
                    Specimen preySpecimen = nodeFactory.createSpecimen(study, new TaxonImpl(preySpeciesDescription, null));
                    preySpecimen.caughtIn(collectionLocation);
                    predatorSpecimen.ate(preySpecimen);
                    nodeFactory.setUnixEpochProperty(preySpecimen, nodeFactory.getUnixEpochProperty(predatorSpecimen));
                }
            }
        }
    } catch (IOException e) {
        throw new StudyImporterException("failed to parse labels", e);
    }
}
Also used : Study(org.eol.globi.domain.Study) HashMap(java.util.HashMap) TaxonImpl(org.eol.globi.domain.TaxonImpl) StudyImpl(org.eol.globi.domain.StudyImpl) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) IOException(java.io.IOException) Specimen(org.eol.globi.domain.Specimen) LocationImpl(org.eol.globi.domain.LocationImpl) Location(org.eol.globi.domain.Location)

Example 43 with LabeledCSVParser

use of com.Ostermiller.util.LabeledCSVParser in project eol-globi-data by jhpoelen.

the class StudyImporterForBlewett method parsePredatorPreyInteraction.

private void parsePredatorPreyInteraction(Study study, Map<String, Location> locationMap, Map<String, Date> collectionTimeMap) throws IOException, NodeFactoryException, TermLookupServiceException {
    LabeledCSVParser parser = parserFactory.createParser("blewett/SnookDietData2000_02_Charlotte_Harbor_FL_Blewett_numeric_abundance.csv", CharsetConstant.UTF8);
    String[] header = parser.getLabels();
    String[] line;
    while ((line = parser.getLine()) != null) {
        if (line.length < 2) {
            break;
        }
        Specimen predatorSpecimen = addPredator(study, parser, line);
        List<Specimen> specimen = addPreyForPredator(header, line, study);
        for (Specimen prey : specimen) {
            predatorSpecimen.ate(prey);
        }
        String collectionCode = parser.getValueByLabel(COLLECTION_NO);
        if (collectionCode != null) {
            specimen.add(predatorSpecimen);
            setLocationAndDate(locationMap, collectionTimeMap, specimen, collectionCode);
        }
    }
}
Also used : Specimen(org.eol.globi.domain.Specimen) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser)

Example 44 with LabeledCSVParser

use of com.Ostermiller.util.LabeledCSVParser in project eol-globi-data by jhpoelen.

the class StudyImporterForBlewett method buildLocationTimeMaps.

private void buildLocationTimeMaps(Map<String, Location> collectionLocationMap, Map<String, Date> collectionTimeMap, Study study) throws IOException, StudyImporterException {
    LabeledCSVParser locationParser = parserFactory.createParser("blewett/SnookDietData2000_02_Charlotte_Harbor_FL_Blewett_date_and_abiotic.csv", CharsetConstant.UTF8);
    String[] line;
    while ((line = locationParser.getLine()) != null) {
        if (line.length < 3) {
            getLogger().warn(study, "line:" + locationParser.getLastLineNumber() + " [" + StringUtils.join(line, ",") + "] has missing location information");
        }
        String collectionCode = locationParser.getValueByLabel(COLLECTION_NO);
        if (StringUtils.isBlank(collectionCode)) {
            getLogger().warn(study, "blank location code for line: [" + locationParser.getLastLineNumber() + "]");
        }
        String latitude = locationParser.getValueByLabel("Latitude");
        if (StringUtils.isBlank(latitude)) {
            getLogger().warn(study, "blank value for lattitude for line: [" + locationParser.getLastLineNumber() + "]");
        }
        String longitude = locationParser.getValueByLabel("Longitude");
        if (StringUtils.isBlank(longitude)) {
            getLogger().warn(study, "blank value for longitude for line: [" + locationParser.getLastLineNumber() + "]");
        }
        Location location;
        try {
            location = nodeFactory.getOrCreateLocation(new LocationImpl(Double.parseDouble(latitude), Double.parseDouble(longitude), 0.0, null));
        } catch (NodeFactoryException e) {
            throw new StudyImporterException("failed to create location", e);
        }
        collectionLocationMap.put(collectionCode, location);
        String timeString = locationParser.getValueByLabel("Time");
        if (StringUtils.isBlank(timeString)) {
            getLogger().warn(study, "blank value for time for line: [" + locationParser.getLastLineNumber() + "]");
        }
        String dateString = locationParser.getValueByLabel("Date");
        if (StringUtils.isBlank(dateString)) {
            getLogger().warn(study, "blank value for date for line: [" + locationParser.getLastLineNumber() + "]");
        }
        String dateTimeString = dateString + " " + timeString;
        try {
            Date dateTime = parseDateString(dateTimeString);
            collectionTimeMap.put(collectionCode, dateTime);
        } catch (ParseException e) {
            throw new StudyImporterException("failed to parse date time [" + dateTimeString + "] for collection [" + collectionCode + "] on line [" + locationParser.getLastLineNumber() + "]");
        }
    }
}
Also used : LocationImpl(org.eol.globi.domain.LocationImpl) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) ParseException(java.text.ParseException) Date(java.util.Date) Location(org.eol.globi.domain.Location)

Example 45 with LabeledCSVParser

use of com.Ostermiller.util.LabeledCSVParser in project eol-globi-data by jhpoelen.

the class StudyImporterForBarnes method importStudy.

@Override
public void importStudy() throws StudyImporterException {
    LabeledCSVParser dataParser;
    try {
        dataParser = parserFactory.createParser(RESOURCE_PATH, CharsetConstant.UTF8);
    } catch (IOException e) {
        throw new StudyImporterException("failed to read resource [" + RESOURCE_PATH + "]", e);
    }
    dataParser.changeDelimiter('\t');
    Map<String, String> refMap = ReferenceUtil.buildRefMap(parserFactory, REFERENCE_PATH);
    try {
        while (dataParser.getLine() != null) {
            if (importFilter.shouldImportRecord((long) dataParser.getLastLineNumber())) {
                importLine(dataParser, refMap);
            }
        }
    } catch (IOException e) {
        throw new StudyImporterException("problem importing study at line [" + dataParser.lastLineNumber() + "]", e);
    }
}
Also used : LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) IOException(java.io.IOException)

Aggregations

LabeledCSVParser (com.Ostermiller.util.LabeledCSVParser)82 IOException (java.io.IOException)40 Test (org.junit.Test)31 Study (org.eol.globi.domain.Study)24 StudyImpl (org.eol.globi.domain.StudyImpl)17 Specimen (org.eol.globi.domain.Specimen)15 HashMap (java.util.HashMap)13 ArrayList (java.util.ArrayList)12 Location (org.eol.globi.domain.Location)12 TaxonImpl (org.eol.globi.domain.TaxonImpl)12 CSVParser (com.Ostermiller.util.CSVParser)10 StringReader (java.io.StringReader)8 LocationImpl (org.eol.globi.domain.LocationImpl)8 Taxon (org.eol.globi.domain.Taxon)8 InteractType (org.eol.globi.domain.InteractType)7 File (java.io.File)6 FileInputStream (java.io.FileInputStream)6 InputStream (java.io.InputStream)6 Date (java.util.Date)6 List (java.util.List)6