1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*
This file is part of BioD.
Copyright (C) 2016 George Githinji <biorelated@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/*
The bio.core.fastq module is based from a question that I posted on forums.dlang.org.
I have bundled the answer as a module to support parsing fastq files with D.
Credit should go to Rikki Cattermole.
*/
module bio.std.file.fastq;
struct FastqRecord {
const(char)[] id;
const(char)[] seq;
const(char)[] qual;
static auto read(const(char)[] from) {
struct Result {
private {
const(char)[] source;
FastqRecord value;
bool isEmpty;
}
this(const(char)[] source) {
this.source = source;
popFront;
}
@property {
FastqRecord front() {
return value;
}
bool empty() {
return isEmpty;
}
}
void popFront() {
import std.string : indexOf;
if (source is null) {
isEmpty = true;
return;
}
void tidyInput() {
foreach(i, c; source) {
switch(c) {
case 0: .. case ' ':
break;
default:
source = source[i .. $];
return;
}
}
source = null;
}
tidyInput();
if (source is null)
return;
// id
assert(source[0] == '@');
ptrdiff_t len = source.indexOf("\n");
assert(len > 0);
value.id = source[1 .. len];
if (value.id[$-1] == "\r"[0])
value.id = value.id[0 .. $-1];
source = source[len + 1 .. $];
// seq
len = source.indexOf("\n");
assert(len > 0);
value.seq = source[0 .. len];
if (value.seq[$-1] == "\r"[0])
value.seq = value.seq[0 .. $-1];
source = source[len + 1 .. $];
// +id
len = source.indexOf("\n");
assert(len > 0);
source = source[len + 1 .. $];
// qual
len = source.indexOf("\n");
assert(len > 0);
value.qual = source[0 .. len];
if (value.qual[$-1] == "\r"[0])
value.qual = value.qual[0 .. $-1];
if (source.length > len + 1) {
source = source[len + 1 .. $];
tidyInput();
} else
source = null;
}
}
return Result(from);
}
}
/* fails with ldc >1.11
unittest {
string input = """
@seq1
TTATTTTAAT
+
?+BBB/DHH@
@seq2
GACCCTTTGCA
+
?+BHB/DIH@
@SEQ_ID
GATTTGGGGTTCAAAGCAGTATCGATCAAATAGTAAATCCATTTGTTCAACTCACAGTTT
+
!''*((((***+))%%%++)(%%%%).1***-+*''))**55CCF>>>>>>CCCCCCC65
"""[1 .. $];
foreach(record; FastqRecord.read(input)) {
import std.stdio;
// stderr.writeln(record); -> should be an assert statement
}
}
*/
|