From 7f0ed6e07fd7f2938bafd5e06726f0867dbf1314 Mon Sep 17 00:00:00 2001 From: Jim Balhoff Date: Thu, 5 Oct 2017 16:55:21 -0400 Subject: [PATCH 1/3] Added table-format option to choose CSV or TSV. --- .../org/monarchinitiative/dosdp/cli/Common.scala | 10 ++++++++++ .../monarchinitiative/dosdp/cli/Generate.scala | 4 ++-- .../org/monarchinitiative/dosdp/cli/Query.scala | 16 +++++++--------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main/scala/org/monarchinitiative/dosdp/cli/Common.scala b/src/main/scala/org/monarchinitiative/dosdp/cli/Common.scala index f55856d..81a2147 100644 --- a/src/main/scala/org/monarchinitiative/dosdp/cli/Common.scala +++ b/src/main/scala/org/monarchinitiative/dosdp/cli/Common.scala @@ -17,6 +17,9 @@ import io.circe.syntax._ import io.circe.yaml.parser import org.monarchinitiative.dosdp._ import com.typesafe.scalalogging.LazyLogging +import com.github.tototoshi.csv.CSVFormat +import com.github.tototoshi.csv.DefaultCSVFormat +import com.github.tototoshi.csv.TSVFormat trait Common extends Command with LazyLogging { @@ -27,6 +30,7 @@ trait Common extends Command with LazyLogging { var prefixesFileOpt = opt[Option[File]](name = "prefixes", default = None, description = "CURIE prefixes (YAML)") var oboPrefixes = opt[Boolean](name = "obo-prefixes", default = false, description = "Assume prefixes are OBO ontologies; predefine rdf, rdfs, and owl") var outfile = opt[File](name = "outfile", default = new File("dosdp.out"), description = "Output file (OWL or TSV)") + var tableFormat = opt[String](name = "table-format", default = "tsv", description = "Tabular format: TSV or CSV") def ontologyOpt: Option[OWLOntology] = ontOpt.map { ontPath => val ontIRI = if (ontPath.startsWith("http")) IRI.create(ontPath) else IRI.create(new File(ontPath)) @@ -50,4 +54,10 @@ trait Common extends Command with LazyLogging { if (oboPrefixes) specifiedPrefixes.orElse(OBOPrefixes) else specifiedPrefixes } + def tabularFormat: CSVFormat = tableFormat.toLowerCase match { + case "csv" => new DefaultCSVFormat {} + case "tsv" => new TSVFormat {} + case other => throw new RuntimeException(s"Invalid tabular format requested: $other") + } + } \ No newline at end of file diff --git a/src/main/scala/org/monarchinitiative/dosdp/cli/Generate.scala b/src/main/scala/org/monarchinitiative/dosdp/cli/Generate.scala index 8c11b74..b67d4ba 100644 --- a/src/main/scala/org/monarchinitiative/dosdp/cli/Generate.scala +++ b/src/main/scala/org/monarchinitiative/dosdp/cli/Generate.scala @@ -17,7 +17,6 @@ import org.semanticweb.owlapi.model.OWLOntology import org.semanticweb.owlapi.model.parameters.Imports import com.github.tototoshi.csv.CSVReader -import com.github.tototoshi.csv.TSVFormat import cats.implicits._ @@ -28,11 +27,12 @@ object Generate extends Command(description = "generate ontology axioms for TSV val LocalLabelProperty = IRI.create("http://example.org/TSVProvidedLabel") def run: Unit = { + val sepFormat = tabularFormat val dosdp = inputDOSDP val eDOSDP = ExpandedDOSDP(dosdp, prefixes) val readableIDIndex = ontologyOpt.map(ont => createReadableIdentifierIndex(eDOSDP, ont)).getOrElse(Map.empty) val axioms = (for { - row <- CSVReader.open(infile, "utf-8")(new TSVFormat {}).iteratorWithHeaders + row <- CSVReader.open(infile, "utf-8")(sepFormat).iteratorWithHeaders } yield { val (varBindingsItems, localLabelItems) = (for { vars <- dosdp.vars.toSeq diff --git a/src/main/scala/org/monarchinitiative/dosdp/cli/Query.scala b/src/main/scala/org/monarchinitiative/dosdp/cli/Query.scala index 32ebef7..46ce1b9 100644 --- a/src/main/scala/org/monarchinitiative/dosdp/cli/Query.scala +++ b/src/main/scala/org/monarchinitiative/dosdp/cli/Query.scala @@ -1,10 +1,5 @@ package org.monarchinitiative.dosdp.cli -import java.io.FileOutputStream -import java.io.OutputStreamWriter -import java.io.PrintWriter -import java.nio.charset.StandardCharsets - import scala.collection.JavaConverters._ import org.apache.jena.query.QueryExecutionFactory @@ -18,6 +13,8 @@ import org.semanticweb.elk.owlapi.ElkReasonerFactory import org.semanticweb.owlapi.apibinding.OWLManager import org.semanticweb.owlapi.model.OWLOntology +import com.github.tototoshi.csv.CSVWriter + import uk.ac.manchester.cs.jfact.JFactFactory object Query extends Command(description = "query an ontology for terms matching a Dead Simple OWL Design Pattern") with Common { @@ -26,6 +23,7 @@ object Query extends Command(description = "query an ontology for terms matching var printQuery = opt[Boolean](name = "print-query", default = false, description = "Print generated query without running against ontology") def run: Unit = { + val sepFormat = tabularFormat val sparqlQuery = SPARQL.queryFor(ExpandedDOSDP(inputDOSDP, prefixes)) val reasonerFactoryOpt = reasonerNameOpt.map(_.toLowerCase).map { name => name match { @@ -59,13 +57,13 @@ object Query extends Command(description = "query an ontology for terms matching val query = QueryFactory.create(processedQuery) val results = QueryExecutionFactory.create(query, model).execSelect() val columns = results.getResultVars.asScala.toList - val pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outfile), StandardCharsets.UTF_8)) - pw.println(columns.mkString("\t")) + val writer = CSVWriter.open(outfile, "utf-8")(sepFormat) + writer.writeRow(columns) while (results.hasNext()) { val qs = results.next() - pw.println(columns.map(variable => Option(qs.get(variable)).map(_.toString).getOrElse("")).mkString("\t")) + writer.writeRow(columns.map(variable => Option(qs.get(variable)).map(_.toString).getOrElse(""))) } - pw.close() + writer.close() } } From 365a315b1aa0c5ad3f17bb1bbffe1427081bba29 Mon Sep 17 00:00:00 2001 From: Jim Balhoff Date: Fri, 6 Oct 2017 09:50:49 -0400 Subject: [PATCH 2/3] Updated Scala version; print exception messages. --- build.sbt | 4 ++-- .../scala/org/monarchinitiative/dosdp/cli/Common.scala | 2 +- src/main/scala/org/monarchinitiative/dosdp/cli/Main.scala | 8 +++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/build.sbt b/build.sbt index 82d787e..fc3a659 100644 --- a/build.sbt +++ b/build.sbt @@ -6,9 +6,9 @@ organization := "org.monarchinitiative" name := "dosdp-tools" -version := "0.5" +version := "0.6-SNAPSHOT" -scalaVersion := "2.11.8" +scalaVersion := "2.12.3" scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8") diff --git a/src/main/scala/org/monarchinitiative/dosdp/cli/Common.scala b/src/main/scala/org/monarchinitiative/dosdp/cli/Common.scala index 81a2147..9590342 100644 --- a/src/main/scala/org/monarchinitiative/dosdp/cli/Common.scala +++ b/src/main/scala/org/monarchinitiative/dosdp/cli/Common.scala @@ -30,7 +30,7 @@ trait Common extends Command with LazyLogging { var prefixesFileOpt = opt[Option[File]](name = "prefixes", default = None, description = "CURIE prefixes (YAML)") var oboPrefixes = opt[Boolean](name = "obo-prefixes", default = false, description = "Assume prefixes are OBO ontologies; predefine rdf, rdfs, and owl") var outfile = opt[File](name = "outfile", default = new File("dosdp.out"), description = "Output file (OWL or TSV)") - var tableFormat = opt[String](name = "table-format", default = "tsv", description = "Tabular format: TSV or CSV") + var tableFormat = opt[String](name = "table-format", default = "tsv", description = "Tabular format: TSV (default) or CSV") def ontologyOpt: Option[OWLOntology] = ontOpt.map { ontPath => val ontIRI = if (ontPath.startsWith("http")) IRI.create(ontPath) else IRI.create(new File(ontPath)) diff --git a/src/main/scala/org/monarchinitiative/dosdp/cli/Main.scala b/src/main/scala/org/monarchinitiative/dosdp/cli/Main.scala index 79cb8f2..6d9ebd2 100644 --- a/src/main/scala/org/monarchinitiative/dosdp/cli/Main.scala +++ b/src/main/scala/org/monarchinitiative/dosdp/cli/Main.scala @@ -29,6 +29,12 @@ import org.apache.jena.system.JenaSystem object Main extends App { JenaSystem.init() - Cli.parse(args).withProgramName("dosdp-tools").withCommands(Generate, Query).foreach(_.run) + try { + Cli.parse(args).withProgramName("dosdp-tools").withCommands(Generate, Query).foreach(_.run) + } catch { + case e: Exception => + println(e.getMessage) + throw e + } } \ No newline at end of file From b1553203b0875654cc33dee2cbfa9bdb8cb3710b Mon Sep 17 00:00:00 2001 From: Jim Balhoff Date: Fri, 6 Oct 2017 09:52:12 -0400 Subject: [PATCH 3/3] Updated travis config. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5bde039..1b30a71 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: scala scala: - - "2.11.8" + - "2.12.3" jdk: - oraclejdk8