1 /* 2 This file is part of BioD. 3 Copyright (C) 2013 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.maf.reader; 25 26 import bio.maf.block; 27 import bio.maf.parser; 28 29 import std.array; 30 import std.string; 31 import std.stdio; 32 import std.algorithm; 33 34 /// 35 struct MafBlockRange { 36 private { 37 38 alias File.ByLine!(char, char) LineRange; 39 File _f; 40 LineRange _lines; 41 42 bool _empty; 43 MafBlock _front; 44 45 void skipHeader() { 46 if (!_lines.empty && _lines.front.startsWith("##maf")) 47 _lines.popFront(); 48 } 49 } 50 51 this(string fn) { 52 _f = File(fn); 53 _lines = _f.byLine(KeepTerminator.yes); 54 skipHeader(); 55 popFront(); 56 } 57 58 /// 59 bool empty() @property const { 60 return _empty; 61 } 62 63 /// 64 MafBlock front() @property { 65 return _front; 66 } 67 68 /// 69 void popFront() { 70 auto block_data = Appender!(char[])(); 71 while (!_lines.empty && !_lines.front.chomp().empty) { 72 block_data.put(_lines.front.dup); 73 _lines.popFront(); 74 } 75 if (block_data.data.empty) { 76 _empty = true; 77 } else { 78 _front = parseMafBlock(cast(string)(block_data.data)); 79 if (!_lines.empty) 80 _lines.popFront(); 81 } 82 } 83 } 84 85 86 /// 87 class MafReader { 88 89 private string _fn; 90 91 /// 92 this(string filename) { 93 _fn = filename; 94 } 95 96 /// 97 string filename() @property const { 98 return _fn; 99 } 100 101 /// 102 MafBlockRange blocks() @property { 103 return MafBlockRange(_fn); 104 } 105 }