89 lines
3.1 KiB
Scala
89 lines
3.1 KiB
Scala
package eu.dnetlib.dhp.sx.bio
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper
|
|
import eu.dnetlib.dhp.application.AbstractScalaApplication
|
|
import eu.dnetlib.dhp.collection.CollectionUtils
|
|
import eu.dnetlib.dhp.common.Constants.MDSTORE_DATA_PATH
|
|
import eu.dnetlib.dhp.schema.mdstore.MDStoreVersion
|
|
import eu.dnetlib.dhp.schema.oaf.Oaf
|
|
import eu.dnetlib.dhp.sx.bio.BioDBToOAF.ScholixResolved
|
|
import org.apache.spark.sql.{Encoder, Encoders, SparkSession}
|
|
import org.slf4j.{Logger, LoggerFactory}
|
|
|
|
class SparkTransformBioDatabaseToOAF(propertyPath: String, args: Array[String], log: Logger)
|
|
extends AbstractScalaApplication(propertyPath, args, log: Logger) {
|
|
|
|
private def convertDatabase(
|
|
spark: SparkSession,
|
|
dbPath: String,
|
|
database: String,
|
|
targetPath: String,
|
|
outputBasePath: String
|
|
): Unit = {
|
|
val sc = spark.sparkContext
|
|
implicit val resultEncoder: Encoder[Oaf] = Encoders.kryo(classOf[Oaf])
|
|
import spark.implicits._
|
|
database.toUpperCase() match {
|
|
case "UNIPROT" =>
|
|
CollectionUtils.saveDataset(
|
|
spark.createDataset(sc.textFile(dbPath).flatMap(i => BioDBToOAF.uniprotToOAF(i))),
|
|
targetPath
|
|
)
|
|
reportTotalSize(targetPath, outputBasePath)
|
|
case "PDB" =>
|
|
CollectionUtils.saveDataset(
|
|
spark.createDataset(sc.textFile(dbPath).flatMap(i => BioDBToOAF.pdbTOOaf(i))),
|
|
targetPath
|
|
)
|
|
reportTotalSize(targetPath, outputBasePath)
|
|
case "SCHOLIX" =>
|
|
CollectionUtils.saveDataset(
|
|
spark.read.load(dbPath).as[ScholixResolved].map(i => BioDBToOAF.scholixResolvedToOAF(i)),
|
|
targetPath
|
|
)
|
|
reportTotalSize(targetPath, outputBasePath)
|
|
case "CROSSREF_LINKS" =>
|
|
CollectionUtils.saveDataset(
|
|
spark.read.load(dbPath).select("json").as[String].map(i => BioDBToOAF.crossrefLinksToOaf(i)),
|
|
targetPath
|
|
)
|
|
reportTotalSize(targetPath, outputBasePath)
|
|
}
|
|
}
|
|
|
|
/** Here all the spark applications runs this method
|
|
* where the whole logic of the spark node is defined
|
|
*/
|
|
override def run(): Unit = {
|
|
val database: String = parser.get("database")
|
|
log.info("database: {}", database)
|
|
|
|
val dbPath: String = parser.get("dbPath")
|
|
log.info("dbPath: {}", database)
|
|
|
|
val mdstoreOutputVersion = parser.get("mdstoreOutputVersion")
|
|
log.info(s"mdstoreOutputVersion is '$mdstoreOutputVersion'")
|
|
|
|
val mapper = new ObjectMapper()
|
|
val cleanedMdStoreVersion = mapper.readValue(mdstoreOutputVersion, classOf[MDStoreVersion])
|
|
val outputBasePath = cleanedMdStoreVersion.getHdfsPath
|
|
log.info(s"outputBasePath is '$outputBasePath'")
|
|
|
|
val targetPath = s"$outputBasePath$MDSTORE_DATA_PATH"
|
|
log.info(s"targetPath is '$targetPath'")
|
|
convertDatabase(spark, dbPath, database, targetPath, outputBasePath)
|
|
|
|
}
|
|
}
|
|
|
|
object SparkTransformBioDatabaseToOAF {
|
|
val log: Logger = LoggerFactory.getLogger(SparkTransformBioDatabaseToOAF.getClass)
|
|
|
|
def main(args: Array[String]): Unit = {
|
|
new SparkTransformBioDatabaseToOAF("/eu/dnetlib/dhp/sx/bio/ebi/bio_to_oaf_params.json", args, log)
|
|
.initialize()
|
|
.run()
|
|
}
|
|
|
|
}
|