1 /*
2 This file is part of BioD.
3 Copyright (C) 2012 Artem Tarasov <lomereiter@gmail.com>
4 5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11 12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.
22 23 */24 modulebio.core.sequence;
25 26 importbio.core.base;
27 28 importstd.algorithm;
29 importstd.range;
30 importstd.conv;
31 importstd.traits;
32 publicimportstd.array;
33 34 /// Identity function35 Tidentity(T)(autorefTt) { returnt; }
36 37 /// Range that allows to unify operations in forward and reverse directions38 /// without virtual function call overhead introduced by $(D inputRangeObject).39 ///40 /// $(D reverseTransform) is a function that will be applied to elements41 /// if range is iterated backwards.42 structReversableRange(aliasreverseTransform=identity, R)
43 if(isBidirectionalRange!R)
44 {
45 private46 {
47 bool_rev = void;
48 R_range = void;
49 }
50 51 /// Construct reversable range.52 ///53 /// Params:54 /// range = bidirectional range55 /// reverse = if true, all operations on the range will be as if56 /// $(D retro(range)) was used instead of $(D range).57 this(Rrange, boolreverse=false)
58 {
59 _rev = reverse;
60 _range = range;
61 }
62 63 /// Bidirectional range primitives64 boolempty() @property65 {
66 return_range.empty;
67 }
68 69 /// ditto70 autofront() @property71 {
72 return_rev ? reverseTransform(_range.back) : _range.front;
73 }
74 75 /// ditto76 autoback() @property77 {
78 return_rev ? reverseTransform(_range.front) : _range.back;
79 }
80 81 /// ditto82 voidpopFront()
83 {
84 if (_rev)
85 _range.popBack();
86 else87 _range.popFront();
88 }
89 90 /// ditto91 voidpopBack()
92 {
93 if (_rev)
94 _range.popFront();
95 else96 _range.popBack();
97 }
98 99 /// ditto100 autosave() @property101 {
102 returnReversableRange(_range.save, _rev);
103 }
104 105 /// Reverse of this range106 ReversableRangereverse() @property {
107 returnReversableRange(_range.save, !_rev);
108 }
109 110 staticif(hasLength!R)
111 {
112 /// If source range has length, the result also has length113 size_tlength() @property114 {
115 return_range.length;
116 }
117 }
118 119 staticif(isRandomAccessRange!R)
120 {
121 /// If source range is a random access range, $(D opIndex) is defined122 autoopIndex(size_tindex)
123 {
124 if (_rev)
125 returnreverseTransform(_range[_range.length - 1 - index]);
126 else127 return_range[index];
128 }
129 }
130 131 staticif(hasSlicing!R)
132 {
133 /// Slicing is also propagated134 autoopSlice(size_tfrom, size_tto)
135 {
136 if (_rev)
137 {
138 autolen = _range.length;
139 //140 // [b, e) -> (len - 1 - e, len - 1 - b] ~ [len - e, len - b)141 //142 returnReversableRange(_range[len - to .. len - from], true);
143 }
144 else145 returnReversableRange(_range[from .. to], false);
146 }
147 }
148 }
149 150 /// Create reversable range from bidirectional one.151 ReversableRange!(reverseTransform, R)
152 reversableRange(aliasreverseTransform=identity, R)(Rrange, boolreverse=false)
153 {
154 returntypeof(return)(range, reverse);
155 }
156 157 unittest {
158 159 autobidir_range = [1, 2, 3, 4, 5];
160 autorev = reversableRange(bidir_range[], true);
161 162 assert(rev.front == 5);
163 assert(rev[2] == 3);
164 rev.popFront();
165 assert(rev.back == 1);
166 assert(rev.front == 4);
167 assert(equal(rev[1 .. 3], [3, 2]));
168 169 // Here. That's the whole point.170 // One can't do the same with $(D retro)171 // without using $(D inputRangeObject),172 // but that kills performance because173 // virtual calls can not be inlined.174 rev = reversableRange(bidir_range[], false);
175 176 assert(rev.front == 1);
177 assert(equal(rev[1 .. 3], [2, 3]));
178 }
179 180 /// Sequence of bases. Element of reversed range will be complemented.181 templateSequence(R)
182 {
183 aliasReversableRange!(complementBase, R) Sequence;
184 }
185 186 /// Returns an object very similar to string, but sliceable.187 /// Tricks std.traits.isNarrowString.188 autosliceableString(strings) {
189 returnmap!"cast(char)a"(cast(ubyte[])s);
190 }
191 192 ///193 aliasReturnType!sliceableStringSliceableString;
194 195 /// Create nucleotide sequence from bidirectional base range.196 autonucleotideSequence(R)(Rbases, boolreverse=false)
197 if(isBidirectionalRange!R)
198 {
199 200 staticif(isNarrowString!R)
201 {
202 returnnucleotideSequence(sliceableString(bases), reverse);
203 }
204 elsestaticif(is(Unqual!(ElementType!R) == char) ||
205 is(Unqual!(ElementType!R) == dchar))
206 {
207 returnnucleotideSequence(map!(charToBase!Base16)(bases), reverse);
208 }
209 else210 {
211 returnSequence!R(bases, reverse);
212 }
213 }
214 215 ///216 aliasReturnType!(nucleotideSequence!SliceableString) NucleotideSequence;
217 218 unittest {
219 autoseq0 = nucleotideSequence("ACGTACGT");
220 221 // reverse-complement222 assert(equal(seq0.reverse[2 .. 6], "GTAC"));
223 224 autoseq1 = nucleotideSequence(seq0, true);
225 assert(equal(seq1[1 .. 5], "CGTA"));
226 assert(equal(seq1, map!complementBase(retro(seq0))));
227 228 seq1 = nucleotideSequence(seq0, false);
229 assert(equal(seq1, seq0));
230 }