Repository navigation
Expand file tree
/
Copy pathTwoBitSequenceReader.java
More file actions
152 lines (130 loc) · 4.21 KB
/
Copy pathTwoBitSequenceReader.java
File metadata and controls
152 lines (130 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* BioJava development code
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. If you do not have a copy,
* see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
*/
package org.biojava.nbio.core.sequence.storage;
import org.biojava.nbio.core.sequence.AccessionID;
import org.biojava.nbio.core.sequence.compound.NucleotideCompound;
import org.biojava.nbio.core.sequence.template.CompoundSet;
import org.biojava.nbio.core.sequence.template.Sequence;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Implementation of the 2bit encoding. This will default to the following
* encodings:
*
* <ul>
* <li>0 - T</li>
* <li>1 - C</li>
* <li>2 - A</li>
* <li>3 - G</li>
* </ul>
*
* We also do not support case sensitive encodings therefore if you pass a
* lowercased a this will be treated as if it is an uppercase A and we will
* erase that information.
*
* @author ayates
*/
public class TwoBitSequenceReader<C extends NucleotideCompound> extends BitSequenceReader<C> {
public TwoBitSequenceReader(Sequence<C> sequence) {
super(new TwoBitArrayWorker<C>(sequence), sequence.getAccession());
}
public TwoBitSequenceReader(String sequence, CompoundSet<C> compoundSet) {
this(sequence, compoundSet, new AccessionID("Unknown"));
}
public TwoBitSequenceReader(String sequence, CompoundSet<C> compoundSet, AccessionID accession) {
super(new TwoBitArrayWorker<C>(sequence, compoundSet), accession);
}
public TwoBitSequenceReader(TwoBitArrayWorker<C> worker) {
super(worker, new AccessionID("unknown"));
}
public TwoBitSequenceReader(TwoBitArrayWorker<C> worker, AccessionID accession) {
super(worker, accession);
}
/**
* Extension of the BitArrayWorker which provides the 2bit implementation
* code. This is intended to work with the 4 basic nucelotide types. If you
* require a different version of the encoding used here then extend
* and override as required.
*
* @param <C> Must extend NucleotideCompound
*/
public static class TwoBitArrayWorker<C extends NucleotideCompound> extends BitArrayWorker<C> {
public TwoBitArrayWorker(CompoundSet<C> compoundSet, int length) {
super(compoundSet, length);
}
public TwoBitArrayWorker(CompoundSet<C> compoundSet, int[] sequence) {
super(compoundSet, sequence);
}
public TwoBitArrayWorker(Sequence<C> sequence) {
super(sequence);
}
public TwoBitArrayWorker(String sequence, CompoundSet<C> compoundSet) {
super(sequence, compoundSet);
}
/**
* Masking value used for extracting the right most 2 bits from a byte
*/
private final static byte MASK = (byte) ((int) Math.pow(2, 0) | (int) Math.pow(2, 1));
@Override
protected byte bitMask() {
return MASK;
}
@Override
protected int compoundsPerDatatype() {
return 16;
}
/**
* Returns a Map which encodes TCAG into positions 0,1,2,3.
*/
@Override
@SuppressWarnings("serial")
protected Map<C, Integer> generateCompoundsToIndex() {
final CompoundSet<C> cs = getCompoundSet();
return new HashMap<C, Integer>() {
{
put(cs.getCompoundForString("T"), 0);
put(cs.getCompoundForString("C"), 1);
put(cs.getCompoundForString("A"), 2);
put(cs.getCompoundForString("G"), 3);
put(cs.getCompoundForString("t"), 0);
put(cs.getCompoundForString("c"), 1);
put(cs.getCompoundForString("a"), 2);
put(cs.getCompoundForString("g"), 3);
}
};
}
/**
* Returns a List which encodes TCAG into positions 0,1,2,3.
*/
@Override
protected List<C> generateIndexToCompounds() {
CompoundSet<C> cs = getCompoundSet();
List<C> result = new ArrayList<C>();
result.add( cs.getCompoundForString("T"));
result.add( cs.getCompoundForString("C"));
result.add( cs.getCompoundForString("A"));
result.add( cs.getCompoundForString("G"));
return result;
}
}
}