Search in sources :

Example 66 with LabeledCSVParser

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

the class OpenTreeUtil method readTaxonomy.

public static void readTaxonomy(OpenTreeListener listener, InputStream inputStream) throws IOException {
    LabeledCSVParser parser = CSVTSVUtil.createLabeledCSVParser(new CSVParser(IOUtils.toBufferedInputStream(inputStream), '\t'));
    while (parser.getLine() != null) {
        String taxonId = parser.getValueByLabel("uid");
        String[] externalIds = StringUtils.split(parser.getValueByLabel("sourceinfo"), ",");
        for (String otherTaxonId : externalIds) {
            listener.taxonSameAs(taxonId, otherTaxonId);
        }
    }
}
Also used : LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) CSVParser(com.Ostermiller.util.CSVParser) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser)

Example 67 with LabeledCSVParser

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

the class TaxonCacheParserTest method parse.

public static void parse(BufferedReader reader, TaxonCacheListener listener) throws IOException {
    LabeledCSVParser labeledCSVParser = CSVTSVUtil.createLabeledCSVParser(reader);
    listener.start();
    while (labeledCSVParser.getLine() != null) {
        Taxon taxa = TaxonCacheParser.parseLine(labeledCSVParser);
        listener.addTaxon(taxa);
    }
    listener.finish();
}
Also used : Taxon(org.eol.globi.domain.Taxon) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser)

Example 68 with LabeledCSVParser

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

the class StudyImporterForRaymond method importData.

private void importData(InputStream inputStream) throws IOException, StudyImporterException {
    File dietFile = null;
    File sourcesFile = null;
    LabeledCSVParser sourcesParser = null;
    LabeledCSVParser dietParser = null;
    try {
        ZipInputStream zis = new ZipInputStream(inputStream);
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (DIET_CSV.equals(entry.getName())) {
                dietFile = File.createTempFile("raymondDiet", ".csv");
                dietParser = CSVTSVUtil.createParser(dietFile, zis);
            } else if (SOURCES_CSV.equals(entry.getName())) {
                sourcesFile = File.createTempFile("raymondSources", ".csv");
                sourcesParser = CSVTSVUtil.createParser(sourcesFile, zis);
            } else {
                IOUtils.copy(zis, new NullOutputStream());
            }
        }
        if (sourcesParser == null) {
            throw new StudyImporterException("failed to find [" + SOURCES_CSV + "] in [" + RESOURCE_URL + "]");
        }
        if (dietParser == null) {
            throw new StudyImporterException("failed to find [" + DIET_CSV + "] in [" + RESOURCE_URL + "]");
        }
        importData(sourcesParser, dietParser);
    } finally {
        org.apache.commons.io.FileUtils.deleteQuietly(dietFile);
        org.apache.commons.io.FileUtils.deleteQuietly(sourcesFile);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) File(java.io.File) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 69 with LabeledCSVParser

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

the class StudyImporterForKelpForest method importStudy.

@Override
public void importStudy() throws StudyImporterException {
    try {
        String source = "Beas-Luna, R., Novak, M., Carr, M. H., Tinker, M. T., Black, A., Caselle, J. E., … Iles, A. (2014). An Online Database for Informing Ecological Network Models: http://kelpforest.ucsc.edu. PLoS ONE, 9(10), e109356. doi:10.1371/journal.pone.0109356";
        Study study = nodeFactory.getOrCreateStudy(new StudyImpl(source, source, "doi:10.1371/journal.pone.0109356", source));
        LabeledCSVParser parser = parserFactory.createParser(NODES, "UTF-8");
        String[] line;
        Map<String, Long> nameToId = new HashMap<String, Long>();
        while ((line = parser.getLine()) != null) {
            if (line.length > 2) {
                String name = parser.getValueByLabel("working_name");
                String itisId = parser.getValueByLabel("itis_id");
                Long id = StringUtils.isBlank(itisId) ? null : Long.parseLong(itisId);
                id = (id != null && id > 0L) ? id : null;
                nameToId.put(name, id);
            }
        }
        Map<String, InteractType> typeToType = new HashMap<String, InteractType>() {

            {
                put("trophic", InteractType.ATE);
                put("parasitic", InteractType.PARASITE_OF);
            }
        };
        parser = parserFactory.createParser(LINKS, "UTF-8");
        while ((line = parser.getLine()) != null) {
            if (line.length > 2) {
                String interactionType = parser.getValueByLabel("type");
                InteractType interactType = typeToType.get(interactionType);
                if (null == interactType) {
                    LOG.warn("ignoring type [" + interactionType + "] on line: [" + (parser.getLastLineNumber() + 1) + "]");
                } else {
                    Specimen sourceSpecimen = createSpecimen(parser, nameToId, "node_1_working_name", "node1_stage", study);
                    Specimen targetSpecimen = createSpecimen(parser, nameToId, "node_2_working_name", "node2_stage", study);
                    sourceSpecimen.interactsWith(targetSpecimen, interactType);
                }
            }
        }
    } catch (IOException | NodeFactoryException e) {
        throw new StudyImporterException("failed to import", e);
    }
}
Also used : InteractType(org.eol.globi.domain.InteractType) Study(org.eol.globi.domain.Study) HashMap(java.util.HashMap) StudyImpl(org.eol.globi.domain.StudyImpl) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) IOException(java.io.IOException) Specimen(org.eol.globi.domain.Specimen)

Example 70 with LabeledCSVParser

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

the class StudyImporterForJRFerrerParis method importStudy.

@Override
public void importStudy() throws StudyImporterException {
    String citation = "Ferrer-Paris, José R.; Sánchez-Mercado, Ada Y.; Lozano, Cecilia; Zambrano, Liset; Soto, José; Baettig, Jessica; Leal, María (2014): A compilation of larval host-plant records for six families of butterflies (Lepidoptera: Papilionoidea) from available electronic resources. figshare. http://dx.doi.org/10.6084/m9.figshare.1168861 . " + CitationUtil.createLastAccessedString(RESOURCE);
    Study study = nodeFactory.getOrCreateStudy(new StudyImpl("Ferrer-Paris 2014", citation, "http://dx.doi.org/10.6084/m9.figshare.1168861", citation));
    try {
        LabeledCSVParser parser = parserFactory.createParser(RESOURCE, CharsetConstant.UTF8);
        while (parser.getLine() != null) {
            String butterflyName = createTaxon(parser, "Lepidoptera Name");
            String plantName = createTaxon(parser, "Hostplant Name");
            if (validNames(butterflyName, plantName, study, parser.lastLineNumber())) {
                addAssociation(study, parser, butterflyName, plantName);
            }
        }
    } catch (IOException e) {
        throw new StudyImporterException("failed to access resource [" + RESOURCE + "]");
    }
}
Also used : Study(org.eol.globi.domain.Study) StudyImpl(org.eol.globi.domain.StudyImpl) 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