1
1
/** Elixir BioHackathon 2022 */
2
2
package com .elixir .biohackaton .ISAToSRA .biosamples .service ;
3
3
4
- import com .elixir .biohackaton .ISAToSRA .biosamples .model .Attribute ;
5
- import com .elixir .biohackaton .ISAToSRA .biosamples .model .BiosampleAccessionsMap ;
6
- import com .elixir .biohackaton .ISAToSRA .biosamples .model .Relationship ;
7
- import com .elixir .biohackaton .ISAToSRA .biosamples .model .BioSample ;
8
- import com .elixir .biohackaton .ISAToSRA .receipt .ReceiptAccessionsMap ;
9
- import com .elixir .biohackaton .ISAToSRA .receipt .isamodel .*;
10
-
11
4
import java .time .Instant ;
12
- import java .util .*;
5
+ import java .util .ArrayList ;
6
+ import java .util .Collections ;
7
+ import java .util .List ;
8
+ import java .util .Map ;
13
9
import java .util .concurrent .atomic .AtomicReference ;
14
- import lombok .extern .slf4j .Slf4j ;
10
+
11
+ import org .springframework .beans .factory .annotation .Autowired ;
15
12
import org .springframework .core .ParameterizedTypeReference ;
16
13
import org .springframework .hateoas .EntityModel ;
17
14
import org .springframework .http .HttpEntity ;
21
18
import org .springframework .stereotype .Service ;
22
19
import org .springframework .web .client .RestTemplate ;
23
20
21
+ import com .elixir .biohackaton .ISAToSRA .biosamples .model .Attribute ;
22
+ import com .elixir .biohackaton .ISAToSRA .biosamples .model .BioSample ;
23
+ import com .elixir .biohackaton .ISAToSRA .biosamples .model .BiosampleAccessionsMap ;
24
+ import com .elixir .biohackaton .ISAToSRA .biosamples .model .Relationship ;
25
+ import com .elixir .biohackaton .ISAToSRA .receipt .MarsReceiptException ;
26
+ import com .elixir .biohackaton .ISAToSRA .receipt .ReceiptAccessionsMap ;
27
+ import com .elixir .biohackaton .ISAToSRA .receipt .isamodel .Category ;
28
+ import com .elixir .biohackaton .ISAToSRA .receipt .isamodel .Characteristic ;
29
+ import com .elixir .biohackaton .ISAToSRA .receipt .isamodel .Sample ;
30
+ import com .elixir .biohackaton .ISAToSRA .receipt .isamodel .Source ;
31
+ import com .elixir .biohackaton .ISAToSRA .receipt .isamodel .Study ;
32
+ import com .elixir .biohackaton .ISAToSRA .receipt .isamodel .Value ;
33
+
34
+ import lombok .extern .slf4j .Slf4j ;
35
+
24
36
@ Service
25
37
@ Slf4j
26
38
public class BioSamplesSubmitter {
27
39
40
+ @ Autowired
41
+ private MarsReceiptService marsReceiptService ;
42
+
28
43
public BiosampleAccessionsMap createBioSamples (final List <Study > studies , final String webinToken ) {
29
44
final BiosampleAccessionsMap typeToBioSamplesAccessionMap = new BiosampleAccessionsMap ();
30
45
@@ -38,7 +53,7 @@ public BiosampleAccessionsMap createBioSamples(final List<Study> studies, final
38
53
}
39
54
}
40
55
41
- typeToBioSamplesAccessionMap .sourceAccessionsMap .keyName = Source .Fields .name ;
56
+ typeToBioSamplesAccessionMap .sourceAccessionsMap .isaItemName = Source .Fields .name ;
42
57
typeToBioSamplesAccessionMap .sourceAccessionsMap .accessionMap .put (
43
58
sourceBioSample .getName (),
44
59
sourceBioSample .getAccession ());
@@ -51,36 +66,44 @@ public BiosampleAccessionsMap createBioSamples(final List<Study> studies, final
51
66
typeToBioSamplesAccessionMap .studyAccessionsMap = new ReceiptAccessionsMap (
52
67
Study .Fields .title ,
53
68
study .getTitle ());
54
-
55
69
study
56
70
.getMaterials ()
57
71
.getSamples ()
58
72
.forEach (
59
73
sample -> {
60
- final BioSample persistedChildSample = this .createAndUpdateChildSampleWithRelationship (
61
- sample ,
62
- sourceBioSample .getAccession (),
63
- finalSourceBioSampleOrganismAttribute .getValue (),
64
- webinToken );
65
-
66
- if (persistedChildSample != null ) {
67
- final Characteristic biosampleAccessionCharacteristic = getBioSampleAccessionCharacteristic (
68
- new AtomicReference <>(persistedChildSample ));
69
- final ArrayList <Characteristic > sampleCharacteristics = sample .getCharacteristics () != null
70
- ? sample .getCharacteristics ()
71
- : new ArrayList <>();
72
- sampleCharacteristics .add (biosampleAccessionCharacteristic );
73
-
74
- typeToBioSamplesAccessionMap .sampleAccessionsMap .keyName = Sample .Fields .name ;
75
- typeToBioSamplesAccessionMap .sampleAccessionsMap .accessionMap .put (
76
- persistedChildSample .getName (),
77
- persistedChildSample .getAccession ());
74
+ try {
75
+ final BioSample persistedChildSample = this .createAndUpdateChildSampleWithRelationship (
76
+ sample ,
77
+ sourceBioSample .getAccession (),
78
+ finalSourceBioSampleOrganismAttribute .getValue (),
79
+ webinToken );
80
+
81
+ if (persistedChildSample != null ) {
82
+ final Characteristic biosampleAccessionCharacteristic = getBioSampleAccessionCharacteristic (
83
+ new AtomicReference <>(persistedChildSample ));
84
+ final ArrayList <Characteristic > sampleCharacteristics = sample
85
+ .getCharacteristics () != null
86
+ ? sample .getCharacteristics ()
87
+ : new ArrayList <>();
88
+ sampleCharacteristics .add (biosampleAccessionCharacteristic );
89
+
90
+ typeToBioSamplesAccessionMap .sampleAccessionsMap .isaItemName = Sample .Fields .name ;
91
+ typeToBioSamplesAccessionMap .sampleAccessionsMap .accessionMap .put (
92
+ persistedChildSample .getName (),
93
+ persistedChildSample .getAccession ());
94
+ }
95
+ } catch (Exception e ) {
96
+ throw new MarsReceiptException (e ,
97
+ "Failed to parse ISA Json and create samples in BioSamples (SAMPLE)" ,
98
+ marsReceiptService .getSampleMarsPath (
99
+ Map .entry (Study .Fields .title , study .title ),
100
+ Map .entry (Sample .Fields .id , sample .id )));
78
101
}
79
102
});
80
103
});
81
104
}
82
105
} catch (final Exception e ) {
83
- throw new RuntimeException ( "Failed to parse ISA Json and create samples in BioSamples" , e );
106
+ throw new MarsReceiptException ( e , "Failed to parse ISA Json and create samples in BioSamples" );
84
107
}
85
108
86
109
return typeToBioSamplesAccessionMap ;
@@ -95,8 +118,8 @@ private BioSample createAndUpdateChildSampleWithRelationship(
95
118
.withRelease (Instant .now ())
96
119
.withAttributes (
97
120
List .of (Attribute .build ("organism" , parentSampleOrganism ),
98
- Attribute .build ("collection date" , "not provided" ),
99
- Attribute .build ("geographic location (country and/or sea)" , "not provided" )))
121
+ Attribute .build ("collection date" , "not provided" ),
122
+ Attribute .build ("geographic location (country and/or sea)" , "not provided" )))
100
123
.build ();
101
124
try {
102
125
final EntityModel <BioSample > persistedSampleEntity = this .createSampleInBioSamples (bioSample , webinToken );
@@ -122,7 +145,7 @@ private BioSample createAndUpdateChildSampleWithRelationship(
122
145
return null ;
123
146
}
124
147
} catch (final Exception e ) {
125
- throw new RuntimeException ( "Failed to handle child samples" , e );
148
+ throw new MarsReceiptException ( e , "Failed to handle child samples" );
126
149
}
127
150
}
128
151
@@ -150,8 +173,8 @@ private BioSample createSourceBioSample(final List<Study> studies, final String
150
173
final BioSample sourceSample = new BioSample .Builder (source .getName ())
151
174
.withRelease (Instant .now ())
152
175
.withAttributes (List .of (organismAttribute .get (),
153
- Attribute .build ("collection date" , "not provided" ),
154
- Attribute .build ("geographic location (country and/or sea)" , "not provided" )))
176
+ Attribute .build ("collection date" , "not provided" ),
177
+ Attribute .build ("geographic location (country and/or sea)" , "not provided" )))
155
178
.build ();
156
179
final EntityModel <BioSample > persistedParentSampleEntity = this .createSampleInBioSamples (sourceSample ,
157
180
webinToken );
@@ -165,7 +188,7 @@ private BioSample createSourceBioSample(final List<Study> studies, final String
165
188
sourceCharacteristics .add (biosampleAccessionCharacteristic );
166
189
source .setCharacteristics (sourceCharacteristics );
167
190
} else {
168
- throw new RuntimeException ("Failed to store source sample to BioSamples" );
191
+ throw new MarsReceiptException ("Failed to store source sample to BioSamples" );
169
192
}
170
193
}));
171
194
@@ -204,8 +227,8 @@ private BioSample updateSampleWithRelationshipsToBioSamples(
204
227
new ParameterizedTypeReference <>() {
205
228
});
206
229
return biosamplesResponse .getBody ().getContent ();
207
- } catch (final Exception ex ) {
208
- throw new RuntimeException ( "Failed to add relationships to child samples" , ex );
230
+ } catch (final Exception e ) {
231
+ throw new MarsReceiptException ( e , "Failed to add relationships to child samples" );
209
232
}
210
233
}
211
234
@@ -226,8 +249,8 @@ private EntityModel<BioSample> createSampleInBioSamples(
226
249
});
227
250
228
251
return biosamplesResponse .getBody ();
229
- } catch (final Exception ex ) {
230
- throw new RuntimeException ( "Failed to create samples in BioSamples" , ex );
252
+ } catch (final Exception e ) {
253
+ throw new MarsReceiptException ( e , "Failed to create samples in BioSamples" );
231
254
}
232
255
}
233
256
0 commit comments