-
Notifications
You must be signed in to change notification settings - Fork 0
/
countTaxa.d
72 lines (63 loc) · 1.88 KB
/
countTaxa.d
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
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] ~" <folder> <condition> <number> [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);
}
}