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.std.hts.iontorrent.flowindex; 25 26 import std.range; 27 28 /// 29 struct FlowIndex(S) 30 { 31 private { 32 S _seq; 33 string _fo; 34 size_t _index; 35 } 36 37 this(S sequence, string flow_order) { 38 _seq = sequence; 39 _fo = flow_order; 40 41 if (!_seq.empty) { 42 while (_index < _fo.length) { 43 if (_fo[_index] == _seq.front) 44 break; 45 46 ++_index; 47 } 48 } 49 } 50 51 /// 52 bool empty() @property { 53 return _seq.empty || (_index == _fo.length); 54 } 55 56 /// Current flow index 57 size_t front() @property const { 58 return _index; 59 } 60 61 /// Move to next read base 62 void popFront() { 63 auto prev_base = _seq.front; 64 _seq.popFront(); 65 if (_seq.empty) return; 66 67 if (_seq.front == prev_base) { 68 return; // keep index as is 69 } 70 71 _index += 1; 72 while (_index < _fo.length) { 73 if (_fo[_index] == _seq.front) 74 break; 75 76 _index++; 77 } 78 } 79 } 80 81 /// Given a sequence of bases and flow order, recover flow index, 82 /// i.e. sequence of 0-based flow positions for each base. 83 auto flowIndex(S)(S sequence, string flow_order) 84 { 85 return FlowIndex!S(sequence, flow_order); 86 } 87 88 unittest { 89 import bio.core.base; 90 91 import std.conv; 92 import std.algorithm; 93 94 auto seq = map!(c => Base5(c))("AACGTAAACCTCACT"); 95 string flow_order = "ATGCATGCATGCATGCATGCATGCATGC"; 96 // 0123456789111111111122222222 97 // 012345678901234567 98 assert(equal(flowIndex(seq, flow_order), [0, 0, 3, 6, 9, 12, 12, 12, 15, 15, 17, 19, 20, 23, 25])); 99 }