Skip to content

Commit

Permalink
Merge pull request #31 from INCATools/issue-28
Browse files Browse the repository at this point in the history
Add handling of CSV.
  • Loading branch information
balhoff authored Oct 6, 2017
2 parents 0194899 + b155320 commit 8f52b68
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: scala
scala:
- "2.11.8"
- "2.12.3"
jdk:
- oraclejdk8
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
10 changes: 10 additions & 0 deletions src/main/scala/org/monarchinitiative/dosdp/cli/Common.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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 (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))
Expand All @@ -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")
}

}
4 changes: 2 additions & 2 deletions src/main/scala/org/monarchinitiative/dosdp/cli/Generate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/main/scala/org/monarchinitiative/dosdp/cli/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
16 changes: 7 additions & 9 deletions src/main/scala/org/monarchinitiative/dosdp/cli/Query.scala
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
}
}

Expand Down

0 comments on commit 8f52b68

Please sign in to comment.