2023-07-05 23:51:01 +02:00
|
|
|
|
|
|
|
package eu.dnetlib.dhp.actionmanager.bipaffiliations;
|
|
|
|
|
|
|
|
import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
2023-07-06 17:29:15 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2023-07-05 23:51:01 +02:00
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
2023-07-06 14:56:12 +02:00
|
|
|
import org.apache.hadoop.io.Text;
|
2023-07-28 18:03:47 +02:00
|
|
|
import org.apache.hadoop.io.compress.GzipCodec;
|
2023-07-06 14:56:12 +02:00
|
|
|
import org.apache.hadoop.mapred.SequenceFileOutputFormat;
|
2023-07-05 23:51:01 +02:00
|
|
|
import org.apache.spark.SparkConf;
|
2023-10-19 18:58:25 +02:00
|
|
|
import org.apache.spark.api.java.JavaPairRDD;
|
2023-07-06 17:29:15 +02:00
|
|
|
import org.apache.spark.api.java.function.FlatMapFunction;
|
2023-07-28 18:03:47 +02:00
|
|
|
import org.apache.spark.sql.*;
|
2023-07-17 15:04:21 +02:00
|
|
|
import org.apache.spark.sql.Dataset;
|
2023-07-05 23:51:01 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
2023-07-17 15:04:21 +02:00
|
|
|
import eu.dnetlib.dhp.actionmanager.Constants;
|
|
|
|
import eu.dnetlib.dhp.actionmanager.ror.GenerateRorActionSetJob;
|
2023-07-05 23:51:01 +02:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2023-07-17 15:04:21 +02:00
|
|
|
import eu.dnetlib.dhp.schema.action.AtomicAction;
|
2023-07-05 23:51:01 +02:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelConstants;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.*;
|
2023-07-17 15:04:21 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.utils.CleaningFunctions;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.utils.IdentifierFactory;
|
2023-07-05 23:51:01 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.utils.OafMapperUtils;
|
2023-07-06 14:56:12 +02:00
|
|
|
import scala.Tuple2;
|
2023-07-05 23:51:01 +02:00
|
|
|
|
|
|
|
/**
|
2023-07-06 17:08:33 +02:00
|
|
|
* Creates action sets for Crossref affiliation relations inferred by BIP!
|
2023-07-05 23:51:01 +02:00
|
|
|
*/
|
|
|
|
public class PrepareAffiliationRelations implements Serializable {
|
|
|
|
|
2023-07-17 15:04:21 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(PrepareAffiliationRelations.class);
|
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
|
|
private static final String ID_PREFIX = "50|doi_________::";
|
|
|
|
public static final String BIP_AFFILIATIONS_CLASSID = "result:organization:bipinference";
|
|
|
|
public static final String BIP_AFFILIATIONS_CLASSNAME = "Affiliation relation inferred by BIP!";
|
|
|
|
public static final String BIP_INFERENCE_PROVENANCE = "bip:affiliation:crossref";
|
|
|
|
|
|
|
|
public static <I extends Result> void main(String[] args) throws Exception {
|
|
|
|
|
|
|
|
String jsonConfiguration = IOUtils
|
|
|
|
.toString(
|
|
|
|
PrepareAffiliationRelations.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/actionmanager/bipaffiliations/input_actionset_parameter.json"));
|
|
|
|
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
Boolean isSparkSessionManaged = Constants.isSparkSessionManaged(parser);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
2023-10-25 21:05:02 +02:00
|
|
|
final String crossrefInputPath = parser.get("crossrefInputPath");
|
|
|
|
log.info("crossrefInputPath: {}", crossrefInputPath);
|
2023-10-19 18:58:25 +02:00
|
|
|
|
|
|
|
final String pubmedInputPath = parser.get("pubmedInputPath");
|
|
|
|
log.info("pubmedInputPath: {}", pubmedInputPath);
|
2023-07-17 15:04:21 +02:00
|
|
|
|
|
|
|
final String outputPath = parser.get("outputPath");
|
2023-10-19 18:58:25 +02:00
|
|
|
log.info("outputPath: {}", outputPath);
|
2023-07-17 15:04:21 +02:00
|
|
|
|
|
|
|
SparkConf conf = new SparkConf();
|
|
|
|
|
|
|
|
runWithSparkSession(
|
|
|
|
conf,
|
|
|
|
isSparkSessionManaged,
|
|
|
|
spark -> {
|
|
|
|
Constants.removeOutputDir(spark, outputPath);
|
2023-10-19 18:58:25 +02:00
|
|
|
|
2023-10-20 11:48:21 +02:00
|
|
|
List<KeyValue> collectedFromCrossref = OafMapperUtils
|
|
|
|
.listKeyValues(ModelConstants.CROSSREF_ID, "Crossref");
|
|
|
|
JavaPairRDD<Text, Text> crossrefRelations = prepareAffiliationRelations(
|
2023-10-25 21:05:02 +02:00
|
|
|
spark, crossrefInputPath, collectedFromCrossref);
|
2023-10-19 18:58:25 +02:00
|
|
|
|
2023-10-20 11:48:21 +02:00
|
|
|
List<KeyValue> collectedFromPubmed = OafMapperUtils
|
|
|
|
.listKeyValues(ModelConstants.PUBMED_CENTRAL_ID, "Pubmed");
|
|
|
|
JavaPairRDD<Text, Text> pubmedRelations = prepareAffiliationRelations(
|
2023-10-25 21:05:02 +02:00
|
|
|
spark, pubmedInputPath, collectedFromPubmed);
|
2023-10-19 18:58:25 +02:00
|
|
|
|
|
|
|
crossrefRelations
|
2023-10-20 11:48:21 +02:00
|
|
|
.union(pubmedRelations)
|
|
|
|
.saveAsHadoopFile(
|
|
|
|
outputPath, Text.class, Text.class, SequenceFileOutputFormat.class, GzipCodec.class);
|
2023-10-19 18:58:25 +02:00
|
|
|
|
2023-07-17 15:04:21 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-20 11:48:21 +02:00
|
|
|
private static <I extends Result> JavaPairRDD<Text, Text> prepareAffiliationRelations(SparkSession spark,
|
|
|
|
String inputPath,
|
|
|
|
List<KeyValue> collectedfrom) {
|
2023-07-17 15:04:21 +02:00
|
|
|
|
|
|
|
// load and parse affiliation relations from HDFS
|
2023-07-28 18:03:47 +02:00
|
|
|
Dataset<Row> df = spark
|
|
|
|
.read()
|
2023-09-04 15:04:41 +02:00
|
|
|
.schema("`DOI` STRING, `Matchings` ARRAY<STRUCT<`RORid`:STRING,`Confidence`:DOUBLE>>")
|
2023-07-28 18:03:47 +02:00
|
|
|
.json(inputPath);
|
|
|
|
|
|
|
|
// unroll nested arrays
|
|
|
|
df = df
|
|
|
|
.withColumn("matching", functions.explode(new Column("Matchings")))
|
|
|
|
.select(
|
|
|
|
new Column("DOI").as("doi"),
|
2023-09-04 15:04:41 +02:00
|
|
|
new Column("matching.RORid").as("rorid"),
|
2023-07-28 18:03:47 +02:00
|
|
|
new Column("matching.Confidence").as("confidence"));
|
2023-07-17 15:04:21 +02:00
|
|
|
|
|
|
|
// prepare action sets for affiliation relations
|
2023-10-19 18:58:25 +02:00
|
|
|
return df
|
2023-07-28 18:03:47 +02:00
|
|
|
.toJavaRDD()
|
|
|
|
.flatMap((FlatMapFunction<Row, Relation>) row -> {
|
2023-07-17 15:04:21 +02:00
|
|
|
|
|
|
|
// DOI to OpenAIRE id
|
|
|
|
final String paperId = ID_PREFIX
|
2023-07-28 18:03:47 +02:00
|
|
|
+ IdentifierFactory.md5(CleaningFunctions.normalizePidValue("doi", row.getAs("doi")));
|
2023-07-17 15:04:21 +02:00
|
|
|
|
|
|
|
// ROR id to OpenAIRE id
|
2023-07-28 18:03:47 +02:00
|
|
|
final String affId = GenerateRorActionSetJob.calculateOpenaireId(row.getAs("rorid"));
|
2023-07-17 15:04:21 +02:00
|
|
|
|
|
|
|
Qualifier qualifier = OafMapperUtils
|
|
|
|
.qualifier(
|
|
|
|
BIP_AFFILIATIONS_CLASSID,
|
|
|
|
BIP_AFFILIATIONS_CLASSNAME,
|
|
|
|
ModelConstants.DNET_PROVENANCE_ACTIONS,
|
|
|
|
ModelConstants.DNET_PROVENANCE_ACTIONS);
|
|
|
|
|
|
|
|
// format data info; setting `confidence` into relation's `trust`
|
|
|
|
DataInfo dataInfo = OafMapperUtils
|
|
|
|
.dataInfo(
|
|
|
|
false,
|
|
|
|
BIP_INFERENCE_PROVENANCE,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
qualifier,
|
2023-07-28 18:03:47 +02:00
|
|
|
Double.toString(row.getAs("confidence")));
|
2023-07-17 15:04:21 +02:00
|
|
|
|
|
|
|
// return bi-directional relations
|
2023-09-04 15:56:06 +02:00
|
|
|
return getAffiliationRelationPair(paperId, affId, collectedfrom, dataInfo).iterator();
|
2023-07-17 15:04:21 +02:00
|
|
|
|
2023-07-28 18:03:47 +02:00
|
|
|
})
|
2023-07-17 15:04:21 +02:00
|
|
|
.map(p -> new AtomicAction(Relation.class, p))
|
|
|
|
.mapToPair(
|
|
|
|
aa -> new Tuple2<>(new Text(aa.getClazz().getCanonicalName()),
|
2023-10-19 18:58:25 +02:00
|
|
|
new Text(OBJECT_MAPPER.writeValueAsString(aa))));
|
2023-07-17 15:04:21 +02:00
|
|
|
}
|
|
|
|
|
2023-09-04 15:56:06 +02:00
|
|
|
private static List<Relation> getAffiliationRelationPair(String paperId, String affId, List<KeyValue> collectedfrom,
|
|
|
|
DataInfo dataInfo) {
|
2023-07-17 15:04:21 +02:00
|
|
|
return Arrays
|
|
|
|
.asList(
|
|
|
|
OafMapperUtils
|
|
|
|
.getRelation(
|
|
|
|
paperId,
|
|
|
|
affId,
|
|
|
|
ModelConstants.RESULT_ORGANIZATION,
|
|
|
|
ModelConstants.AFFILIATION,
|
|
|
|
ModelConstants.HAS_AUTHOR_INSTITUTION,
|
2023-09-04 15:56:06 +02:00
|
|
|
collectedfrom,
|
2023-07-17 15:04:21 +02:00
|
|
|
dataInfo,
|
|
|
|
null),
|
|
|
|
OafMapperUtils
|
|
|
|
.getRelation(
|
|
|
|
affId,
|
|
|
|
paperId,
|
|
|
|
ModelConstants.RESULT_ORGANIZATION,
|
|
|
|
ModelConstants.AFFILIATION,
|
|
|
|
ModelConstants.IS_AUTHOR_INSTITUTION_OF,
|
2023-09-04 15:56:06 +02:00
|
|
|
collectedfrom,
|
2023-07-17 15:04:21 +02:00
|
|
|
dataInfo,
|
|
|
|
null));
|
|
|
|
}
|
2023-07-05 23:51:01 +02:00
|
|
|
}
|