1
1
package life .qbic .model .download ;
2
2
3
- import ch .ethz .sis .openbis .generic .asapi . v3 . IApplicationServerApi ;
3
+ import ch .ethz .sis .openbis .generic .OpenBIS ;
4
4
import ch .ethz .sis .openbis .generic .asapi .v3 .dto .common .search .SearchResult ;
5
+ import ch .ethz .sis .openbis .generic .asapi .v3 .dto .dataset .DataSet ;
6
+ import ch .ethz .sis .openbis .generic .asapi .v3 .dto .dataset .fetchoptions .DataSetFetchOptions ;
7
+ import ch .ethz .sis .openbis .generic .asapi .v3 .dto .dataset .id .DataSetPermId ;
8
+ import ch .ethz .sis .openbis .generic .asapi .v3 .dto .dataset .search .DataSetSearchCriteria ;
9
+ import ch .ethz .sis .openbis .generic .asapi .v3 .dto .entitytype .EntityKind ;
10
+ import ch .ethz .sis .openbis .generic .asapi .v3 .dto .entitytype .id .EntityTypePermId ;
11
+ import ch .ethz .sis .openbis .generic .asapi .v3 .dto .experiment .fetchoptions .ExperimentFetchOptions ;
12
+ import ch .ethz .sis .openbis .generic .asapi .v3 .dto .experiment .id .ExperimentIdentifier ;
13
+ import ch .ethz .sis .openbis .generic .asapi .v3 .dto .experiment .search .ExperimentSearchCriteria ;
5
14
import ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample ;
6
15
import ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .SampleType ;
7
16
import ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .fetchoptions .SampleFetchOptions ;
8
17
import ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .search .SampleSearchCriteria ;
9
18
import ch .ethz .sis .openbis .generic .asapi .v3 .dto .space .Space ;
10
19
import ch .ethz .sis .openbis .generic .asapi .v3 .dto .space .fetchoptions .SpaceFetchOptions ;
11
20
import ch .ethz .sis .openbis .generic .asapi .v3 .dto .space .search .SpaceSearchCriteria ;
12
- import ch .systemsx .cisd .common .spring .HttpInvokerUtils ;
21
+ import ch .ethz .sis .openbis .generic .dssapi .v3 .dto .dataset .create .UploadedDataSetCreation ;
22
+ import ch .ethz .sis .openbis .generic .dssapi .v3 .dto .datasetfile .DataSetFile ;
23
+ import ch .ethz .sis .openbis .generic .dssapi .v3 .dto .datasetfile .download .DataSetFileDownload ;
24
+ import ch .ethz .sis .openbis .generic .dssapi .v3 .dto .datasetfile .download .DataSetFileDownloadOptions ;
25
+ import ch .ethz .sis .openbis .generic .dssapi .v3 .dto .datasetfile .download .DataSetFileDownloadReader ;
26
+ import ch .ethz .sis .openbis .generic .dssapi .v3 .dto .datasetfile .id .DataSetFilePermId ;
27
+ import ch .ethz .sis .openbis .generic .dssapi .v3 .dto .datasetfile .id .IDataSetFileId ;
28
+ import java .io .File ;
29
+ import java .io .FileOutputStream ;
30
+ import java .io .IOException ;
31
+ import java .io .InputStream ;
32
+ import java .nio .file .Path ;
33
+ import java .util .ArrayList ;
34
+ import java .util .Arrays ;
13
35
import java .util .HashMap ;
14
36
import java .util .List ;
15
37
import java .util .Map ;
21
43
public class OpenbisConnector {
22
44
23
45
private static final Logger LOG = LogManager .getLogger (OpenbisConnector .class );
24
- private final IApplicationServerApi applicationServer ;
25
- private final String sessionToken ;
46
+ OpenBIS openBIS ;
47
+
26
48
/**
27
49
* Constructor for a QBiCDataDownloader instance
28
50
*
29
- * @param AppServerUri The openBIS application server URL (AS)
51
+ * @param AppServerUri The openBIS application server URL (AS)
30
52
* @param sessionToken The session token for the datastore & application servers
31
53
*/
32
54
public OpenbisConnector (
33
- String AppServerUri ,
34
- String sessionToken ) {
55
+ String AppServerUri ,
56
+ String sessionToken ) {
57
+ /*
35
58
this.sessionToken = sessionToken;
36
59
37
60
if (!AppServerUri.isEmpty()) {
@@ -41,57 +64,152 @@ public OpenbisConnector(
41
64
} else {
42
65
applicationServer = null;
43
66
}
67
+
68
+ */
69
+ }
70
+
71
+ public OpenbisConnector (OpenBIS authentication ) {
72
+ this .openBIS = authentication ;
44
73
}
45
74
46
75
public List <String > getSpaces () {
47
76
SpaceSearchCriteria criteria = new SpaceSearchCriteria ();
48
77
SpaceFetchOptions options = new SpaceFetchOptions ();
49
- return applicationServer .searchSpaces (sessionToken , criteria , options ).getObjects ()
78
+ return openBIS .searchSpaces (criteria , options ).getObjects ()
50
79
.stream ().map (Space ::getCode ).collect (Collectors .toList ());
51
80
}
52
81
82
+ public DataSetPermId registerDataset (Path uploadPath , String experimentID ,
83
+ List <String > parentCodes ) {
84
+ final String uploadId = openBIS .uploadFileWorkspaceDSS (uploadPath );
85
+
86
+ final UploadedDataSetCreation creation = new UploadedDataSetCreation ();
87
+ creation .setUploadId (uploadId );
88
+ creation .setExperimentId (new ExperimentIdentifier (experimentID ));
89
+ creation .setParentIds (parentCodes .stream ().map (DataSetPermId ::new ).collect (
90
+ Collectors .toList ()));
91
+ creation .setTypeId (new EntityTypePermId ("UNKNOWN" , EntityKind .DATA_SET ));
92
+
93
+ try {
94
+ return openBIS .createUploadedDataSet (creation );
95
+ } catch (final Exception e ) {
96
+ LOG .error (e .getMessage ());
97
+ }
98
+ return null ;
99
+ }
100
+
101
+ private static void copyInputStreamToFile (InputStream inputStream , File file )
102
+ throws IOException {
103
+ System .err .println (file .getPath ());
104
+ try (FileOutputStream outputStream = new FileOutputStream (file , false )) {
105
+ int read ;
106
+ byte [] bytes = new byte [8192 ];
107
+ while ((read = inputStream .read (bytes )) != -1 ) {
108
+ outputStream .write (bytes , 0 , read );
109
+ }
110
+ }
111
+ }
112
+
113
+ public List <DataSet > listDatasetsOfExperiment (List <String > spaces , String experiment ) {
114
+ DataSetSearchCriteria criteria = new DataSetSearchCriteria ();
115
+ criteria .withExperiment ().withCode ().thatEquals (experiment );
116
+ if (!spaces .isEmpty ()) {
117
+ criteria .withAndOperator ();
118
+ criteria .withExperiment ().withProject ().withSpace ().withCodes ().thatIn (spaces );
119
+ }
120
+ DataSetFetchOptions options = new DataSetFetchOptions ();
121
+ options .withType ();
122
+ options .withRegistrator ();
123
+ options .withExperiment ().withProject ().withSpace ();
124
+ return openBIS .searchDataSets (criteria , options ).getObjects ();
125
+ }
126
+
127
+ public void downloadDataset (String targetPath , String datasetID ) throws IOException {
128
+ DataSetFileDownloadOptions options = new DataSetFileDownloadOptions ();
129
+ IDataSetFileId fileToDownload = new DataSetFilePermId (new DataSetPermId (datasetID ),
130
+ "" );
131
+
132
+ System .err .println (fileToDownload );
133
+ // Setting recursive flag to true will return both subfolders and files
134
+ options .setRecursive (true );
135
+
136
+ // Read the contents and print them out
137
+ InputStream stream = openBIS .downloadFiles (new ArrayList <>(List .of (fileToDownload )),
138
+ options );
139
+ DataSetFileDownloadReader reader = new DataSetFileDownloadReader (stream );
140
+ DataSetFileDownload file = null ;
141
+ while ((file = reader .read ()) != null ) {
142
+ DataSetFile df = file .getDataSetFile ();
143
+ String currentPath = df .getPath ().replace ("original" , "" );
144
+ if (df .isDirectory ()) {
145
+ File newDir = new File (targetPath , currentPath );
146
+ if (!newDir .exists ()) {
147
+ newDir .mkdirs ();
148
+ }
149
+ } else {
150
+ File toWrite = new File (targetPath , currentPath );
151
+ copyInputStreamToFile (file .getInputStream (), toWrite );
152
+ }
153
+ }
154
+ }
155
+
53
156
public Map <SampleTypeConnection , Integer > queryFullSampleHierarchy (List <String > spaces ) {
54
157
Map <SampleTypeConnection , Integer > hierarchy = new HashMap <>();
55
- if (spaces .isEmpty ()) {
158
+ if (spaces .isEmpty ()) {
56
159
spaces = getSpaces ();
57
160
}
58
- for (String space : spaces ) {
161
+ for (String space : spaces ) {
59
162
SampleFetchOptions fetchType = new SampleFetchOptions ();
60
163
fetchType .withType ();
61
164
SampleFetchOptions withDescendants = new SampleFetchOptions ();
62
165
withDescendants .withChildrenUsing (fetchType );
63
166
withDescendants .withType ();
64
167
SampleSearchCriteria criteria = new SampleSearchCriteria ();
65
168
criteria .withSpace ().withCode ().thatEquals (space .toUpperCase ());
66
- SearchResult <Sample > result = applicationServer .searchSamples (
67
- sessionToken , criteria , withDescendants );
169
+ SearchResult <Sample > result = openBIS .searchSamples (criteria , withDescendants );
68
170
for (Sample s : result .getObjects ()) {
69
- SampleType parentType = s .getType ();
70
- List <Sample > children = s .getChildren ();
71
- if (children .isEmpty ()) {
72
- SampleTypeConnection leaf = new SampleTypeConnection (parentType );
73
- if (hierarchy .containsKey (leaf )) {
74
- int count = hierarchy .get (leaf ) + 1 ;
75
- hierarchy .put (leaf , count );
76
- } else {
77
- hierarchy .put (leaf , 1 );
78
- }
171
+ SampleType parentType = s .getType ();
172
+ List <Sample > children = s .getChildren ();
173
+ if (children .isEmpty ()) {
174
+ SampleTypeConnection leaf = new SampleTypeConnection (parentType );
175
+ if (hierarchy .containsKey (leaf )) {
176
+ int count = hierarchy .get (leaf ) + 1 ;
177
+ hierarchy .put (leaf , count );
79
178
} else {
80
- for (Sample c : children ) {
81
- SampleType childType = c .getType ();
82
- SampleTypeConnection connection = new SampleTypeConnection (parentType , childType );
83
- if (hierarchy .containsKey (connection )) {
84
- int count = hierarchy .get (connection ) + 1 ;
85
- hierarchy .put (connection , count );
86
- } else {
87
- hierarchy .put (connection , 1 );
88
- }
179
+ hierarchy .put (leaf , 1 );
180
+ }
181
+ } else {
182
+ for (Sample c : children ) {
183
+ SampleType childType = c .getType ();
184
+ SampleTypeConnection connection = new SampleTypeConnection (parentType , childType );
185
+ if (hierarchy .containsKey (connection )) {
186
+ int count = hierarchy .get (connection ) + 1 ;
187
+ hierarchy .put (connection , count );
188
+ } else {
189
+ hierarchy .put (connection , 1 );
89
190
}
191
+ }
90
192
}
91
193
}
92
194
}
93
195
return hierarchy ;
94
196
}
95
197
198
+ public List <DataSet > findDataSets (List <String > codes ) {
199
+ if (codes .isEmpty ()) {
200
+ return new ArrayList <>();
201
+ }
202
+ DataSetSearchCriteria criteria = new DataSetSearchCriteria ();
203
+ criteria .withCodes ().thatIn (codes );
204
+ DataSetFetchOptions options = new DataSetFetchOptions ();
205
+ return openBIS .searchDataSets (criteria , options ).getObjects ();
206
+ }
207
+
208
+ public boolean experimentExists (String experimentID ) {
209
+ ExperimentSearchCriteria criteria = new ExperimentSearchCriteria ();
210
+ criteria .withIdentifier ().thatEquals (experimentID );
96
211
212
+ return !openBIS .searchExperiments (criteria , new ExperimentFetchOptions ()).getObjects ()
213
+ .isEmpty ();
214
+ }
97
215
}
0 commit comments