/
componentvector.go
167 lines (136 loc) · 3.11 KB
/
componentvector.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
package wtype
import (
"fmt"
"strings"
"github.com/antha-lang/antha/antha/anthalib/wunit"
)
type ComponentVector []*Liquid
func (cv ComponentVector) String() string {
s := fmt.Sprintf("%v %v %v", cv.GetNames(), cv.GetVols(), cv.GetWellCoords())
return s
}
// Map return a new componentvector which is the result calling each function in maps sequentially on
// each element
func (cv ComponentVector) Map(maps ...func(*Liquid) *Liquid) ComponentVector {
ret := make(ComponentVector, len(cv))
for i := range cv {
ret[i] = cv[i]
for _, m := range maps {
ret[i] = m(ret[i])
}
}
return ret
}
// Filter returns the subset of the liquids in cv for which every filter returns true
// does not guarantee to call every filter on every liquid
func (cv ComponentVector) Filter(filters ...func(*Liquid) bool) ComponentVector {
ret := make(ComponentVector, 0, len(cv))
Outer:
for _, l := range cv {
for _, filter := range filters {
if !filter(l) {
// don't bother checking the rest once one fails
// continue the outer for loop
continue Outer
}
}
ret = append(ret, l)
}
return ret
}
func (cv ComponentVector) SubtractVolume(vol wunit.Volume) {
for i := 0; i < len(cv); i++ {
v := cv[i].Volume()
v.Subtract(vol)
if !v.IsPositive() {
cv[i] = nil
} else {
cv[i].SetVolume(v)
}
}
}
func (cv ComponentVector) Dup() ComponentVector {
ret := make(ComponentVector, len(cv))
for i, v := range cv {
if v == nil {
continue
}
ret[i] = v.Dup()
}
return ret
}
func (cv ComponentVector) GetNames() []string {
sa := make([]string, len(cv))
for i := 0; i < len(cv); i++ {
if cv[i] != nil {
sa[i] = cv[i].FullyQualifiedName()
}
}
return sa
}
func (cv ComponentVector) GetVols() []wunit.Volume {
ret := make([]wunit.Volume, len(cv))
for i, c := range cv {
if c == nil {
ret[i] = wunit.ZeroVolume()
} else if c.Vunit == "" {
ret[i] = wunit.NewVolume(c.Vol, "ul")
} else {
ret[i] = wunit.NewVolume(c.Vol, c.Vunit)
}
}
return ret
}
// IsEmpty returns true if there is no volume in the component vector
func (cv ComponentVector) IsEmpty() bool {
for _, c := range cv {
if !c.Volume().IsZero() {
return false
}
}
return true
}
func (cv ComponentVector) GetPlateIds() []string {
return cv.getLocTok(0)
}
func (cv ComponentVector) GetWellCoords() []string {
return cv.getLocTok(1)
}
func (cv ComponentVector) getLocTok(x int) []string {
ret := make([]string, len(cv))
for i, c := range cv {
if c == nil {
continue
}
tx := strings.Split(c.Loc, ":")
if len(tx) <= x {
ret[i] = ""
continue
}
ret[i] = tx[x]
}
return ret
}
func (cv1 ComponentVector) Equal(cv2 ComponentVector) bool {
if len(cv1) != len(cv2) {
return false
}
for i := 0; i < len(cv1); i++ {
if cv1[i] != nil && cv2[i] != nil {
if !cv1[i].EqualTypeVolumeID(cv2[i]) {
return false
}
} else if !(cv1[i] == nil && cv2[i] == nil) {
return false
}
}
return true
}
// Volume get the total volume of liquid in the component vector
func (cv ComponentVector) Volume() wunit.Volume {
r := wunit.ZeroVolume()
for _, l := range cv {
r.Add(l.Volume())
}
return r
}