From 45bcd7f3a120a3ae465e9dd149173352f239c556 Mon Sep 17 00:00:00 2001 From: Guoyi Zhang Date: Sun, 15 Sep 2024 16:12:47 +1000 Subject: [PATCH] add: count fasta taxa from one folder --- countTaxa.d | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 countTaxa.d diff --git a/countTaxa.d b/countTaxa.d new file mode 100644 index 0000000..08825e2 --- /dev/null +++ b/countTaxa.d @@ -0,0 +1,72 @@ +import std.stdio; +import std.file; +import std.algorithm; +import std.array; +import std.conv; +import std.getopt; +import std.path; + +void main(string[] args) +{ + if (args.length < 4 || args.length > 5 ) + { + writeln("Usage: "~ args[0] ~" [output_file]"); + return; + } + + // getargs + string folder = args[1]; + string condition = args[2]; + long threshold = to!long(args[3]); + bool hasOutputFile = args.length > 4; + string outputFile = hasOutputFile ? args[4] : ""; + + // resotre results + string result; + + // ie all the file + foreach (entry; dirEntries(folder, SpanMode.shallow)) + { + if (entry.isFile) + { + // count '>' + size_t count = 0; + foreach (line; File(entry.name).byLine()) + { + if (line.length > 0 && line[0] == '>') + { + count++; + } + } + + // judge + bool conditionMet = false; + if (condition == "=" && count == threshold) conditionMet = true; + else if (condition == "<" && count < threshold) conditionMet = true; + else if (condition == ">" && count > threshold) conditionMet = true; + else if (condition == "<=" && count <= threshold) conditionMet = true; + else if (condition == ">=" && count >= threshold) conditionMet = true; + else if (condition == "><" && count != threshold) conditionMet = true; + else if (condition == "<>" && count != threshold) conditionMet = true; + + + if (conditionMet) + { + result ~= baseName(entry.name) ~ " : " ~ to!string(count) ~ "\n"; + } + } + } + + // write + if (hasOutputFile) + { + // write to file + std.file.write(outputFile, result); + } + else + { + // output to consol + writeln(result); + } +} +