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.sff.reader; 25 26 public import bio.std.sff.index; 27 import bio.std.sff.read; 28 import bio.std.sff.readrange; 29 import bio.std.sff.constants; 30 31 import bio.core.utils.stream; 32 import contrib.undead.stream; 33 import std.system; 34 import std.range; 35 import std.exception; 36 37 /// SFF file reader 38 class SffReader { 39 40 /// Open file by filename 41 this(string filename) { 42 _filename = filename; 43 44 _readHeader(); 45 } 46 47 /// Reads 48 auto reads() @property { 49 auto stream = new bio.core.utils.stream.File(filename); 50 Stream sff = new EndianStream(stream, Endian.bigEndian); 51 52 sff.seekSet(_header_length); 53 auto sff_reads = SffReadRange(sff, cast(ushort)_flow_chars.length, _index_location); 54 return takeExactly(sff_reads, _n_reads); 55 } 56 57 /// 58 SffRead getReadAtOffset(size_t offset) { 59 auto stream = new bio.core.utils.stream.File(filename); 60 Stream sff = new EndianStream(stream, Endian.bigEndian); 61 62 sff.seekSet(offset); 63 auto read = SffReadRange(sff, cast(ushort)_flow_chars.length, _index_location).front; 64 sff.close(); 65 return read; 66 } 67 68 /// File name 69 string filename() @property const { 70 return _filename; 71 } 72 73 /// Location of the index (if included). 74 IndexLocation index_location() @property const { 75 return _index_location; 76 } 77 78 /// 79 bool has_index() @property const { 80 return _index_location.offset != 0 && _index_location.length != 0; 81 } 82 83 /// Set index location (saves new index location to the file) 84 void index_location(IndexLocation location) @property { 85 _index_location = location; 86 87 // offset spans 8 bytes (8 .. 16), 88 // length spans 4 bytes (16 .. 20) 89 90 auto stream = new bio.core.utils.stream.File(filename, "r+"); 91 stream.seekSet(8); 92 auto endian_stream = new EndianStream(stream, Endian.bigEndian); 93 endian_stream.write(location.offset); 94 endian_stream.write(location.length); 95 endian_stream.close(); 96 } 97 98 /// Nucleotides used for each flow of each read 99 string flow_order() @property const { 100 return _flow_chars; 101 } 102 103 /// Nucleotide bases of the key sequence used for each read 104 string key_sequence() @property const { 105 return _key_sequence; 106 } 107 108 private { 109 string _filename; 110 111 uint _magic_number; 112 char[4] _version; 113 114 uint _n_reads; 115 ushort _header_length; 116 117 string _flow_chars; 118 string _key_sequence; 119 120 IndexLocation _index_location; 121 122 void _readHeader() { 123 auto stream = new bio.core.utils.stream.File(_filename); 124 auto sff = new EndianStream(stream, Endian.bigEndian); 125 126 sff.read(_magic_number); 127 enforce(_magic_number == SFF_MAGIC, "Wrong magic number, expected 0x2E736666"); 128 129 sff.readExact(_version.ptr, 4); 130 enforce(_version == SFF_VERSION, "Unsupported version, expected 1"); 131 132 sff.read(_index_location.offset); 133 sff.read(_index_location.length); 134 135 sff.read(_n_reads); 136 sff.read(_header_length); 137 138 ushort _key_length; 139 ushort _number_of_flows; 140 ubyte _flowgram_format_code; 141 142 sff.read(_key_length); 143 sff.read(_number_of_flows); 144 sff.read(_flowgram_format_code); 145 enforce(_flowgram_format_code == 1, 146 "Flowgram format codes other than 1 are not supported"); 147 148 _flow_chars = cast(string)sff.readString(_number_of_flows); 149 _key_sequence = cast(string)sff.readString(_key_length); 150 } 151 } 152 }