forked from andrawaag/EnsemblLinksetExtractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetLinkset.java
216 lines (193 loc) · 9.9 KB
/
getLinkset.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.zip.GZIPInputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPListParseEngine;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.DC;
import com.hp.hpl.jena.vocabulary.DCTerms;
import com.hp.hpl.jena.vocabulary.RDF;
public class getLinkset {
/**
* @param args
* @throws IOException
*/
/*public static InputStream downLoad(String file, String directory) throws IOException{
//Here the Ensembl ftp server is contacted and the given file is downloaded and stored in the /tmp directory
FTPClient ftp = new FTPClient();
ftp.setControlEncoding("UTF-16");
ftp.connect("ftp.ensembl.org");
ftp.setFileType(FTPClient.COMPRESSED_TRANSFER_MODE);
ftp.setFileType(FTP.COMPRESSED_TRANSFER_MODE);
ftp.login("anonymous", "");
InputStream inputStream = ftp.retrieveFileStream(directory+file);
System.out.println(ftp.getReplyString());
return inputStream;
} */
public static void convertGene2RDF(Model model, String geneFile, String species, String directory) throws IOException{
System.out.println("Gene");
InputStream fileStream = new FileInputStream(directory+"/gene.txt.gz");
InputStream gzipStream = new GZIPInputStream(fileStream);
BufferedReader buffered = new BufferedReader(new InputStreamReader(gzipStream));
String line;
Property hasStableId = model.createProperty("http://hasExternalEnsemblId");
while ( ((line = buffered.readLine()) != null)){
//System.out.println(line);
String[] fields = line.split("\\t");
Resource internalIdResource = model.createResource("http://internalEnsemblId#"+fields[0]);
Resource stableIdResource = model.createResource("http://externalEnsemblId#"+fields[14]);
internalIdResource.addProperty(hasStableId, stableIdResource);
}
}
public static void convertXref2RDF(Model model, String geneFile, String species, String directory) throws IOException{
System.out.println("http://xref");
InputStream fileStream = new FileInputStream(directory+"/xref.txt.gz");
InputStream gzipStream = new GZIPInputStream(fileStream);
BufferedReader buffered = new BufferedReader(new InputStreamReader(gzipStream));
String line;
Property hasExternalDbId = model.createProperty("http://hasExternalDbId");
Property hasDbPrimary_acc = model.createProperty("http://hasDbprimary_acc");
Property hasDisplayLabel = model.createProperty("http://hasDisplayLabel");
Resource xrefType = model.createResource("xref");
while ( ((line = buffered.readLine()) != null)){
String[] fields = line.split("\\t");
Resource xrefidResource = model.createResource("http://xrefid#"+fields[0]);
xrefidResource.addProperty(RDF.type, xrefType);
Resource externaldbidResource = model.createResource("http://external_db_id#"+fields[1]);
Resource dbprimaryaccResource = model.createResource("http://dbprimary_acc#"+URLEncoder.encode(fields[2], "UTF-8"));
Resource displayLabelResource = model.createResource("http://display_label#"+URLEncoder.encode(fields[3], "UTF-8"));
xrefidResource.addProperty(hasExternalDbId, externaldbidResource);
xrefidResource.addProperty(hasDisplayLabel, displayLabelResource);
xrefidResource.addProperty(hasDbPrimary_acc, dbprimaryaccResource);
}
}
public static void convertExternalDb2RDF(Model model, String geneFile, String species, String directory) throws IOException{
System.out.println("externalDb");
InputStream fileStream = new FileInputStream(directory+"/external_db.txt.gz");
InputStream gzipStream = new GZIPInputStream(fileStream);
BufferedReader buffered = new BufferedReader(new InputStreamReader(gzipStream));
String line;
Property hasDbName = model.createProperty("http://hasDbName");
Property hasDbRelease = model.createProperty("http://hasDbRelease");
while ( ((line = buffered.readLine()) != null)){
String[] fields = line.split("\\t");
Resource externalDbIdResource = model.createResource("http://external_db_id#"+fields[0]);
Resource dbNameResource = model.createResource("http://dbName#"+fields[1].replace(" ", ""));
//Resource dbRelease = model.createResource("dbRelease#"+fields[2]);
externalDbIdResource.addProperty(hasDbName, dbNameResource);
//externalDbIdResource.addProperty(hasDbRelease, dbRelease);
}
}
public static void convertObjectXref2RDF(Model model, String geneFile, String species, String directory) throws IOException{
System.out.println("objectXref");
InputStream fileStream = new FileInputStream(directory+"/object_xref.txt.gz");
InputStream gzipStream = new GZIPInputStream(fileStream);
BufferedReader buffered = new BufferedReader(new InputStreamReader(gzipStream));
String line;
Property hasInternalEnsemblId = model.createProperty("http://hasInternalEnsemblId");
while ( ((line = buffered.readLine()) != null)){
String[] fields = line.split("\\t");
Resource objectXrefId = model.createResource("http://xrefid#"+fields[3]);
Resource ensemblId = model.createResource("http://internalEnsemblId#"+fields[1]);
//System.out.println("xrefid#"+fields[3] + " - "+ "internalEnsemblId#"+fields[1]);
objectXrefId.addProperty(hasInternalEnsemblId, ensemblId);
}
}
public static void main(String[] args) throws IOException {
// Download the following species file
//downloadSpecies("homo_sapiens");
String directory = args[0];
String[] directorySplit = directory.split("_");
String species = directorySplit[0] + directorySplit[1];
Model ensemblModel = ModelFactory.createDefaultModel();
Model voidModel = ModelFactory.createDefaultModel();
Resource sourceResource = voidModel.createResource(directory);
sourceResource.addLiteral(Pav.version, directorySplit[3]+"_"+directorySplit[4]);
sourceResource.addLiteral(DCTerms.subject, species);
System.out.println(directory);
convertObjectXref2RDF(ensemblModel, directory+"/object_xref.txt.gz", "homo_sapiens", directory);
convertGene2RDF(ensemblModel, directory+"/gene.txt.gz", "homo_sapiens", directory);
convertXref2RDF(ensemblModel, directory+"/xref.txt.gz", "homo_sapiens", directory);
convertExternalDb2RDF(ensemblModel, directory+"/external_db.txt.gz", "homo_sapiens", directory);
String constructQueryString = "CONSTRUCT {?externalEnsemblId <"+Skos.relatedMatch.getURI()+"> ?dbPrimaryAcc ." +
"?dbPrimaryAcc <"+DC.source.getURI()+"> ?dbName .}" +
"WHERE {" +
" ?xref a <http://xref> ." +
" ?xref <http://hasDbprimary_acc> ?dbPrimaryAcc ." +
" ?xref <http://hasDisplayLabel> ?dbDisplayLabel ." +
" ?xref <http://hasExternalDbId> ?externalDbId ." +
" ?xref <http://hasInternalEnsemblId> ?internalEnsemblId ." +
" ?externalDbId <http://hasDbName> ?dbName ." +
" ?internalEnsemblId <http://hasExternalEnsemblId> ?externalEnsemblId ." +
"}";
System.out.println(constructQueryString);
Query constructquery = QueryFactory.create(constructQueryString);
QueryExecution queryExecution = QueryExecutionFactory.create(constructquery, ensemblModel);
Model resultSetModel = queryExecution.execConstruct();
System.out.println();
FileOutputStream fout;
System.out.println("Is the model empty?: "+resultSetModel.isEmpty());
fout = new FileOutputStream(directory+ "/Stringensembl.ttl");
resultSetModel.write(fout, "TURTLE");
//All the links between Ensembl and external db are extracted and stored in resultSetModel
//The next block separates each
String selectDatasourceString = "SELECT DISTINCT ?dbName" +
"WHERE {" +
" ?externalDbId <http://hasDbName> ?dbName ."+
"}";
Query seperateQuery = QueryFactory.create(selectDatasourceString);
queryExecution = QueryExecutionFactory.create(seperateQuery, resultSetModel);
ResultSet dbResultSet = queryExecution.execSelect();
while (dbResultSet.hasNext()) {
String datasource = dbResultSet.next().get("dbName").toString();
System.out.println(datasource);
String constructseperateQueryString = "CONSTRUCT {?externalEnsemblId <"+Skos.relatedMatch.getURI()+"> ?dbPrimaryAcc ." +
" ?dbPrimaryAcc <"+DC.source.getURI()+"> ?dbName .}" +
" WHERE {" +
" ?xref a <xref> ." +
" ?xref <http://hasDbprimary_acc> ?dbPrimaryAcc ." +
" ?xref <http://hasDisplayLabel> ?dbDisplayLabel ." +
" ?xref <http://hasExternalDbId> ?externalDbId ." +
" ?xref <http://hasInternalEnsemblId> ?internalEnsemblId ." +
" ?externalDbId <http://hasDbName> "+datasource+" ." +
" ?internalEnsemblId <http://hasExternalEnsemblId> ?externalEnsemblId ;" +
" }";
Query query = QueryFactory.create(constructseperateQueryString);
queryExecution = QueryExecutionFactory.create(query, resultSetModel);
Model seperatedLinkSetModel = queryExecution.execConstruct();
FileOutputStream foutlocal;
foutlocal = new FileOutputStream(directory+"/ensembl_"+datasource+".ttl");
seperatedLinkSetModel.write(foutlocal, "TURTLE");
}
System.out.println(directory);
FileOutputStream fout2;
fout2 = new FileOutputStream(directory+"/ensemblAll.ttl");
ensemblModel.write(fout2, "TURTLE");
FileOutputStream voidOut;
voidOut = new FileOutputStream(directory+"/void.ttl");
}
}