1
|
/**
|
2
|
* Copyright (C) 2015 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.service.dto;
|
10
|
|
11
|
import java.io.Serializable;
|
12
|
import java.net.URISyntaxException;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Collection;
|
15
|
import java.util.List;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
|
20
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
21
|
import eu.etaxonomy.cdm.common.URI;
|
22
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
23
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
24
|
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
25
|
import eu.etaxonomy.cdm.model.media.MediaUtils;
|
26
|
import eu.etaxonomy.cdm.model.molecular.DnaSample;
|
27
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
28
|
import eu.etaxonomy.cdm.model.molecular.SingleRead;
|
29
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
30
|
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
|
31
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
32
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
33
|
import eu.etaxonomy.cdm.model.term.DefinedTerm;
|
34
|
|
35
|
/**
|
36
|
* @author pplitzner
|
37
|
* @since Mar 26, 2015
|
38
|
*/
|
39
|
public class DerivateDataDTO implements Serializable {
|
40
|
|
41
|
private static final long serialVersionUID = 8329871164348514709L;
|
42
|
|
43
|
private static final Logger logger = Logger.getLogger(DerivateDataDTO.class);
|
44
|
|
45
|
private List<Link> specimenScans = new ArrayList<>();
|
46
|
private List<MolecularData> molecularDataList = new ArrayList<>();
|
47
|
private List<Link> detailImages = new ArrayList<>();
|
48
|
private List<UUID> specimenScanUuids = new ArrayList<>();
|
49
|
private List<UUID> detailImageUuids = new ArrayList<>();
|
50
|
|
51
|
public static DerivateDataDTO fromEntity(SpecimenOrObservationBase<?> specimenOrObservation, String specimenIdentifier) {
|
52
|
|
53
|
DerivateDataDTO derivateDataDTO = new DerivateDataDTO();
|
54
|
|
55
|
Collection<DerivedUnit> childDerivates = specimenOrObservation.collectDerivedUnits();
|
56
|
for (DerivedUnit childDerivate : childDerivates) {
|
57
|
// assemble molecular data
|
58
|
//pattern: DNAMarker [contig1, primer1_1, primer1_2, ...][contig2, primer2_1, ...]...
|
59
|
if (childDerivate.isInstanceOf(DnaSample.class)) {
|
60
|
if (childDerivate.getRecordBasis() == SpecimenOrObservationType.TissueSample) {
|
61
|
// TODO implement TissueSample assembly for web service
|
62
|
}
|
63
|
if (childDerivate.getRecordBasis() == SpecimenOrObservationType.DnaSample) {
|
64
|
DnaSample dna = HibernateProxyHelper.deproxy(childDerivate, DnaSample.class);
|
65
|
for (Sequence sequence : dna.getSequences()) {
|
66
|
URI boldUri = null;
|
67
|
try {
|
68
|
boldUri = sequence.getBoldUri();
|
69
|
} catch (URISyntaxException e1) {
|
70
|
// TODO consider better reporting of this incident
|
71
|
logger.error("Could not create BOLD URI", e1);
|
72
|
}
|
73
|
final DefinedTerm dnaMarker = sequence.getDnaMarker();
|
74
|
Link providerLink = null;
|
75
|
if(boldUri!=null && dnaMarker!=null){
|
76
|
providerLink = new DerivateDataDTO.Link(boldUri, dnaMarker.getLabel());
|
77
|
}
|
78
|
MolecularData molecularData = derivateDataDTO.addProviderLink(providerLink);
|
79
|
|
80
|
//contig file
|
81
|
ContigFile contigFile = null;
|
82
|
if (sequence.getContigFile() != null) {
|
83
|
MediaRepresentationPart contigMediaRepresentationPart = MediaUtils.getFirstMediaRepresentationPart(sequence.getContigFile());
|
84
|
if (contigMediaRepresentationPart != null) {
|
85
|
contigFile = molecularData.addContigFile(new Link(contigMediaRepresentationPart.getUri(), "contig"));
|
86
|
}
|
87
|
}
|
88
|
else{
|
89
|
contigFile = molecularData.addContigFile(null);
|
90
|
}
|
91
|
// primer files
|
92
|
if (sequence.getSingleReads() != null) {
|
93
|
int readCount = 1;
|
94
|
for (SingleRead singleRead : sequence.getSingleReads()) {
|
95
|
MediaRepresentationPart pherogramMediaRepresentationPart = MediaUtils.getFirstMediaRepresentationPart(singleRead.getPherogram());
|
96
|
if (pherogramMediaRepresentationPart != null && contigFile != null) {
|
97
|
contigFile.addPrimerLink(pherogramMediaRepresentationPart.getUri(), "read"+readCount++);
|
98
|
}
|
99
|
}
|
100
|
}
|
101
|
}
|
102
|
}
|
103
|
}
|
104
|
// assemble media data
|
105
|
else if (childDerivate.isInstanceOf(MediaSpecimen.class)) {
|
106
|
MediaSpecimen media = HibernateProxyHelper.deproxy(childDerivate, MediaSpecimen.class);
|
107
|
|
108
|
URI mediaUri = getMediaUri(media);
|
109
|
if (media.getKindOfUnit() != null) {
|
110
|
// specimen scan
|
111
|
if (media.getKindOfUnit().getUuid().equals(DefinedTerm.uuidSpecimenScan)) {
|
112
|
derivateDataDTO.addSpecimenScanUuid(media.getMediaSpecimen().getUuid());
|
113
|
String imageLinkText = "scan";
|
114
|
imageLinkText = specimenIdentifier;
|
115
|
derivateDataDTO.addSpecimenScan(mediaUri, imageLinkText);
|
116
|
}
|
117
|
// detail image
|
118
|
else if (media.getKindOfUnit().getUuid().equals(DefinedTerm.uuidDetailImage)) {
|
119
|
derivateDataDTO.addDetailImageUuid(media.getMediaSpecimen().getUuid());
|
120
|
String motif = "detail image";
|
121
|
if (media.getMediaSpecimen()!=null){
|
122
|
if(CdmUtils.isNotBlank(media.getMediaSpecimen().getTitleCache())) {
|
123
|
motif = media.getMediaSpecimen().getTitleCache();
|
124
|
}
|
125
|
}
|
126
|
derivateDataDTO.addDetailImage(mediaUri, motif);
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
}
|
131
|
return derivateDataDTO;
|
132
|
}
|
133
|
|
134
|
@Deprecated
|
135
|
private static URI getMediaUri(MediaSpecimen mediaSpecimen) {
|
136
|
URI mediaUri = null;
|
137
|
Collection<MediaRepresentation> mediaRepresentations = mediaSpecimen.getMediaSpecimen().getRepresentations();
|
138
|
if (mediaRepresentations != null && !mediaRepresentations.isEmpty()) {
|
139
|
Collection<MediaRepresentationPart> mediaRepresentationParts = mediaRepresentations.iterator().next().getParts();
|
140
|
if (mediaRepresentationParts != null && !mediaRepresentationParts.isEmpty()) {
|
141
|
MediaRepresentationPart part = mediaRepresentationParts.iterator().next();
|
142
|
if (part.getUri() != null) {
|
143
|
mediaUri = part.getUri();
|
144
|
}
|
145
|
}
|
146
|
}
|
147
|
return mediaUri;
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* @return the molecularData
|
152
|
*/
|
153
|
public List<MolecularData> getMolecularDataList() {
|
154
|
return molecularDataList;
|
155
|
}
|
156
|
|
157
|
public MolecularData addProviderLink(Link providerLink) {
|
158
|
MolecularData molecularData = new MolecularData(providerLink);
|
159
|
this.molecularDataList.add(molecularData);
|
160
|
return molecularData;
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* @return the specimenScans
|
165
|
*/
|
166
|
public List<Link> getSpecimenScans() {
|
167
|
return specimenScans;
|
168
|
}
|
169
|
|
170
|
public void addSpecimenScan(URI uri, String linkText){
|
171
|
specimenScans.add(new Link(uri, linkText));
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* @return the detailImages
|
176
|
*/
|
177
|
public List<Link> getDetailImages() {
|
178
|
return detailImages;
|
179
|
}
|
180
|
|
181
|
public void addDetailImage(URI uri, String motif){
|
182
|
detailImages.add(new Link(uri, motif));
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* @return the specimenScanUuids
|
187
|
*/
|
188
|
public List<UUID> getSpecimenScanUuids() {
|
189
|
return specimenScanUuids;
|
190
|
}
|
191
|
|
192
|
public void addSpecimenScanUuid(UUID uuid){
|
193
|
specimenScanUuids.add(uuid);
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* @return the detailImageUuids
|
198
|
*/
|
199
|
public List<UUID> getDetailImageUuids() {
|
200
|
return detailImageUuids;
|
201
|
}
|
202
|
|
203
|
public void addDetailImageUuid(UUID uuid){
|
204
|
detailImageUuids.add(uuid);
|
205
|
}
|
206
|
|
207
|
public class MolecularData implements Serializable{
|
208
|
|
209
|
private static final long serialVersionUID = -3509828381796659200L;
|
210
|
|
211
|
private final Link providerLink;
|
212
|
private List<ContigFile> contigFiles;
|
213
|
|
214
|
public MolecularData(Link providerLink) {
|
215
|
super();
|
216
|
this.providerLink = providerLink;
|
217
|
}
|
218
|
|
219
|
public ContigFile addContigFile(Link contigLink){
|
220
|
if(contigFiles==null){
|
221
|
contigFiles = new ArrayList<ContigFile>();
|
222
|
}
|
223
|
ContigFile contigFile = new ContigFile(contigLink);
|
224
|
contigFiles.add(contigFile);
|
225
|
return contigFile;
|
226
|
}
|
227
|
|
228
|
public synchronized Link getProviderLink() {
|
229
|
return providerLink;
|
230
|
}
|
231
|
|
232
|
public List<ContigFile> getContigFiles() {
|
233
|
return contigFiles;
|
234
|
}
|
235
|
|
236
|
}
|
237
|
|
238
|
public class ContigFile implements Serializable{
|
239
|
|
240
|
private final Link contigLink;
|
241
|
private List<Link> primerLinks;
|
242
|
|
243
|
public ContigFile(Link contigLink) {
|
244
|
this.contigLink = contigLink;
|
245
|
}
|
246
|
|
247
|
public void addPrimerLink(URI uri, String linkText){
|
248
|
if(primerLinks==null){
|
249
|
primerLinks = new ArrayList<Link>();
|
250
|
}
|
251
|
primerLinks.add(new Link(uri, linkText));
|
252
|
}
|
253
|
|
254
|
public Link getContigLink() {
|
255
|
return contigLink;
|
256
|
}
|
257
|
|
258
|
public List<Link> getPrimerLinks() {
|
259
|
return primerLinks;
|
260
|
}
|
261
|
|
262
|
}
|
263
|
|
264
|
public static class Link implements Serializable{
|
265
|
|
266
|
private static final long serialVersionUID = 6635385359662624579L;
|
267
|
|
268
|
private String linkText;
|
269
|
private URI uri;
|
270
|
|
271
|
public Link(URI uri, String linkText) {
|
272
|
super();
|
273
|
this.linkText = linkText;
|
274
|
this.uri = uri;
|
275
|
}
|
276
|
/**
|
277
|
* @return the linkText
|
278
|
*/
|
279
|
public synchronized String getLinkText() {
|
280
|
return linkText;
|
281
|
}
|
282
|
/**
|
283
|
* @param linkText the linkText to set
|
284
|
*/
|
285
|
public synchronized void setLinkText(String linkText) {
|
286
|
this.linkText = linkText;
|
287
|
}
|
288
|
/**
|
289
|
* @return the uri
|
290
|
*/
|
291
|
public synchronized URI getUri() {
|
292
|
return uri;
|
293
|
}
|
294
|
/**
|
295
|
* @param uri the uri to set
|
296
|
*/
|
297
|
public synchronized void setUri(URI uri) {
|
298
|
this.uri = uri;
|
299
|
}
|
300
|
|
301
|
@Override
|
302
|
public int hashCode() {
|
303
|
final int prime = 31;
|
304
|
int result = 1;
|
305
|
result = prime * result + ((linkText == null) ? 0 : linkText.hashCode());
|
306
|
result = prime * result + ((uri == null) ? 0 : uri.hashCode());
|
307
|
return result;
|
308
|
}
|
309
|
|
310
|
@Override
|
311
|
public boolean equals(Object obj) {
|
312
|
if (this == obj) {
|
313
|
return true;
|
314
|
}
|
315
|
if (obj == null) {
|
316
|
return false;
|
317
|
}
|
318
|
if (getClass() != obj.getClass()) {
|
319
|
return false;
|
320
|
}
|
321
|
Link other = (Link) obj;
|
322
|
if (linkText == null) {
|
323
|
if (other.linkText != null) {
|
324
|
return false;
|
325
|
}
|
326
|
} else if (!linkText.equals(other.linkText)) {
|
327
|
return false;
|
328
|
}
|
329
|
if (uri == null) {
|
330
|
if (other.uri != null) {
|
331
|
return false;
|
332
|
}
|
333
|
} else if (!uri.equals(other.uri)) {
|
334
|
return false;
|
335
|
}
|
336
|
return true;
|
337
|
}
|
338
|
}
|
339
|
|
340
|
|
341
|
}
|