forked from davidebolo1993/cosigt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosigt.go
233 lines (141 loc) · 3.46 KB
/
cosigt.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
package main
import (
"bufio"
"compress/gzip"
"encoding/csv"
"fmt"
"io"
"math"
"os"
"sort"
"strconv"
"strings"
"gonum.org/v1/gonum/stat/combin"
)
//all these functions assume no errors in input files
func GetCosineSimilarity(A []float64, B []float64) float64 {
var eucl_magn, dot_product, similarity float64
eucl_magn = GetMagnitude(A, B)
if eucl_magn > 0 {
dot_product = GetDotProduct(A, B)
similarity = dot_product / eucl_magn
} else {
similarity = 0
}
return similarity
}
func GetMagnitude(A []float64, B []float64) float64 {
var A_len, B_len float64
A_len = 0
B_len = 0
for i := 0; i < len(A); i += 1 {
A_len += (A[i] * A[i])
B_len += (B[i] * B[i])
}
return math.Sqrt(A_len * B_len)
}
func GetDotProduct(A []float64, B []float64) float64 {
var dot_product float64
for i := 0; i < len(A); i++ {
dot_product += (A[i] * B[i])
}
return dot_product
}
func ReadBlacklist(s string) []string {
ids := make([]string, 0, 10) //don't want to re-allocate too many times
f, _ := os.Open(s)
defer f.Close()
b := bufio.NewScanner(f)
for b.Scan() {
ids = append(ids, b.Text())
}
return ids
}
func ReadGz(s string) ([]string, [][]float64) {
cov := make([][]float64, 0, 1000)
id := make([]string, 0, 1000)
f, _ := os.Open(s)
defer f.Close()
gr, _ := gzip.NewReader(f)
defer gr.Close()
cr := csv.NewReader(gr)
cr.Comma = '\t'
//skip header
cr.Read()
for {
rec, err := cr.Read()
if err == io.EOF {
break
}
f64s := make([]float64, len(rec)-1) //rec[0] is the haplotype name in z.paths and sample name in x.gafpack
for i, s := range rec[1:] {
f64s[i], _ = strconv.ParseFloat(s, 64)
}
cov = append(cov, f64s)
id = append(id, rec[0])
}
return id, cov
}
func WriteMap(m map[string]float64, s string) {
f, _ := os.Create(s)
defer f.Close()
w := csv.NewWriter(f)
w.Comma = '\t'
defer w.Flush()
for k, v := range m {
_ = w.Write([]string{k, fmt.Sprintf("%.16f", v)})
}
}
func SliceContains(s string, ids []string) bool {
for _, x := range ids {
if strings.Contains(s, x) {
return true
}
}
return false
}
func SumSlices(a []float64, b []float64) []float64 {
c := make([]float64, len(a))
for i := 0; i < len(a); i++ {
c[i] = a[i] + b[i]
}
return c
}
func main() {
//I/O files
//read first table
hapid, gcov := ReadGz(os.Args[1])
//read second table
smpl, bcov := ReadGz(os.Args[2])
//read blacklist - it can be empty or not
blck := ReadBlacklist(os.Args[3])
//store results in map
m := make(map[string]float64)
n := len(hapid)
k := 2
gen := combin.NewCombinationGenerator(n, k)
for gen.Next() {
h1, h2 := gen.Combination(nil)[0], gen.Combination(nil)[1]
if len(blck) == 0 || (!SliceContains(hapid[h1], blck) && !SliceContains(hapid[h2], blck)) { //nothing to blacklist or both ids not in blacklist
sum := SumSlices(gcov[h1], gcov[h2])
indiv := (hapid[h1] + "-" + hapid[h2])
m[indiv] = GetCosineSimilarity(sum, bcov[0])
}
}
//sort map
keys := make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
}
sort.SliceStable(keys, func(i, j int) bool {
return m[keys[i]] > m[keys[j]]
})
//write combinations
_ = os.Mkdir(os.Args[4], os.ModePerm)
WriteMap(m, os.Args[4]+"/combos.tsv")
//write best score
f, _ := os.Create(os.Args[4] + "/best_genotype.tsv")
defer f.Close()
result := fmt.Sprintf("#sample\tbest_genotype\tbest_score\n%s\t%s\t%.16f\n", smpl[0], keys[0], m[keys[0]])
_, _ = f.WriteString(result)
}