Project

General

Profile

Download (5.64 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.text.StringUtils;
14
import info.bioinfweb.jphyloio.ReadWriteConstants;
15
import info.bioinfweb.jphyloio.dataadapters.JPhyloIOEventReceiver;
16
import info.bioinfweb.jphyloio.dataadapters.MatrixDataAdapter;
17
import info.bioinfweb.jphyloio.dataadapters.implementations.NoSetsMatrixDataAdapter;
18
import info.bioinfweb.jphyloio.events.LinkedLabeledIDEvent;
19
import info.bioinfweb.jphyloio.events.SequenceTokensEvent;
20
import info.bioinfweb.jphyloio.events.type.EventContentType;
21

    
22
import java.io.IOException;
23
import java.util.Iterator;
24

    
25
import eu.etaxonomy.cdm.model.molecular.Sequence;
26

    
27

    
28

    
29
/**
30
 * In implementation of {@link MatrixDataAdapter} that delegates to a {@link Sequence} object.
31
 *
32
 * @author Ben Stöver
33
 * @date 29.04.2016
34
 */
35
public class CDMSequenceMatrixAdapter extends NoSetsMatrixDataAdapter implements ReadWriteConstants {
36
    public static final String MATRIX_ID = DEFAULT_MATRIX_ID_PREFIX + "ContigAlignment";
37
    public static final String SINGLE_READ_SEQUENCE_ID_PREFIX = DEFAULT_SEQUENCE_ID_PREFIX + "SingleRead";
38
    public static final String CONSENSUS_SEQUENCE_ID= DEFAULT_SEQUENCE_ID_PREFIX + "Consensus";
39

    
40

    
41
    private Sequence sequence;
42
    private String consensusSequenceLabel;
43
    //TODO Also allow specifying single read labels?
44

    
45

    
46
    /**
47
     * Creates a new instance of this class.
48
     *
49
     * @param sequence
50
     * @param consensusSequenceLabel
51
     */
52
    public CDMSequenceMatrixAdapter(Sequence sequence, String consensusSequenceLabel) {
53
        super();
54
        this.sequence = sequence;
55
        this.consensusSequenceLabel = consensusSequenceLabel;
56
    }
57

    
58

    
59
    /**
60
     * @return the sequence
61
     */
62
    public Sequence getCDMSequence() {
63
        return sequence;
64
    }
65

    
66

    
67
    private int extractSingleReadIndexFromID(String sequenceID) {
68
        try {
69
            return Integer.parseInt(sequenceID.substring(SINGLE_READ_SEQUENCE_ID_PREFIX.length()));
70
        }
71
        catch (NumberFormatException e) {
72
            return -1;
73
        }
74
    }
75

    
76

    
77
    @Override
78
    public LinkedLabeledIDEvent getStartEvent() {
79
        return new LinkedLabeledIDEvent(EventContentType.ALIGNMENT, MATRIX_ID, "Contig alignment", null);
80
                //TODO Use label according to derivate and markers.
81
    }
82

    
83

    
84
    @Override
85
    public boolean containsLongTokens() {
86
        return false;
87
    }
88

    
89

    
90
    @Override
91
    public long getColumnCount() {
92
        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.
93
    }
94

    
95

    
96
    @Override
97
    public long getSequenceCount() {
98
        return /*sequence.getSingleReadAlignments().size() +*/ 1;
99
    }
100

    
101

    
102
    @Override
103
    public Iterator<String> getSequenceIDIterator() {
104
        return new SequenceIDIterator(getCDMSequence().getSingleReadAlignments().size());
105
    }
106

    
107

    
108
    @Override
109
    public long getSequenceLength(String sequenceID) throws IllegalArgumentException {
110
        int singleReadIndex = extractSingleReadIndexFromID(sequenceID);
111
        if (singleReadIndex >= 0) {
112
            //TODO Determine single read length. (Current problem: Reads are not ordered in the set of the Sequence object.)
113
            return 0;
114
        }
115
        else if (CONSENSUS_SEQUENCE_ID.equals(sequenceID)) {
116
            return getCDMSequence().getSequenceString().length();
117
        }
118
        else {
119
            throw new IllegalArgumentException("No sequence with the ID \"" + sequenceID + "\" could be found.");
120
        }
121
    }
122

    
123

    
124
    @Override
125
    public LinkedLabeledIDEvent getSequenceStartEvent(String sequenceID) {
126
        int singleReadIndex = extractSingleReadIndexFromID(sequenceID);
127
        if (singleReadIndex >= 0) {
128
            return new LinkedLabeledIDEvent(EventContentType.SEQUENCE, sequenceID, "singleRead" + singleReadIndex, null);
129
                    //TODO Use name displayed in derivate hierarchy or specified name as label instead?
130
        }
131
        else if (CONSENSUS_SEQUENCE_ID.equals(sequenceID)) {
132
            return new LinkedLabeledIDEvent(EventContentType.SEQUENCE, sequenceID, consensusSequenceLabel, null);
133
        }
134
        else {
135
            throw new IllegalArgumentException("No sequence with the ID \"" + sequenceID + "\" could be found.");
136
        }
137
    }
138

    
139

    
140
    @Override
141
    public void writeSequencePartContentData(JPhyloIOEventReceiver receiver, String sequenceID, long startColumn,
142
            long endColumn) throws IOException, IllegalArgumentException {
143

    
144
        int singleReadIndex = extractSingleReadIndexFromID(sequenceID);
145
        if (singleReadIndex >= 0) {
146
            //TODO Implement single read output.
147
        }
148
        else if (CONSENSUS_SEQUENCE_ID.equals(sequenceID)) {
149
            if (startColumn == 0) {
150
                //TODO Add consensus sequence metadata.
151
                //TODO Possibly export additional properties of sequence (e.g. isBarcode(), getDdbjId(), ...) as metadata.
152
            }
153
            receiver.add(new SequenceTokensEvent(StringUtils.charSequenceToStringList(
154
                    getCDMSequence().getSequenceString().substring((int)startColumn, (int)endColumn))));
155
        }
156
        else {
157
            throw new IllegalArgumentException("No sequence with the ID \"" + sequenceID + "\" could be found.");
158
        }
159
    }
160
}
(1-1/3)