-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
283 lines (275 loc) · 9.54 KB
/
main.go
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
// The are different phases that this tool will normally run.
// 1 - Find: Find all cliques, where a clique is a fully connected graph
// where all nodes are connected to all other nodes in the graph.
// 2 - Build: Build upon the cliques found in step 1 to enlarge the groups
// to include nodes that are highly connected to the other nodes but are
// not fully connected using the parameter settings to determine what
// percentage of existing nodes a potential new node must be connected to.
// This process will result in many overlapping groups.
// 3 - Merge: Merge overlapping groups. This is done by the percent overlap
// setting and the resulting groups may violate the rules in the
// paramater settings.
// 4 - Prune: Prune groups down by kicking out group members that violate the
// parameter settings.
// There are other functions that can also be run in addition to the phases:
// Sort: sort the internal database of groups by placing the largest groups
// in front
// Rename: rename the internal groups so that the groups at the beginning
// of the list is 1, then 2, etc.
import (
"fmt"
"os"
"strconv"
"strings"
)
// CliqueToolVersion represents the release number
const CliqueToolVersion string = "1.0.0"
var verboseLevel int = 0
func fatalError(message string) {
_, _ = os.Stderr.WriteString(message + "\n")
os.Exit(1)
}
func debugMessage(message string) {
if verboseLevel > 0 {
fmt.Printf("%s\n", message)
}
}
func main() {
//paramFile := nil
inputFile := ""
minGroupSize := 3
searchPasses := 1
//outputFile := ""
//outputDir := "."
var nodeDb NodeDb
var err error
var groupDb *GroupDb
needWrite := false
var groupParams GroupParameters
mergeOverlapRatio := DefaultMergeRatioThreshold
mergeMaxMissingGroupLinks := -1
groupPrefix := "cliquetool"
reportFormat := ReportFormatMarkdown
reportSortBySize := true
numThreads := 1
// Rather than using the built-in command line parsing, we handle them one at a time here.
// This is because the order of the parameters matters.
for i := 1; i < len(os.Args); i++ {
arg := os.Args[i]
//fmt.Printf("Arg %d: %s\n", i, arg)
if arg == "-v" || arg == "-verbose" {
verboseLevel++
logInit("cliquetool.log", verboseLevel)
} else if arg == "-version" {
fmt.Printf("cliquetool version %s\n", CliqueToolVersion)
os.Exit(0)
} else if arg == "-minsize" {
if i+1 >= len(os.Args) {
fatalError("Parameter -minsize requres an integer parameter")
}
i++
minGroupSize, err = strconv.Atoi(os.Args[i])
if err != nil {
fatalError("Invalid parameter for -minGroupSize")
}
} else if arg == "-t" || arg == "-threads" {
if i+1 >= len(os.Args) {
fatalError("Parameter -threads requres an integer parameter")
}
i++
numThreads, err = strconv.Atoi(os.Args[i])
if err != nil {
fatalError("Invalid parameter for -threads")
}
} else if arg == "-prefix" {
if i+1 >= len(os.Args) {
fatalError("Parameter -prefix requres an string parameter")
}
i++
groupPrefix = os.Args[i]
} else if arg == "-passes" {
if i+1 >= len(os.Args) {
fatalError("Parameter -passes requres an integer parameter")
}
i++
searchPasses, err = strconv.Atoi(os.Args[i])
if err != nil {
fatalError("Invalid parameter for -passes")
}
} else if arg == "-i" || arg == "-infile" {
if i+1 >= len(os.Args) {
fatalError("Parameter -infile requres a filename")
}
if len(inputFile) > 0 {
fatalError("Error: only one input file can be specified\n")
}
i++
inputFile = os.Args[i]
nodeDb, err = readNodeFile(inputFile, verboseLevel)
if err != nil {
fatalError("Error opening file: " + inputFile)
}
if verboseLevel > 0 {
fmt.Printf("Found %d unique nodes\n", len(nodeDb.nodes))
if verboseLevel > 1 {
dumpDb(nodeDb)
}
}
fmt.Printf("Loaded %d nodes from %s.\n", len(nodeDb.nodes), inputFile)
} else if strings.HasPrefix(arg, "-dimacs") {
if i+1 >= len(os.Args) {
fatalError("Parameter -dimacsinfile requres a filename")
}
if len(inputFile) > 0 {
fatalError("Error: only one input file can be specified\n")
}
i++
inputFile = os.Args[i]
nodeDb, err = readNodeFileDimacs(inputFile, verboseLevel)
if err != nil {
fatalError("Error opening file: " + inputFile)
}
if verboseLevel > 0 {
fmt.Printf("Found %d unique nodes\n", len(nodeDb.nodes))
if verboseLevel > 1 {
dumpDb(nodeDb)
}
}
fmt.Printf("Loaded %d nodes from %s.\n", len(nodeDb.nodes), inputFile)
} else if arg == "-loadgroupcsv" {
// Must have already loaded data with -infile
if len(nodeDb.nodes) == 0 {
fatalError("You must load data with -infile or -dimacsinfile before using -loadgroupcsv")
}
i++
inputFile = os.Args[i]
if groupDb != nil && len(groupDb.groups) > 0 {
fmt.Printf("Warning: discarding previously found %d groups\n", len(groupDb.groups))
}
groupDb, err = loadGroupDbFromCSV(inputFile, &nodeDb)
if err != nil {
fatalError(fmt.Sprintf("Error loading groups from file: %v", err))
}
} else if arg == "-f" || arg == "-find" {
// Must have already loaded data with -infile
if len(nodeDb.nodes) == 0 {
fatalError("You must load data with -infile or -dimacsinfile before using -find")
}
groupDb, err = findGroups(&nodeDb, minGroupSize, searchPasses, verboseLevel, numThreads)
if err != nil {
fatalError(fmt.Sprintf("Error finding groups: %v", err))
}
needWrite = true
} else if arg == "-p" || strings.HasPrefix(arg, "-param") {
if i+1 >= len(os.Args) {
fatalError("Parameter -param requres a filename")
}
if len(groupParams.filename) > 0 {
fatalError("Error: only one group parameter file can be specified\n")
}
i++
paramFile := os.Args[i]
groupParams, err = readParamFile(paramFile)
if err != nil {
fatalError(fmt.Sprintf("Error reading group parameter file: %v", err))
}
} else if arg == "-b" || strings.HasPrefix(arg, "-build") {
// Must have already loaded data with -infile and specified output with -outfile
if len(nodeDb.nodes) == 0 {
fatalError("You must load node data with -infile before using -build")
}
if groupDb == nil || len(groupDb.groups) == 0 {
fatalError("You must load group data with -loadcsv or using -infile and -find before using -build")
}
if len(groupParams.params) == 0 {
fatalError("You must load a group paramater file with -param before using -build")
}
groupDb2 := buildGroups(&nodeDb, groupDb, groupParams, false, minGroupSize, verboseLevel)
groupDb = groupDb2
needWrite = true
} else if arg == "-rename" {
renameGroups(groupDb, groupPrefix)
needWrite = true
} else if strings.HasPrefix(arg, "-mergeparam") {
if i+1 >= len(os.Args) {
fatalError("Parameter -mergeparameter requres a decimal")
}
i++
mergeOverlapRatio, err = strconv.ParseFloat(os.Args[i], 64)
if err != nil {
fatalError(fmt.Sprintf("Invalid mergeparameter: %v", err))
}
} else if arg == "-merge" {
groupDb = mergeGroups(&nodeDb, groupDb, mergeOverlapRatio,
mergeMaxMissingGroupLinks, &groupParams, minGroupSize, verboseLevel)
needWrite = true
} else if arg == "-removesubsets" {
removeGroupSubsets(groupDb, verboseLevel)
} else if arg == "-prune" {
// Must have already loaded data with -infile and specified output with -outfile
if len(nodeDb.nodes) == 0 {
fatalError("You must load node data with -infile before using -prune")
}
if groupDb == nil || len(groupDb.groups) == 0 {
fatalError("You must load group data with -loadcsv or using -infile and -find before using -prune")
}
if len(groupParams.params) == 0 {
fatalError("You must load a group paramater file with -param before using -prune")
}
pruneGroups(&nodeDb, groupDb, &groupParams, minGroupSize, verboseLevel)
removeGroupSubsets(groupDb, verboseLevel)
needWrite = true
} else if arg == "-sort" {
sortGroupDb(groupDb)
} else if arg == "-writecsv" {
// Write found groups to a file. Make sure we have groups.
if groupDb == nil || len(groupDb.groups) == 0 {
fatalError("You must find or load groups before using -writecsv")
}
if i+1 >= len(os.Args) {
fatalError("Parameter -writecsv requres a filename")
}
i++
outputFile := os.Args[i]
err = writeGroupFile(&nodeDb, groupDb, outputFile, minGroupSize, verboseLevel)
if err != nil {
fmt.Printf("Error writing file %s: %s\n", outputFile, err)
}
needWrite = false
} else if arg == "-html" {
// For use with -verify
reportFormat = ReportFormatHtml
} else if arg == "-markdown" {
// For use with -verify
reportFormat = ReportFormatMarkdown
} else if arg == "-reportbysize" {
// Sort groups in report by group size, largest first
reportSortBySize = true
} else if arg == "-reportbyconcern" {
// Sort groups in report with those most likely missing some nodes first
reportSortBySize = false
} else if arg == "-verify" || arg == "-report" {
if groupDb == nil || len(groupDb.groups) == 0 {
fatalError("You must find or load groups before using -verify or -report")
}
if i+1 >= len(os.Args) {
fatalError("Parameters -verify and -report requre a filename")
}
i++
outputFile := os.Args[i]
if outputFile == "-" {
outputFile = "" // Allow stdout when "-" is passed as output file
}
err = verifyGroupDb(&nodeDb, groupDb, outputFile, reportFormat, reportSortBySize)
if err != nil {
fmt.Printf("Error writing file %s: %s\n", outputFile, err)
}
} else {
fatalError("Unrecognized parameter: " + os.Args[i])
}
}
if needWrite {
fmt.Println("Warning: final data was not saved to a file.")
}
}