Browse Source

SimpleSplitConv: handle last item and exception(s)

georgeg
Pjotr Prins 4 years ago
parent
commit
133d4b7151
  1. 31
      bio/std/genotype/snp.d
  2. 5
      bio/std/range/splitter.d

31
bio/std/genotype/snp.d

@ -52,18 +52,25 @@ SNP[] fetch_snp_annotations(string filen) {
info("Parsing ",filen);
File f = File(filen);
foreach(line; f.byLine) {
auto fields = array(SimpleSplitConv!(char[])(line));
auto name = to!string(fields[0]);
if (name in names)
throw new Exception("SNP name "~name~" appears multiple times in "~filen);
auto pos = (fields[1] == "NA" ? -1 : to!ulong(fields[1]));
auto chr = to!string(fields[2]);
double cm = -1.0;
if (fields.length > 3)
cm = (fields[3] == "NA" ? -1.0 : to!double(fields[3]));
auto snp = SNP(name,chr,pos,cm);
list ~= snp;
try {
foreach(line; f.byLine) {
auto fields = array(SimpleSplitConv!(char[])(line));
auto name = to!string(fields[0]);
if (name in names)
throw new Exception("SNP name "~name~" appears multiple times in "~filen);
auto pos = (fields[1] == "NA" ? -1 : to!ulong(fields[1]));
auto chr = to!string(fields[2]);
double cm = -1.0;
if (fields.length > 3)
cm = (fields[3] == "NA" ? -1.0 : to!double(fields[3]));
auto snp = SNP(name,chr,pos,cm);
list ~= snp;
}
}
catch(Exception e) {
writeln(e.msg); // need to test and give proper output FIXME
throw e;
}
return list;
}

5
bio/std/range/splitter.d

@ -55,6 +55,10 @@ struct SimpleSplitConv(R)
in_whitespace = false;
}
}
if (!in_whitespace) { // emit final
auto token = list[start..$];
dg(token);
}
return 0;
}
}
@ -64,4 +68,5 @@ unittest {
assert(array(SimpleSplitConv!(ubyte[])(s)) == ["hello","1","2","3","4"]);
assert(array(SimpleSplitConv!(ubyte[])(cast(ubyte[])" hello, 1 2 \t3 4 \n")) == ["","hello","1","2","3","4"]);
assert(array(SimpleSplitConv!(ubyte[])(cast(ubyte[])"hello, 1 2 \n\t3 4 \n")) == ["hello","1","2","3","4"]);
assert(array(SimpleSplitConv!(ubyte[])(cast(ubyte[])"chr1:55365,55365,1")) == ["chr1:55365","55365","1"]);
}

Loading…
Cancel
Save