Project

General

Profile

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() == null || 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() == null || 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

    
95

    
96
        if (instance.getDataciteDatasource() != null) {
97
            return instance.getDataciteDatasource().get(publisher.trim().toLowerCase());
98
        }
99
        return "";
100
    }
101

    
102
	public static void generateDSMap() throws ISLookUpException {
103
		if (datasources.keySet() != null && datasources.keySet().size() > 0)
104
			return;
105

    
106
		final String query = "for $x in collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType') "
107
				+ "return concat($x//FIELD[./key='NamespacePrefix']/value/text(),'@--@',$x//FIELD[./key='DataSourceId']/value/text(),'@--@',$x//ENGLISH_NAME )";
108
        final ISLookUpService lookupService = instance.getServiceLocator().getService(ISLookUpService.class);
109
        final List<String> results = lookupService.quickSearchProfile(query);
110
        datasources.clear();
111
		if (results != null)
112
			results.forEach(it -> {
113
				final String[] splitted = it.split("@--@");
114
				if (splitted != null && splitted.length == 3) {
115
					datasources.put(splitted[0], new ImmutablePair<>(splitted[1], splitted[2]));
116
				}
117
			});
118
	}
119

    
120
	public static String inferPidType(final String pid, final String pidType) {
121
		if (pidType != null && !pidType.toLowerCase().equals("doi")) {
122
			if (pid != null && pid.contains("http://dx.doi.org/") || pid.contains("http://doi.org/"))
123
				return "doi";
124
		}
125
		return pidType;
126
	}
127

    
128
	public static String fixPID(String input) {
129
		if (input != null) {
130
			return input.replace("http://dx.doi.org/", "").replace("http://doi.org/", "");
131
		}
132
		return input;
133
	}
134

    
135
    public static String normalizeRelation(final String relation) {
136
        if (relation == null || StringUtils.isEmpty(relation)) {
137
            return null;
138
        }
139
        return Character.toLowerCase(relation.charAt(0)) + relation.substring(1);
140

    
141
    }
142

    
143
    public static String getInverse(final String relation) throws Exception {
144
        if (ontologies == null) {
145
            ontologies = OntologyLoader.loadOntologiesFromIS();
146
        }
147
        final String normalizedRelation = normalizeRelation(relation);
148

    
149

    
150
        try {
151
        	return ontologies.getTerms(normalizedRelation).stream().findFirst().get().getInverseCode();
152
        } catch (Throwable e) {
153
            System.out.println("Relation not found = " + normalizedRelation);
154
            return "related";
155
        }
156
    }
157

    
158

    
159
	public static String generateIdentifier(final String pid, final String pidtype) {
160
		if (StringUtils.isBlank(pid) || StringUtils.isBlank(pidtype))
161
			throw new RuntimeException("Error pid or pidtype cannot be null");
162
		return DnetXsltFunctions.md5(String.format("%s::%s", pid.toLowerCase().trim(), pidtype.toLowerCase().trim()));
163
	}
164

    
165
    /**
166
     * This method is used only for test Scope
167
     *
168
     * @param mockInstance
169
     */
170
    public static void setInstance(final DLIUtils mockInstance) {
171
        instance = mockInstance;
172
    }
173

    
174
	@PostConstruct
175
    public void registerInstance() throws Exception {
176
        instance = this;
177
        final InputStream inputStream = this.getClass().getResourceAsStream("/eu/dnetlib/transformation/data-center.json");
178
        dataciteDatasource = new Gson().fromJson(IOUtils.toString(inputStream), Map.class);
179
    }
180

    
181
	public UniqueServiceLocator getServiceLocator() {
182
		return serviceLocator;
183
	}
184

    
185
	public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
186
		this.serviceLocator = serviceLocator;
187
	}
188

    
189
    public Map<String, String> getDataciteDatasource() {
190
        return dataciteDatasource;
191
    }
192
}
    (1-1/1)