1 /*
2     This file is part of BioD.
3     Copyright (C) 2013    Artem Tarasov <lomereiter@gmail.com>
4     Copyright (C) 2018    Pjotr Prins <pjotr.prins@thebird.nl>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a
7     copy of this software and associated documentation files (the "Software"),
8     to deal in the Software without restriction, including without limitation
9     the rights to use, copy, modify, merge, publish, distribute, sublicense,
10     and/or sell copies of the Software, and to permit persons to whom the
11     Software is furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22     DEALINGS IN THE SOFTWARE.
23 
24 */
25 module bio.std.maf.reader;
26 
27 import bio.std.maf.block;
28 import bio.std.maf.parser;
29 
30 import std.array;
31 import std..string;
32 import std.stdio;
33 import std.algorithm;
34 import std.range.interfaces;
35 import std..string;
36 
37 ///
38 struct MafBlockRange {
39     private {
40 
41         File _f;
42         bool _empty;
43         MafBlock _front;
44 
45         void skipHeader() {
46           auto _lines = _f.byLine();
47           if (!_lines.empty)
48             if (_lines.front.startsWith("##maf"))
49                 _lines.popFront();
50         }
51     }
52 
53     this(string fn) {
54         _f = File(fn);
55         // _lines = _f.byLine(KeepTerminator.yes);
56         skipHeader();
57         popFront();
58     }
59 
60     ///
61     bool empty() @property const {
62         return _empty;
63     }
64 
65     ///
66     MafBlock front() @property {
67         return _front;
68     }
69 
70     ///
71     void popFront() {
72         auto block_data = Appender!(char[])();
73         auto _lines = _f.byLine();
74         while (!_lines.empty && !_lines.front.chomp().empty) {
75             block_data.put(_lines.front.dup);
76             _lines.popFront();
77         }
78         if (block_data.data.empty) {
79             _empty = true;
80         } else {
81             _front = parseMafBlock(cast(string)(block_data.data));
82             if (!_lines.empty)
83                 _lines.popFront();
84         }
85     }
86 }
87 
88 
89 ///
90 class MafReader {
91 
92     private string _fn;
93 
94     ///
95     this(string filename) {
96         _fn = filename;
97     }
98 
99     ///
100     string filename() @property const {
101         return _fn;
102     }
103 
104     ///
105     MafBlockRange blocks() @property {
106         return MafBlockRange(_fn);
107     }
108 }