/
bin_fasta.go
195 lines (163 loc) · 5.06 KB
/
bin_fasta.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
package main
import (
"flag"
"fmt"
genetic "github.com/handcraftsman/GeneticGo"
"github.com/op/go-logging"
"io/ioutil"
"math"
"strconv"
"strings"
"time"
)
var log = logging.MustGetLogger("main")
type resource struct {
name string
length int
}
func main() {
var lengthTable = flag.String("lengthTable", "", "Source length table (2 columns, name<TAB>length)")
var targetLength = flag.Int("targetLength", 1000, "Target length for bins")
var maxBins = flag.Int("maxBins", 10, "Try and have fewer bins than this")
var batchSize = flag.Int("batchSize", 40, "Batch N items at a time. MUST be <90")
var slop = flag.Int("slop", 100, "Allow a certain amount of slop.")
var patience = flag.Int("patience", 0, "Integer 0-5, with the max being Dalai-Lama-level patience")
flag.Parse()
resources := []resource{}
content, err := ioutil.ReadFile(*lengthTable)
if err != nil {
//Do something
panic(err)
}
lines := strings.Split(string(content), "\n")
for _, line := range lines {
data := strings.Split(line, "\t")
if len(data) == 2 {
length, _ := strconv.Atoi(data[1])
resources = append(
resources,
*&resource{
name: data[0],
length: length,
},
)
}
}
geneSet := "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-=_+!@#$%^&*()<>?|{}[];:',./\\"[0:*batchSize]
fmt.Printf("# Round IDX\tBin Idx\tSum\tFeature IDs\n")
for i := 0; i <= len(resources) / *batchSize; i++ {
min_bound := i * (*batchSize)
max_bound := (i + 1) * (*batchSize)
max_bound = int(math.Min(float64(max_bound), float64(len(resources))))
localResources := resources[min_bound:max_bound]
log.Info(fmt.Sprintf("Processing %d items", len(localResources)))
calc := func(candidate string) int {
decoded := decodeGenes(candidate, localResources, geneSet)
return getFitness(localResources, decoded, *targetLength, *maxBins, *slop)
}
start := time.Now()
disp := func(candidate string) {
decoded := decodeGenes(candidate, localResources, geneSet)
fitness := getFitness(localResources, decoded, *targetLength, *maxBins, *slop)
display(localResources, decoded, fitness, time.Since(start), i, false)
}
var solver = new(genetic.Solver)
solver.MaxSecondsToRunWithoutImprovement = 1 + float64(*patience)*20
solver.MaxRoundsWithoutImprovement = 10 + (*patience)*50
var best = solver.GetBest(calc, disp, geneSet, *maxBins, 32)
log.Info("Final:")
decoded := decodeGenes(best, localResources, geneSet)
fitness := getFitness(localResources, decoded, *targetLength, *maxBins, *slop)
display(localResources, decoded, fitness, time.Since(start), i, true)
}
}
func display(allResources []resource, resourceCounts map[resource]int, fitness int, elapsed time.Duration, roundIdx int, dumpData bool) {
bins := make(map[int][]resource)
binsum := make(map[int]int)
usemap := make(map[string]bool)
for _, resource := range allResources {
usemap[resource.name] = false
}
for resource, count := range resourceCounts {
bins[count] = append(bins[count], resource)
binsum[count] += resource.length
usemap[resource.name] = true
}
avg := 0.0
for bin_idx, _ := range bins {
avg += float64(binsum[bin_idx]) / float64(len(bins))
}
unused_count := 0
for _, used := range usemap {
if !used {
unused_count += 1
}
}
if !dumpData {
log.Debug(fmt.Sprintf(
"%d\t%d bins averaging %0.3f. Unused %d\t%s",
fitness,
len(bins),
avg,
unused_count,
elapsed))
} else {
nice_bin_idx := 1
for bin_idx, bins := range bins {
resnames := make([]string, 0)
for _, res := range bins {
resnames = append(resnames, res.name)
}
fmt.Printf("%d\t%d\t%d\t%s\n", roundIdx, nice_bin_idx, binsum[bin_idx], strings.Join(resnames, ","))
nice_bin_idx++
}
}
}
func decodeGenes(candidate string, lresources []resource, geneSet string) map[resource]int {
resourceCounts := make(map[resource]int, len(candidate)/2)
for i := 0; i < len(candidate); i += 2 {
chromosome := candidate[i : i+2]
resourceId := scale(strings.Index(geneSet, chromosome[0:1]), len(geneSet), len(lresources))
resourceCount := strings.Index(geneSet, chromosome[1:2])
resource := lresources[resourceId]
resourceCounts[resource] = resourceCounts[resource] + resourceCount
}
return resourceCounts
}
func scale(value, currentMax, newMax int) int {
return value * newMax / currentMax
}
func getFitness(allResources []resource, lresources map[resource]int, targetLength int, maxBins int, slop int) int {
score := 0
bins := make(map[int]int)
use_map := make(map[string]bool)
// Initialize with zeros
for _, res := range allResources {
use_map[res.name] = false
}
// loop over our resources
for resource, count := range lresources {
// separate into bins
bins[count] += resource.length
// mark as used when used
use_map[resource.name] = true
if count > maxBins {
score -= count * 100
}
}
// Loop over bins
for _, value := range bins {
diff := math.Abs(float64(targetLength - value))
if diff > float64(slop) {
score -= int(diff)
} else {
score += 1000
}
}
for _, used := range use_map {
if !used {
score -= 10000
}
}
return score
}