Project

General

Profile

Download (7.41 KB) Statistics
| Branch: | Tag: | Revision:
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
        result.getObjectContent(id).add(new CharacterSetIntervalEvent(0, getColumnCount(null)));  //TODO Change this expression, if column count should return -1 in the future.
72
        return result;
73
    }
74

    
75

    
76
    /**
77
     * @return the sequence
78
     */
79
    public Sequence getCDMSequence() {
80
        return sequence;
81
    }
82

    
83

    
84
    private int extractSingleReadIndexFromID(String sequenceID) {
85
        if (sequenceID.startsWith(SINGLE_READ_SEQUENCE_ID_PREFIX)) {
86
            try {
87
                return Integer.parseInt(sequenceID.substring(SINGLE_READ_SEQUENCE_ID_PREFIX.length()));
88
            }
89
            catch (NumberFormatException e) {}  // fall through
90
        }
91
        return -1;
92
    }
93

    
94

    
95
    @Override
96
    public LinkedLabeledIDEvent getStartEvent(ReadWriteParameterMap parameters) {
97
        return new LinkedLabeledIDEvent(EventContentType.ALIGNMENT, MATRIX_ID, "Contig alignment", null);
98
                //TODO Use label according to derivate and markers.
99
    }
100

    
101

    
102
    @Override
103
    public boolean containsLongTokens(ReadWriteParameterMap parameters) {
104
        return false;
105
    }
106

    
107

    
108
    @Override
109
    public long getColumnCount(ReadWriteParameterMap parameters) {
110
        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.
111
    }
112

    
113

    
114
    @Override
115
    public long getSequenceCount(ReadWriteParameterMap parameters) {
116
        return /*sequence.getSingleReadAlignments().size() +*/ 1;
117
    }
118

    
119

    
120
    @Override
121
    public Iterator<String> getSequenceIDIterator(ReadWriteParameterMap parameters) {
122
        return new SequenceIDIterator(getCDMSequence().getSingleReadAlignments().size());
123
    }
124

    
125

    
126
    @Override
127
    public long getSequenceLength(ReadWriteParameterMap parameters, String sequenceID) throws IllegalArgumentException {
128
        int singleReadIndex = extractSingleReadIndexFromID(sequenceID);
129
        if (singleReadIndex >= 0) {
130
            //TODO Determine single read length. (Current problem: Reads are not ordered in the set of the Sequence object.)
131
            return 0;
132
        }
133
        else if (CONSENSUS_SEQUENCE_ID.equals(sequenceID)) {
134
            return getCDMSequence().getSequenceString().length();
135
        }
136
        else {
137
            throw new IllegalArgumentException("No sequence with the ID \"" + sequenceID + "\" could be found.");
138
        }
139
    }
140

    
141

    
142
    @Override
143
    public LinkedLabeledIDEvent getSequenceStartEvent(ReadWriteParameterMap parameters, String sequenceID) {
144
        int singleReadIndex = extractSingleReadIndexFromID(sequenceID);
145
        if (singleReadIndex >= 0) {
146
            return new LinkedLabeledIDEvent(EventContentType.SEQUENCE, sequenceID, "Single read " + singleReadIndex, null);
147
                    //TODO Use name displayed in derivate hierarchy or specified name as label instead?
148
        }
149
        else if (CONSENSUS_SEQUENCE_ID.equals(sequenceID)) {
150
            return new LinkedLabeledIDEvent(EventContentType.SEQUENCE, sequenceID, consensusSequenceLabel, null);
151
        }
152
        else {
153
            throw new IllegalArgumentException("No sequence with the ID \"" + sequenceID + "\" could be found.");
154
        }
155
    }
156

    
157

    
158
    @Override
159
    public void writeSequencePartContentData(ReadWriteParameterMap parameters, JPhyloIOEventReceiver receiver, String sequenceID, long startColumn,
160
            long endColumn) throws IOException, IllegalArgumentException {
161

    
162
        int singleReadIndex = extractSingleReadIndexFromID(sequenceID);
163
        if (singleReadIndex >= 0) {
164
            //TODO Implement single read output.
165
        }
166
        else if (CONSENSUS_SEQUENCE_ID.equals(sequenceID)) {
167
            if (startColumn == 0) {
168
                //TODO Add consensus sequence metadata.
169
                //TODO Possibly export additional properties of sequence (e.g. isBarcode(), getDdbjId(), ...) as metadata.
170
            }
171
            receiver.add(new SequenceTokensEvent(StringUtils.charSequenceToStringList(
172
                    getCDMSequence().getSequenceString().substring((int)startColumn, (int)endColumn))));
173
        }
174
        else {
175
            throw new IllegalArgumentException("No sequence with the ID \"" + sequenceID + "\" could be found.");
176
        }
177
    }
178

    
179

    
180
    /* (non-Javadoc)
181
     * @see info.bioinfweb.jphyloio.dataadapters.implementations.NoSetsMatrixDataAdapter#getTokenSets()
182
     */
183
    @Override
184
    public ObjectListDataAdapter<TokenSetDefinitionEvent> getTokenSets(ReadWriteParameterMap parameters) {
185
        return tokenSetList;
186
    }
187
}
(1-1/2)