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 module bio.core.sequence; 25 26 import bio.core.base; 27 28 import std.algorithm; 29 import std.range; 30 import std.conv; 31 import std.traits; 32 public import std.array; 33 34 /// Identity function 35 T identity(T)(auto ref T t) { return t; } 36 37 /// Range that allows to unify operations in forward and reverse directions 38 /// without virtual function call overhead introduced by $(D inputRangeObject). 39 /// 40 /// $(D reverseTransform) is a function that will be applied to elements 41 /// if range is iterated backwards. 42 struct ReversableRange(alias reverseTransform=identity, R) 43 if(isBidirectionalRange!R) 44 { 45 private 46 { 47 bool _rev = void; 48 R _range = void; 49 } 50 51 /// Construct reversable range. 52 /// 53 /// Params: 54 /// range = bidirectional range 55 /// reverse = if true, all operations on the range will be as if 56 /// $(D retro(range)) was used instead of $(D range). 57 this(R range, bool reverse=false) 58 { 59 _rev = reverse; 60 _range = range; 61 } 62 63 /// Bidirectional range primitives 64 bool empty() @property 65 { 66 return _range.empty; 67 } 68 69 /// ditto 70 auto front() @property 71 { 72 return _rev ? reverseTransform(_range.back) : _range.front; 73 } 74 75 /// ditto 76 auto back() @property 77 { 78 return _rev ? reverseTransform(_range.front) : _range.back; 79 } 80 81 /// ditto 82 void popFront() 83 { 84 if (_rev) 85 _range.popBack(); 86 else 87 _range.popFront(); 88 } 89 90 /// ditto 91 void popBack() 92 { 93 if (_rev) 94 _range.popFront(); 95 else 96 _range.popBack(); 97 } 98 99 /// ditto 100 auto save() @property 101 { 102 return ReversableRange(_range.save, _rev); 103 } 104 105 /// Reverse of this range 106 ReversableRange reverse() @property { 107 return ReversableRange(_range.save, !_rev); 108 } 109 110 static if(hasLength!R) 111 { 112 /// If source range has length, the result also has length 113 size_t length() @property 114 { 115 return _range.length; 116 } 117 } 118 119 static if(isRandomAccessRange!R) 120 { 121 /// If source range is a random access range, $(D opIndex) is defined 122 auto opIndex(size_t index) 123 { 124 if (_rev) 125 return reverseTransform(_range[_range.length - 1 - index]); 126 else 127 return _range[index]; 128 } 129 } 130 131 static if(hasSlicing!R) 132 { 133 /// Slicing is also propagated 134 auto opSlice(size_t from, size_t to) 135 { 136 if (_rev) 137 { 138 auto len = _range.length; 139 // 140 // [b, e) -> (len - 1 - e, len - 1 - b] ~ [len - e, len - b) 141 // 142 return ReversableRange(_range[len - to .. len - from], true); 143 } 144 else 145 return ReversableRange(_range[from .. to], false); 146 } 147 } 148 } 149 150 /// Create reversable range from bidirectional one. 151 ReversableRange!(reverseTransform, R) 152 reversableRange(alias reverseTransform=identity, R)(R range, bool reverse=false) 153 { 154 return typeof(return)(range, reverse); 155 } 156 157 unittest { 158 159 auto bidir_range = [1, 2, 3, 4, 5]; 160 auto rev = 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 because 173 // 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 template Sequence(R) 182 { 183 alias ReversableRange!(complementBase, R) Sequence; 184 } 185 186 /// Returns an object very similar to string, but sliceable. 187 /// Tricks std.traits.isNarrowString. 188 auto sliceableString(string s) { 189 return map!"cast(char)a"(cast(ubyte[])s); 190 } 191 192 /// 193 alias ReturnType!sliceableString SliceableString; 194 195 /// Create nucleotide sequence from bidirectional base range. 196 auto nucleotideSequence(R)(R bases, bool reverse=false) 197 if(isBidirectionalRange!R) 198 { 199 200 static if(isNarrowString!R) 201 { 202 return nucleotideSequence(sliceableString(bases), reverse); 203 } 204 else static if(is(Unqual!(ElementType!R) == char) || 205 is(Unqual!(ElementType!R) == dchar)) 206 { 207 return nucleotideSequence(map!(charToBase!Base16)(bases), reverse); 208 } 209 else 210 { 211 return Sequence!R(bases, reverse); 212 } 213 } 214 215 /// 216 alias ReturnType!(nucleotideSequence!SliceableString) NucleotideSequence; 217 218 unittest { 219 auto seq0 = nucleotideSequence("ACGTACGT"); 220 221 // reverse-complement 222 assert(equal(seq0.reverse[2 .. 6], "GTAC")); 223 224 auto seq1 = 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 }