1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2016 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.taxeditor.molecular.io;
|
11
|
|
12
|
|
13
|
import info.bioinfweb.commons.bio.CharacterStateSetType;
|
14
|
import info.bioinfweb.commons.text.StringUtils;
|
15
|
import info.bioinfweb.jphyloio.ReadWriteConstants;
|
16
|
import info.bioinfweb.jphyloio.ReadWriteParameterMap;
|
17
|
import info.bioinfweb.jphyloio.dataadapters.JPhyloIOEventReceiver;
|
18
|
import info.bioinfweb.jphyloio.dataadapters.MatrixDataAdapter;
|
19
|
import info.bioinfweb.jphyloio.dataadapters.ObjectListDataAdapter;
|
20
|
import info.bioinfweb.jphyloio.dataadapters.implementations.NoCharDefsNoSetsMatrixDataAdapter;
|
21
|
import info.bioinfweb.jphyloio.dataadapters.implementations.store.StoreObjectListDataAdapter;
|
22
|
import info.bioinfweb.jphyloio.events.CharacterSetIntervalEvent;
|
23
|
import info.bioinfweb.jphyloio.events.LinkedLabeledIDEvent;
|
24
|
import info.bioinfweb.jphyloio.events.SequenceTokensEvent;
|
25
|
import info.bioinfweb.jphyloio.events.TokenSetDefinitionEvent;
|
26
|
import info.bioinfweb.jphyloio.events.type.EventContentType;
|
27
|
|
28
|
import java.io.IOException;
|
29
|
import java.util.Iterator;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
32
|
|
33
|
|
34
|
|
35
|
/**
|
36
|
* In implementation of {@link MatrixDataAdapter} that delegates to a {@link Sequence} object.
|
37
|
*
|
38
|
* @author Ben Stöver
|
39
|
* @date 29.04.2016
|
40
|
*/
|
41
|
public class CDMSequenceMatrixAdapter extends NoCharDefsNoSetsMatrixDataAdapter implements ReadWriteConstants {
|
42
|
public static final String MATRIX_ID = DEFAULT_MATRIX_ID_PREFIX + "ContigAlignment";
|
43
|
public static final String SINGLE_READ_SEQUENCE_ID_PREFIX = DEFAULT_SEQUENCE_ID_PREFIX + "SingleRead";
|
44
|
public static final String CONSENSUS_SEQUENCE_ID= DEFAULT_SEQUENCE_ID_PREFIX + "Consensus";
|
45
|
|
46
|
|
47
|
private Sequence sequence;
|
48
|
private String consensusSequenceLabel;
|
49
|
private ObjectListDataAdapter<TokenSetDefinitionEvent> tokenSetList;
|
50
|
//TODO Also allow specifying single read labels?
|
51
|
|
52
|
|
53
|
/**
|
54
|
* Creates a new instance of this class.
|
55
|
*
|
56
|
* @param sequence
|
57
|
* @param consensusSequenceLabel
|
58
|
*/
|
59
|
public CDMSequenceMatrixAdapter(Sequence sequence, String consensusSequenceLabel) {
|
60
|
super();
|
61
|
this.sequence = sequence;
|
62
|
this.consensusSequenceLabel = consensusSequenceLabel;
|
63
|
tokenSetList = createTokenSetList();
|
64
|
}
|
65
|
|
66
|
|
67
|
private ObjectListDataAdapter<TokenSetDefinitionEvent> createTokenSetList() {
|
68
|
StoreObjectListDataAdapter<TokenSetDefinitionEvent> result = new StoreObjectListDataAdapter<TokenSetDefinitionEvent>();
|
69
|
final String id = ReadWriteConstants.DEFAULT_TOKEN_SET_ID_PREFIX;
|
70
|
result.setObjectStartEvent(new TokenSetDefinitionEvent(CharacterStateSetType.DNA, id, null));
|
71
|
long length = getColumnCount(null); //TODO Change this expression, if column count should return -1 in the future.
|
72
|
if (length > 0) { // Empty character set interval events are not allowed. //TODO Handle -1 separately
|
73
|
result.getObjectContent(id).add(new CharacterSetIntervalEvent(0, length));
|
74
|
}
|
75
|
return result;
|
76
|
}
|
77
|
|
78
|
|
79
|
/**
|
80
|
* @return the sequence
|
81
|
*/
|
82
|
public Sequence getCDMSequence() {
|
83
|
return sequence;
|
84
|
}
|
85
|
|
86
|
|
87
|
private int extractSingleReadIndexFromID(String sequenceID) {
|
88
|
if (sequenceID.startsWith(SINGLE_READ_SEQUENCE_ID_PREFIX)) {
|
89
|
try {
|
90
|
return Integer.parseInt(sequenceID.substring(SINGLE_READ_SEQUENCE_ID_PREFIX.length()));
|
91
|
}
|
92
|
catch (NumberFormatException e) {} // fall through
|
93
|
}
|
94
|
return -1;
|
95
|
}
|
96
|
|
97
|
|
98
|
@Override
|
99
|
public LinkedLabeledIDEvent getStartEvent(ReadWriteParameterMap parameters) {
|
100
|
return new LinkedLabeledIDEvent(EventContentType.ALIGNMENT, MATRIX_ID, "Contig alignment", null);
|
101
|
//TODO Use label according to derivate and markers.
|
102
|
}
|
103
|
|
104
|
|
105
|
@Override
|
106
|
public boolean containsLongTokens(ReadWriteParameterMap parameters) {
|
107
|
return false;
|
108
|
}
|
109
|
|
110
|
|
111
|
@Override
|
112
|
public long getColumnCount(ReadWriteParameterMap parameters) {
|
113
|
return getCDMSequence().getSequenceString().length(); //TODO Consider that parts of single reads may lie outside of the current consensus sequence. Possibly return -1 in the future.
|
114
|
}
|
115
|
|
116
|
|
117
|
@Override
|
118
|
public long getSequenceCount(ReadWriteParameterMap parameters) {
|
119
|
return /*sequence.getSingleReadAlignments().size() +*/ 1;
|
120
|
}
|
121
|
|
122
|
|
123
|
@Override
|
124
|
public Iterator<String> getSequenceIDIterator(ReadWriteParameterMap parameters) {
|
125
|
return new SequenceIDIterator(getCDMSequence().getSingleReadAlignments().size());
|
126
|
}
|
127
|
|
128
|
|
129
|
@Override
|
130
|
public long getSequenceLength(ReadWriteParameterMap parameters, String sequenceID) throws IllegalArgumentException {
|
131
|
int singleReadIndex = extractSingleReadIndexFromID(sequenceID);
|
132
|
if (singleReadIndex >= 0) {
|
133
|
//TODO Determine single read length. (Current problem: Reads are not ordered in the set of the Sequence object.)
|
134
|
return 0;
|
135
|
}
|
136
|
else if (CONSENSUS_SEQUENCE_ID.equals(sequenceID)) {
|
137
|
return getCDMSequence().getSequenceString().length();
|
138
|
}
|
139
|
else {
|
140
|
throw new IllegalArgumentException("No sequence with the ID \"" + sequenceID + "\" could be found.");
|
141
|
}
|
142
|
}
|
143
|
|
144
|
|
145
|
@Override
|
146
|
public LinkedLabeledIDEvent getSequenceStartEvent(ReadWriteParameterMap parameters, String sequenceID) {
|
147
|
int singleReadIndex = extractSingleReadIndexFromID(sequenceID);
|
148
|
if (singleReadIndex >= 0) {
|
149
|
return new LinkedLabeledIDEvent(EventContentType.SEQUENCE, sequenceID, "Single read " + singleReadIndex, null);
|
150
|
//TODO Use name displayed in derivate hierarchy or specified name as label instead?
|
151
|
}
|
152
|
else if (CONSENSUS_SEQUENCE_ID.equals(sequenceID)) {
|
153
|
return new LinkedLabeledIDEvent(EventContentType.SEQUENCE, sequenceID, consensusSequenceLabel, null);
|
154
|
}
|
155
|
else {
|
156
|
throw new IllegalArgumentException("No sequence with the ID \"" + sequenceID + "\" could be found.");
|
157
|
}
|
158
|
}
|
159
|
|
160
|
|
161
|
@Override
|
162
|
public void writeSequencePartContentData(ReadWriteParameterMap parameters, JPhyloIOEventReceiver receiver, String sequenceID, long startColumn,
|
163
|
long endColumn) throws IOException, IllegalArgumentException {
|
164
|
|
165
|
int singleReadIndex = extractSingleReadIndexFromID(sequenceID);
|
166
|
if (singleReadIndex >= 0) {
|
167
|
//TODO Implement single read output.
|
168
|
}
|
169
|
else if (CONSENSUS_SEQUENCE_ID.equals(sequenceID)) {
|
170
|
if (startColumn == 0) {
|
171
|
//TODO Add consensus sequence metadata.
|
172
|
//TODO Possibly export additional properties of sequence (e.g. isBarcode(), getDdbjId(), ...) as metadata.
|
173
|
}
|
174
|
receiver.add(new SequenceTokensEvent(StringUtils.charSequenceToStringList(
|
175
|
getCDMSequence().getSequenceString().substring((int)startColumn, (int)endColumn))));
|
176
|
}
|
177
|
else {
|
178
|
throw new IllegalArgumentException("No sequence with the ID \"" + sequenceID + "\" could be found.");
|
179
|
}
|
180
|
}
|
181
|
|
182
|
|
183
|
/* (non-Javadoc)
|
184
|
* @see info.bioinfweb.jphyloio.dataadapters.implementations.NoSetsMatrixDataAdapter#getTokenSets()
|
185
|
*/
|
186
|
@Override
|
187
|
public ObjectListDataAdapter<TokenSetDefinitionEvent> getTokenSets(ReadWriteParameterMap parameters) {
|
188
|
return tokenSetList;
|
189
|
}
|
190
|
}
|