1
|
package eu.dnetlib.dli;
|
2
|
|
3
|
import java.io.InputStream;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import javax.annotation.PostConstruct;
|
8
|
|
9
|
import com.google.common.collect.BiMap;
|
10
|
import com.google.common.collect.HashBiMap;
|
11
|
import com.google.gson.Gson;
|
12
|
import eu.dnetlib.data.transform.Ontologies;
|
13
|
import eu.dnetlib.data.transform.OntologyLoader;
|
14
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
15
|
import eu.dnetlib.miscutils.functional.xml.DnetXsltFunctions;
|
16
|
import eu.dnetlib.rmi.enabling.ISLookUpException;
|
17
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
18
|
import org.apache.commons.io.IOUtils;
|
19
|
import org.apache.commons.lang3.StringUtils;
|
20
|
import org.apache.commons.lang3.tuple.ImmutablePair;
|
21
|
import org.apache.commons.lang3.tuple.Pair;
|
22
|
import org.springframework.beans.factory.annotation.Autowired;
|
23
|
|
24
|
public class DLIUtils {
|
25
|
|
26
|
public final static Map<String, Pair<String, String>> datasources = new HashMap<>();
|
27
|
public static final Map<String, String> resolvedTypes = new HashMap<String, String>() {
|
28
|
{
|
29
|
put("pdb", "http://www.rcsb.org/pdb/explore/explore.do?structureId=%s");
|
30
|
put("ncbi-n", "http://www.ncbi.nlm.nih.gov/gquery/?term=%s");
|
31
|
put("pmid", "http://www.ncbi.nlm.nih.gov/pubmed/%s");
|
32
|
put("pmcid", "http://www.ncbi.nlm.nih.gov/pmc/articles/%s");
|
33
|
put("pubmedid", "http://www.ncbi.nlm.nih.gov/pubmed/%s");
|
34
|
put("doi", "http://dx.doi.org/%s");
|
35
|
put("genbank", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
36
|
put("nuccore", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
37
|
put("swiss-prot", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
38
|
put("arrayexpress", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
39
|
put("biomodels", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
40
|
put("bmrb", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
41
|
put("ena", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
42
|
put("genbank", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
43
|
put("geo", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
44
|
put("ensembl", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
45
|
put("mgi", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
46
|
put("bind", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
47
|
put("pride", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
48
|
put("ddbj", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
49
|
put("bioproject", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
50
|
put("embl", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
51
|
put("sra", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
|
52
|
}
|
53
|
|
54
|
};
|
55
|
public static Ontologies ontologies;
|
56
|
private static DLIUtils instance;
|
57
|
private static BiMap<String, String> relations;
|
58
|
|
59
|
private Map<String, String> dataciteDatasource;
|
60
|
|
61
|
@Autowired
|
62
|
private UniqueServiceLocator serviceLocator;
|
63
|
|
64
|
private static BiMap<String, String> getRelationMap() {
|
65
|
|
66
|
if (relations == null) {
|
67
|
relations = HashBiMap.create();
|
68
|
relations.put("IsCitedBy", "Cites");
|
69
|
relations.put("IsSupplementTo", "IsSupplementedBy");
|
70
|
relations.put("IsReferencedBy", "References");
|
71
|
}
|
72
|
return relations;
|
73
|
}
|
74
|
|
75
|
public static String getNameFromDataSourcePrefix(final String datasourcePrefix) throws ISLookUpException {
|
76
|
if (datasources.keySet().size() == 0) {
|
77
|
generateDSMap();
|
78
|
}
|
79
|
if (!datasources.containsKey(datasourcePrefix))
|
80
|
return "";
|
81
|
return datasources.get(datasourcePrefix).getRight();
|
82
|
}
|
83
|
|
84
|
public static String getIdFromDataSourcePrefix(final String datasourcePrefix) throws ISLookUpException {
|
85
|
if (datasources.keySet().size() == 0) {
|
86
|
generateDSMap();
|
87
|
}
|
88
|
if (!datasources.containsKey(datasourcePrefix))
|
89
|
return "";
|
90
|
return datasources.get(datasourcePrefix).getLeft();
|
91
|
}
|
92
|
|
93
|
public static String getPublisherName(final String publisher) {
|
94
|
if (instance.getDataciteDatasource() != null) {
|
95
|
return instance.getDataciteDatasource().get(publisher.trim().toLowerCase());
|
96
|
}
|
97
|
return "";
|
98
|
}
|
99
|
|
100
|
public static void generateDSMap() throws ISLookUpException {
|
101
|
if (datasources.keySet().size() > 0)
|
102
|
return;
|
103
|
|
104
|
final String query = "for $x in collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType') "
|
105
|
+ "return concat($x//FIELD[./key='NamespacePrefix']/value/text(),'@--@',$x//FIELD[./key='DataSourceId']/value/text(),'@--@',$x//ENGLISH_NAME )";
|
106
|
final ISLookUpService lookupService = instance.getServiceLocator().getService(ISLookUpService.class);
|
107
|
final List<String> results = lookupService.quickSearchProfile(query);
|
108
|
datasources.clear();
|
109
|
if (results != null)
|
110
|
results.forEach(it -> {
|
111
|
final String[] splitted = it.split("@--@");
|
112
|
if (splitted.length == 3) {
|
113
|
datasources.put(splitted[0], new ImmutablePair<>(splitted[1], splitted[2]));
|
114
|
}
|
115
|
});
|
116
|
}
|
117
|
|
118
|
public static String inferPidType(final String pid, final String pidType) {
|
119
|
if (pidType != null && !pidType.toLowerCase().equals("doi")) {
|
120
|
if (pid != null && pid.contains("http://dx.doi.org/") || pid.contains("http://doi.org/"))
|
121
|
return "doi";
|
122
|
}
|
123
|
return pidType;
|
124
|
}
|
125
|
|
126
|
public static String fixPID(String input) {
|
127
|
if (input != null) {
|
128
|
return input.replace("http://dx.doi.org/", "").replace("http://doi.org/", "");
|
129
|
}
|
130
|
return null;
|
131
|
}
|
132
|
|
133
|
public static String normalizeRelation(final String relation) {
|
134
|
if (relation == null || StringUtils.isEmpty(relation)) {
|
135
|
return null;
|
136
|
}
|
137
|
return Character.toLowerCase(relation.charAt(0)) + relation.substring(1);
|
138
|
|
139
|
}
|
140
|
|
141
|
public static String getInverse(final String relation) throws Exception {
|
142
|
if (ontologies == null) {
|
143
|
ontologies = OntologyLoader.loadOntologiesFromIS();
|
144
|
}
|
145
|
final String normalizedRelation = normalizeRelation(relation);
|
146
|
|
147
|
|
148
|
try {
|
149
|
return ontologies.getTerms(normalizedRelation).stream().findFirst().get().getInverseCode();
|
150
|
} catch (Throwable e) {
|
151
|
System.out.println("Relation not found = " + normalizedRelation);
|
152
|
return "related";
|
153
|
}
|
154
|
}
|
155
|
|
156
|
|
157
|
public static String generateIdentifier(final String pid, final String pidtype) {
|
158
|
if (StringUtils.isBlank(pid) || StringUtils.isBlank(pidtype))
|
159
|
throw new RuntimeException("Error pid or pidtype cannot be null");
|
160
|
return DnetXsltFunctions.md5(String.format("%s::%s", pid.toLowerCase().trim(), pidtype.toLowerCase().trim()));
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* This method is used only for test Scope
|
165
|
*
|
166
|
* @param mockInstance
|
167
|
*/
|
168
|
public static void setInstance(final DLIUtils mockInstance) {
|
169
|
instance = mockInstance;
|
170
|
}
|
171
|
|
172
|
@PostConstruct
|
173
|
public void registerInstance() throws Exception {
|
174
|
instance = this;
|
175
|
final InputStream inputStream = this.getClass().getResourceAsStream("/eu/dnetlib/transformation/data-center.json");
|
176
|
dataciteDatasource = new Gson().fromJson(IOUtils.toString(inputStream), Map.class);
|
177
|
}
|
178
|
|
179
|
public UniqueServiceLocator getServiceLocator() {
|
180
|
return serviceLocator;
|
181
|
}
|
182
|
|
183
|
public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
|
184
|
this.serviceLocator = serviceLocator;
|
185
|
}
|
186
|
|
187
|
public Map<String, String> getDataciteDatasource() {
|
188
|
return dataciteDatasource;
|
189
|
}
|
190
|
}
|