@Test public void parseMatrix() throws IOException, StudyImporterException { List<Map<String, String>> interactions = new TreeList<>(); InteractionListener listener = interactions::add; DatasetImporterForGlobalWebDb.parseDietMatrix(listener, aDietMatrix, "some source citation"); assertThat(interactions.size(), is(6)); Map<String, String> first = interactions.get(0); assertThat(first.get(TaxonUtil.SOURCE_TAXON_NAME), is("Balanus balanoides")); assertThat(first.get(TaxonUtil.TARGET_TAXON_NAME), is("detritus")); assertThat(first.get(DatasetImporterForTSV.INTERACTION_TYPE_NAME), is("eats")); assertThat(first.get(DatasetImporterForTSV.INTERACTION_TYPE_ID), is("http: assertThat(first.get(DatasetImporterForTSV.HABITAT_NAME), is("Exposed rocky shore")); assertThat(first.get(DatasetImporterForTSV.LOCALITY_NAME), is("New England, U.S.A.")); assertThat(first.get(DatasetImporterForTSV.REFERENCE_ID), is("df06df18abafa63a6f0473d6d0e6ce68")); assertThat(first.get(DatasetImporterForTSV.REFERENCE_CITATION), is("B.A. Menge and J.P. Sutherland. Species Diversity Gradients: Synthesis of the Roles of Predation, Competition, and Temporal Heterogeneity. The American Naturalist, 110(973), pp. 351-369.")); assertThat(first.get(DatasetImporterForTSV.STUDY_SOURCE_CITATION), is("some source citation")); Map<String, String> last = interactions.get(5); assertThat(last.get(TaxonUtil.SOURCE_TAXON_NAME), is("Thais lapillus")); assertThat(last.get(TaxonUtil.TARGET_TAXON_NAME), is("Mytilus edulis")); assertThat(last.get(DatasetImporterForTSV.INTERACTION_TYPE_NAME), is("eats")); assertThat(last.get(DatasetImporterForTSV.INTERACTION_TYPE_ID), is("http: assertThat(last.get(DatasetImporterForTSV.HABITAT_NAME), is("Exposed rocky shore")); assertThat(last.get(DatasetImporterForTSV.LOCALITY_NAME), is("New England, U.S.A.")); assertThat(last.get(DatasetImporterForTSV.REFERENCE_ID), is("df06df18abafa63a6f0473d6d0e6ce68")); assertThat(last.get(DatasetImporterForTSV.REFERENCE_CITATION), is("B.A. Menge and J.P. Sutherland. Species Diversity Gradients: Synthesis of the Roles of Predation, Competition, and Temporal Heterogeneity. The American Naturalist, 110(973), pp. 351-369.")); assertThat(last.get(DatasetImporterForTSV.STUDY_SOURCE_CITATION), is("some source citation")); }
static void parseDietMatrix(InteractionListener listener, String dietMatrixWithCitation, String sourceCitation) throws IOException, StudyImporterException { String[] rows = dietMatrixWithCitation.split("\r\n"); if (rows.length > 0) { String citation = rows[0].replaceAll("^\"", "") .replaceAll("\",*$", ""); List<String> matrixRows = Arrays.asList(rows).subList(1, rows.length); String matrix = org.apache.commons.lang.StringUtils.join(matrixRows, "\n"); LabeledCSVParser parser = CSVTSVUtil.createLabeledCSVParser(IOUtils.toInputStream(matrix, StandardCharsets.UTF_8)); String[] headerColumns = parser.getLabels(); if (headerColumns.length > 1) { String[] split1 = headerColumns[0].split("-"); String habitat = split1[0]; List<String> localityList = Arrays.asList(split1).subList(1, split1.length); String locality = localityList.stream().map(String::trim).collect(Collectors.joining(", ")); Map<String, String> props = new TreeMap<String, String>() {{ put(DatasetImporterForTSV.HABITAT_NAME, org.apache.commons.lang.StringUtils.trim(habitat)); put(DatasetImporterForTSV.LOCALITY_NAME, org.apache.commons.lang.StringUtils.trim(locality)); put(DatasetImporterForTSV.INTERACTION_TYPE_NAME, InteractType.ATE.getLabel()); put(DatasetImporterForTSV.INTERACTION_TYPE_ID, InteractType.ATE.getIRI()); put(DatasetImporterForTSV.REFERENCE_ID, MD5.getHashString(citation)); put(DatasetImporterForTSV.REFERENCE_CITATION, citation); put(DatasetImporterForTSV.STUDY_SOURCE_CITATION, sourceCitation); }}; List<String> sourceTaxa = Arrays.asList(headerColumns).subList(1, headerColumns.length); while (parser.getLine() != null) { for (String sourceTaxon : sourceTaxa) { String value = parser.getValueByLabel(sourceTaxon); String targetTaxon = parser.getValueByLabel(headerColumns[0]); if (NumberUtils.isDigits(value) && Integer.parseInt(value) > 0) { listener.newLink(new TreeMap<String, String>(props) {{ put(TaxonUtil.SOURCE_TAXON_NAME, org.apache.commons.lang.StringUtils.trim(sourceTaxon)); put(TaxonUtil.TARGET_TAXON_NAME, org.apache.commons.lang.StringUtils.trim(targetTaxon)); }}); } } } } } }
DatasetImporterForGlobalWebDb extends DatasetImporterWithListener { static void parseDietMatrix(InteractionListener listener, String dietMatrixWithCitation, String sourceCitation) throws IOException, StudyImporterException { String[] rows = dietMatrixWithCitation.split("\r\n"); if (rows.length > 0) { String citation = rows[0].replaceAll("^\"", "") .replaceAll("\",*$", ""); List<String> matrixRows = Arrays.asList(rows).subList(1, rows.length); String matrix = org.apache.commons.lang.StringUtils.join(matrixRows, "\n"); LabeledCSVParser parser = CSVTSVUtil.createLabeledCSVParser(IOUtils.toInputStream(matrix, StandardCharsets.UTF_8)); String[] headerColumns = parser.getLabels(); if (headerColumns.length > 1) { String[] split1 = headerColumns[0].split("-"); String habitat = split1[0]; List<String> localityList = Arrays.asList(split1).subList(1, split1.length); String locality = localityList.stream().map(String::trim).collect(Collectors.joining(", ")); Map<String, String> props = new TreeMap<String, String>() {{ put(DatasetImporterForTSV.HABITAT_NAME, org.apache.commons.lang.StringUtils.trim(habitat)); put(DatasetImporterForTSV.LOCALITY_NAME, org.apache.commons.lang.StringUtils.trim(locality)); put(DatasetImporterForTSV.INTERACTION_TYPE_NAME, InteractType.ATE.getLabel()); put(DatasetImporterForTSV.INTERACTION_TYPE_ID, InteractType.ATE.getIRI()); put(DatasetImporterForTSV.REFERENCE_ID, MD5.getHashString(citation)); put(DatasetImporterForTSV.REFERENCE_CITATION, citation); put(DatasetImporterForTSV.STUDY_SOURCE_CITATION, sourceCitation); }}; List<String> sourceTaxa = Arrays.asList(headerColumns).subList(1, headerColumns.length); while (parser.getLine() != null) { for (String sourceTaxon : sourceTaxa) { String value = parser.getValueByLabel(sourceTaxon); String targetTaxon = parser.getValueByLabel(headerColumns[0]); if (NumberUtils.isDigits(value) && Integer.parseInt(value) > 0) { listener.newLink(new TreeMap<String, String>(props) {{ put(TaxonUtil.SOURCE_TAXON_NAME, org.apache.commons.lang.StringUtils.trim(sourceTaxon)); put(TaxonUtil.TARGET_TAXON_NAME, org.apache.commons.lang.StringUtils.trim(targetTaxon)); }}); } } } } } } }
DatasetImporterForGlobalWebDb extends DatasetImporterWithListener { static void parseDietMatrix(InteractionListener listener, String dietMatrixWithCitation, String sourceCitation) throws IOException, StudyImporterException { String[] rows = dietMatrixWithCitation.split("\r\n"); if (rows.length > 0) { String citation = rows[0].replaceAll("^\"", "") .replaceAll("\",*$", ""); List<String> matrixRows = Arrays.asList(rows).subList(1, rows.length); String matrix = org.apache.commons.lang.StringUtils.join(matrixRows, "\n"); LabeledCSVParser parser = CSVTSVUtil.createLabeledCSVParser(IOUtils.toInputStream(matrix, StandardCharsets.UTF_8)); String[] headerColumns = parser.getLabels(); if (headerColumns.length > 1) { String[] split1 = headerColumns[0].split("-"); String habitat = split1[0]; List<String> localityList = Arrays.asList(split1).subList(1, split1.length); String locality = localityList.stream().map(String::trim).collect(Collectors.joining(", ")); Map<String, String> props = new TreeMap<String, String>() {{ put(DatasetImporterForTSV.HABITAT_NAME, org.apache.commons.lang.StringUtils.trim(habitat)); put(DatasetImporterForTSV.LOCALITY_NAME, org.apache.commons.lang.StringUtils.trim(locality)); put(DatasetImporterForTSV.INTERACTION_TYPE_NAME, InteractType.ATE.getLabel()); put(DatasetImporterForTSV.INTERACTION_TYPE_ID, InteractType.ATE.getIRI()); put(DatasetImporterForTSV.REFERENCE_ID, MD5.getHashString(citation)); put(DatasetImporterForTSV.REFERENCE_CITATION, citation); put(DatasetImporterForTSV.STUDY_SOURCE_CITATION, sourceCitation); }}; List<String> sourceTaxa = Arrays.asList(headerColumns).subList(1, headerColumns.length); while (parser.getLine() != null) { for (String sourceTaxon : sourceTaxa) { String value = parser.getValueByLabel(sourceTaxon); String targetTaxon = parser.getValueByLabel(headerColumns[0]); if (NumberUtils.isDigits(value) && Integer.parseInt(value) > 0) { listener.newLink(new TreeMap<String, String>(props) {{ put(TaxonUtil.SOURCE_TAXON_NAME, org.apache.commons.lang.StringUtils.trim(sourceTaxon)); put(TaxonUtil.TARGET_TAXON_NAME, org.apache.commons.lang.StringUtils.trim(targetTaxon)); }}); } } } } } } DatasetImporterForGlobalWebDb(ParserFactory parserFactory, NodeFactory nodeFactory); }
DatasetImporterForGlobalWebDb extends DatasetImporterWithListener { static void parseDietMatrix(InteractionListener listener, String dietMatrixWithCitation, String sourceCitation) throws IOException, StudyImporterException { String[] rows = dietMatrixWithCitation.split("\r\n"); if (rows.length > 0) { String citation = rows[0].replaceAll("^\"", "") .replaceAll("\",*$", ""); List<String> matrixRows = Arrays.asList(rows).subList(1, rows.length); String matrix = org.apache.commons.lang.StringUtils.join(matrixRows, "\n"); LabeledCSVParser parser = CSVTSVUtil.createLabeledCSVParser(IOUtils.toInputStream(matrix, StandardCharsets.UTF_8)); String[] headerColumns = parser.getLabels(); if (headerColumns.length > 1) { String[] split1 = headerColumns[0].split("-"); String habitat = split1[0]; List<String> localityList = Arrays.asList(split1).subList(1, split1.length); String locality = localityList.stream().map(String::trim).collect(Collectors.joining(", ")); Map<String, String> props = new TreeMap<String, String>() {{ put(DatasetImporterForTSV.HABITAT_NAME, org.apache.commons.lang.StringUtils.trim(habitat)); put(DatasetImporterForTSV.LOCALITY_NAME, org.apache.commons.lang.StringUtils.trim(locality)); put(DatasetImporterForTSV.INTERACTION_TYPE_NAME, InteractType.ATE.getLabel()); put(DatasetImporterForTSV.INTERACTION_TYPE_ID, InteractType.ATE.getIRI()); put(DatasetImporterForTSV.REFERENCE_ID, MD5.getHashString(citation)); put(DatasetImporterForTSV.REFERENCE_CITATION, citation); put(DatasetImporterForTSV.STUDY_SOURCE_CITATION, sourceCitation); }}; List<String> sourceTaxa = Arrays.asList(headerColumns).subList(1, headerColumns.length); while (parser.getLine() != null) { for (String sourceTaxon : sourceTaxa) { String value = parser.getValueByLabel(sourceTaxon); String targetTaxon = parser.getValueByLabel(headerColumns[0]); if (NumberUtils.isDigits(value) && Integer.parseInt(value) > 0) { listener.newLink(new TreeMap<String, String>(props) {{ put(TaxonUtil.SOURCE_TAXON_NAME, org.apache.commons.lang.StringUtils.trim(sourceTaxon)); put(TaxonUtil.TARGET_TAXON_NAME, org.apache.commons.lang.StringUtils.trim(targetTaxon)); }}); } } } } } } DatasetImporterForGlobalWebDb(ParserFactory parserFactory, NodeFactory nodeFactory); @Override void importStudy(); }
DatasetImporterForGlobalWebDb extends DatasetImporterWithListener { static void parseDietMatrix(InteractionListener listener, String dietMatrixWithCitation, String sourceCitation) throws IOException, StudyImporterException { String[] rows = dietMatrixWithCitation.split("\r\n"); if (rows.length > 0) { String citation = rows[0].replaceAll("^\"", "") .replaceAll("\",*$", ""); List<String> matrixRows = Arrays.asList(rows).subList(1, rows.length); String matrix = org.apache.commons.lang.StringUtils.join(matrixRows, "\n"); LabeledCSVParser parser = CSVTSVUtil.createLabeledCSVParser(IOUtils.toInputStream(matrix, StandardCharsets.UTF_8)); String[] headerColumns = parser.getLabels(); if (headerColumns.length > 1) { String[] split1 = headerColumns[0].split("-"); String habitat = split1[0]; List<String> localityList = Arrays.asList(split1).subList(1, split1.length); String locality = localityList.stream().map(String::trim).collect(Collectors.joining(", ")); Map<String, String> props = new TreeMap<String, String>() {{ put(DatasetImporterForTSV.HABITAT_NAME, org.apache.commons.lang.StringUtils.trim(habitat)); put(DatasetImporterForTSV.LOCALITY_NAME, org.apache.commons.lang.StringUtils.trim(locality)); put(DatasetImporterForTSV.INTERACTION_TYPE_NAME, InteractType.ATE.getLabel()); put(DatasetImporterForTSV.INTERACTION_TYPE_ID, InteractType.ATE.getIRI()); put(DatasetImporterForTSV.REFERENCE_ID, MD5.getHashString(citation)); put(DatasetImporterForTSV.REFERENCE_CITATION, citation); put(DatasetImporterForTSV.STUDY_SOURCE_CITATION, sourceCitation); }}; List<String> sourceTaxa = Arrays.asList(headerColumns).subList(1, headerColumns.length); while (parser.getLine() != null) { for (String sourceTaxon : sourceTaxa) { String value = parser.getValueByLabel(sourceTaxon); String targetTaxon = parser.getValueByLabel(headerColumns[0]); if (NumberUtils.isDigits(value) && Integer.parseInt(value) > 0) { listener.newLink(new TreeMap<String, String>(props) {{ put(TaxonUtil.SOURCE_TAXON_NAME, org.apache.commons.lang.StringUtils.trim(sourceTaxon)); put(TaxonUtil.TARGET_TAXON_NAME, org.apache.commons.lang.StringUtils.trim(targetTaxon)); }}); } } } } } } DatasetImporterForGlobalWebDb(ParserFactory parserFactory, NodeFactory nodeFactory); @Override void importStudy();  }