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));
}
}
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);
}
}
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;
}
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);
}
}
}
}
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);
}
}
Aggregations