Search in sources :

Example 51 with LabeledCSVParser

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

the class StudyImporterForWrast method createLocationMap.

private void createLocationMap() throws StudyImporterException {
    Map<String, List<LatLng>> locations = new HashMap<String, List<LatLng>>();
    LabeledCSVParser parser = null;
    try {
        parser = parserFactory.createParser(LAVACA_BAY_LOCATIONS, CharsetConstant.UTF8);
        while (parser.getLine() != null) {
            String habitateDef = parser.getValueByLabel(COLUMN_MAPPER.get(HABITAT));
            String regionDef = parser.getValueByLabel(COLUMN_MAPPER.get(REGION));
            String siteDef = parser.getValueByLabel(COLUMN_MAPPER.get(SITE));
            String latitude = parser.getValueByLabel("Latitude");
            if (null == latitude) {
                throw new StudyImporterException(createMsgPrefix(parser) + " missing latitude");
            }
            String longitude = parser.getValueByLabel("Longitude");
            if (null == longitude) {
                throw new StudyImporterException(createMsgPrefix(parser) + " missing longitude");
            }
            Double lat;
            try {
                lat = Double.parseDouble(latitude);
            } catch (NumberFormatException ex) {
                throw new StudyImporterException(createMsgPrefix(parser) + " invalid latitude [" + latitude + "]");
            }
            Double lng;
            try {
                lng = Double.parseDouble(longitude);
            } catch (NumberFormatException ex) {
                throw new StudyImporterException("in [" + LAVACA_BAY_LOCATIONS + ":" + parser.getLastLineNumber() + "]: invalid longtude [" + longitude + "]");
            }
            LatLng latLng = new LatLng(lat, lng);
            String locationId = createLocationId(habitateDef, regionDef, siteDef);
            List<LatLng> latLngs = locations.get(locationId);
            if (null == latLngs) {
                latLngs = new ArrayList<LatLng>();
                locations.put(locationId, latLngs);
            }
            latLngs.add(latLng);
        }
    } catch (IOException e) {
        throw new StudyImporterException("problem to reading from [" + LAVACA_BAY_LOCATIONS + "]", e);
    }
    locationMap = new HashMap<String, LatLng>();
    for (Map.Entry<String, List<LatLng>> entry : locations.entrySet()) {
        Double lat = 0.0d;
        Double lng = 0.0d;
        int count = 0;
        for (LatLng latLng : entry.getValue()) {
            lat += latLng.getLat();
            lng += latLng.getLng();
            count++;
        }
        if (count == 0) {
            throw new StudyImporterException("must have more than one location per locationId");
        }
        locationMap.put(entry.getKey(), new LatLng(lat / count, lng / count));
    }
}
Also used : HashMap(java.util.HashMap) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) IOException(java.io.IOException) ArrayList(java.util.ArrayList) List(java.util.List) LatLng(org.eol.globi.geo.LatLng) HashMap(java.util.HashMap) Map(java.util.Map)

Example 52 with LabeledCSVParser

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

the class StudyImporterForStrona 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);
    }
    try {
        Study study = nodeFactory.getOrCreateStudy(new StudyImpl("strona2013", SOURCE + " . " + CitationUtil.createLastAccessedString(RESOURCE_PATH), "http://dx.doi.org/10.1890/12-1419.1", SOURCE));
        while (dataParser.getLine() != null) {
            if (importFilter.shouldImportRecord((long) dataParser.getLastLineNumber())) {
                try {
                    String parasiteName = StringUtils.trim(dataParser.getValueByLabel("P_SP"));
                    String hostName = StringUtils.trim(dataParser.getValueByLabel("H_SP"));
                    if (areNamesAvailable(parasiteName, hostName)) {
                        Specimen parasite = nodeFactory.createSpecimen(study, new TaxonImpl(parasiteName, null));
                        Specimen host = nodeFactory.createSpecimen(study, new TaxonImpl(hostName, null));
                        parasite.interactsWith(host, InteractType.PARASITE_OF);
                    }
                } catch (NodeFactoryException | NumberFormatException e) {
                    throw new StudyImporterException("failed to import line [" + (dataParser.lastLineNumber() + 1) + "]", e);
                }
            }
        }
    } catch (IOException | NodeFactoryException e) {
        throw new StudyImporterException("problem importing [" + RESOURCE_PATH + "]", e);
    }
}
Also used : Study(org.eol.globi.domain.Study) Specimen(org.eol.globi.domain.Specimen) TaxonImpl(org.eol.globi.domain.TaxonImpl) StudyImpl(org.eol.globi.domain.StudyImpl) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) IOException(java.io.IOException)

Example 53 with LabeledCSVParser

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

the class StudyImporterForSimons method importStudy.

private Study importStudy(ParserFactory parserFactory, String studyResource) throws StudyImporterException {
    Study study = nodeFactory.getOrCreateStudy(new StudyImpl("Simons 1997", StudyImporterForGoMexSI2.GOMEXI_SOURCE_DESCRIPTION, null, ExternalIdUtil.toCitation("James D. Simons", "Food habits and trophic structure of the demersal fish assemblages on the Mississippi-Alabama continental shelf.", "1997")));
    try {
        LabeledCSVParser csvParser = parserFactory.createParser(studyResource, CharsetConstant.UTF8);
        Map<String, String> columnMapper = COLUMN_MAPPER;
        LengthParser parser = new LengthRangeParserImpl(columnMapper.get(LENGTH_RANGE_IN_MM));
        while (csvParser.getLine() != null) {
            addNextRecordToStudy(csvParser, study, COLUMN_MAPPER, parser);
        }
    } catch (IOException e) {
        throw new StudyImporterException("failed to createTaxon study [" + studyResource + "]", e);
    }
    return study;
}
Also used : Study(org.eol.globi.domain.Study) StudyImpl(org.eol.globi.domain.StudyImpl) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) IOException(java.io.IOException)

Example 54 with LabeledCSVParser

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

the class StudyImporterForWebOfLife method importNetwork.

public void importNetwork(InteractType interactType1, Location networkLocation, Study study, File file) throws IOException, NodeFactoryException {
    LabeledCSVParser interactions = CSVTSVUtil.createLabeledCSVParser(new FileInputStream(file));
    final String[] targetLabels = interactions.getLabels();
    List<String> targetTaxonNames = new ArrayList<String>();
    List<String> ignoredLabels = Arrays.asList("number of hosts sampled", "");
    for (String targetLabel : targetLabels) {
        String trimmedLabel = StringUtils.trim(targetLabel);
        if (StringUtils.isNotBlank(targetLabel) || !ignoredLabels.contains(trimmedLabel)) {
            targetTaxonNames.add(targetLabel);
        }
    }
    String[] line;
    while ((line = interactions.getLine()) != null) {
        String sourceTaxonName = line[0];
        final Specimen sourceSpecimen = nodeFactory.createSpecimen(study, new TaxonImpl(sourceTaxonName, null));
        sourceSpecimen.caughtIn(networkLocation);
        for (String targetTaxonName : targetTaxonNames) {
            final String valueByLabel = StringUtils.trim(interactions.getValueByLabel(targetTaxonName));
            if (StringUtils.isNotBlank(valueByLabel) && !StringUtils.equals("0", valueByLabel)) {
                final Specimen targetSpecimen = nodeFactory.createSpecimen(study, new TaxonImpl(targetTaxonName, null));
                targetSpecimen.caughtIn(networkLocation);
                sourceSpecimen.interactsWith(targetSpecimen, interactType1);
            }
        }
    }
}
Also used : Specimen(org.eol.globi.domain.Specimen) TaxonImpl(org.eol.globi.domain.TaxonImpl) ArrayList(java.util.ArrayList) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) FileInputStream(java.io.FileInputStream)

Example 55 with LabeledCSVParser

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

the class StudyImporterForSIAD method downloadAndImportResource.

private void downloadAndImportResource(String resource, String source) throws StudyImporterException {
    try {
        LabeledCSVParser labeledCSVParser = parserFactory.createParser(resource, "UTF-8");
        labeledCSVParser.changeDelimiter('\t');
        while (labeledCSVParser.getLine() != null) {
            String name = labeledCSVParser.getValueByLabel("name");
            String ref = labeledCSVParser.getValueByLabel("source");
            String title = "SIAD-" + ref;
            String citation = "ABRS 2009. Australian Faunal Directory. " + name + ". Australian Biological Resources StudyNode, Canberra. " + CitationUtil.createLastAccessedString(ref);
            StudyImpl study1 = new StudyImpl(title, source, null, citation);
            study1.setExternalId(ref);
            Study study = nodeFactory.getOrCreateStudy(study1);
            Specimen specimen = nodeFactory.createSpecimen(study, new TaxonImpl(name, null));
            String hostName = labeledCSVParser.getValueByLabel("host name");
            Specimen hostSpecimen = nodeFactory.createSpecimen(study, new TaxonImpl(hostName, null));
            InteractType type = map.get(labeledCSVParser.getValueByLabel("interaction"));
            specimen.interactsWith(hostSpecimen, type);
        }
    } catch (FileNotFoundException e) {
        throw new StudyImporterException("failed to open tmp file", e);
    } catch (NodeFactoryException e) {
        throw new StudyImporterException("failed to map data", e);
    } catch (IOException e) {
        throw new StudyImporterException("failed to read resource [" + resource + "]", e);
    }
}
Also used : InteractType(org.eol.globi.domain.InteractType) Study(org.eol.globi.domain.Study) Specimen(org.eol.globi.domain.Specimen) TaxonImpl(org.eol.globi.domain.TaxonImpl) StudyImpl(org.eol.globi.domain.StudyImpl) FileNotFoundException(java.io.FileNotFoundException) 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