-
Notifications
You must be signed in to change notification settings - Fork 3
/
cosigt.go
321 lines (276 loc) · 8.53 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"bufio"
"compress/gzip"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"math"
"github.com/akamensky/argparse"
"gonum.org/v1/gonum/stat/combin"
)
// Vector represents a float64 slice for cleaner type declarations
type Vector []float64
// GetMagnitude calculates the Euclidean magnitude of two vectors
func GetMagnitude(A, B Vector) float64 {
var ALen, BLen float64
for i, a := range A {
ALen += a * a
BLen += B[i] * B[i]
}
return math.Sqrt(ALen * BLen)
}
// GetDotProduct calculates the dot product of two vectors
func GetDotProduct(A, B Vector) float64 {
var dotProduct float64
for i, a := range A {
dotProduct += a * B[i]
}
return dotProduct
}
// GetCosineSimilarity calculates the cosine similarity between two vectors
func GetCosineSimilarity(A, B Vector) float64 {
euclMagn := GetMagnitude(A, B)
if euclMagn > 0 {
return GetDotProduct(A, B) / euclMagn
}
return 0
}
// ReadBlacklist reads a file containing paths to exclude
func ReadBlacklist(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error opening blacklist file: %w", err)
}
defer file.Close()
var ids []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
ids = append(ids, scanner.Text())
}
return ids, scanner.Err()
}
// ReadGz reads gzip-compressed TSV files containing gafpack and odgi paths data
func ReadGz(filename string) ([]string, []Vector, error) {
file, err := os.Open(filename)
if err != nil {
return nil, nil, fmt.Errorf("error opening gzip file: %w", err)
}
defer file.Close()
gr, err := gzip.NewReader(file)
if err != nil {
return nil, nil, fmt.Errorf("error creating gzip reader: %w", err)
}
defer gr.Close()
cr := csv.NewReader(gr)
cr.Comma = '\t'
// Skip header
if _, err := cr.Read(); err != nil {
return nil, nil, fmt.Errorf("error reading header: %w", err)
}
var id []string
var cov []Vector
for {
rec, err := cr.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, nil, fmt.Errorf("error reading record: %w", err)
}
f64s := make(Vector, len(rec)-1)
for i, s := range rec[1:] {
f64s[i], err = strconv.ParseFloat(s, 64)
if err != nil {
return nil, nil, fmt.Errorf("error parsing float: %w", err)
}
}
cov = append(cov, f64s)
id = append(id, rec[0])
}
return id, cov, nil
}
// ReadJSON reads a JSON file containing cluster information
func ReadJSON(filename string) (map[string]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error opening JSON file: %w", err)
}
defer file.Close()
var clstr map[string]string
if err := json.NewDecoder(file).Decode(&clstr); err != nil {
return nil, fmt.Errorf("error decoding JSON: %w", err)
}
return clstr, nil
}
// WriteResults writes the analysis results to output files
func WriteResults(m *sync.Map, keys []string, clstr map[string]string, outDir, id string) error {
if err := os.MkdirAll(outDir, os.ModePerm); err != nil {
return fmt.Errorf("error creating output directory: %w", err)
}
sortedCombosFile, err := os.Create(filepath.Join(outDir, "sorted_combos.tsv"))
if err != nil {
return fmt.Errorf("error creating sorted_combos.tsv: %w", err)
}
defer sortedCombosFile.Close()
genotypeFile, err := os.Create(filepath.Join(outDir, "cosigt_genotype.tsv"))
if err != nil {
return fmt.Errorf("error creating cosigt_genotype.tsv: %w", err)
}
defer genotypeFile.Close()
sortedCombosWriter := csv.NewWriter(sortedCombosFile)
sortedCombosWriter.Comma = '\t'
defer sortedCombosWriter.Flush()
genotypeWriter := csv.NewWriter(genotypeFile)
genotypeWriter.Comma = '\t'
defer genotypeWriter.Flush()
// Write headers
if err := genotypeWriter.Write([]string{"#sample.id", "haplotype.1", "haplotype.2", "cluster.1", "cluster.2", "cosine.similarity"}); err != nil {
return fmt.Errorf("error writing genotype header: %w", err)
}
if err := sortedCombosWriter.Write([]string{"#haplotype.1", "haplotype.2", "cluster.1", "cluster.2", "cosine.similarity"}); err != nil {
return fmt.Errorf("error writing sorted combos header: %w", err)
}
for i, k := range keys {
haps := strings.Split(k, "$")
val, _ := m.Load(k) // Fetch value from sync.Map
cosineSimilarity := val.(float64)
if i == 0 {
if err := genotypeWriter.Write([]string{id, haps[0], haps[1], clstr[haps[0]], clstr[haps[1]], fmt.Sprintf("%.16f", cosineSimilarity)}); err != nil {
return fmt.Errorf("error writing genotype data: %w", err)
}
}
if err := sortedCombosWriter.Write([]string{haps[0], haps[1], clstr[haps[0]], clstr[haps[1]], fmt.Sprintf("%.16f", cosineSimilarity)}); err != nil {
return fmt.Errorf("error writing sorted combos data: %w", err)
}
}
return nil
}
// SliceContains checks if a string is contained in any string of a slice
func SliceContains(s string, ids []string) bool {
for _, x := range ids {
if strings.Contains(s, x) {
return true
}
}
return false
}
// SumSlices adds two float64 slices element-wise
func SumSlices(a, b Vector) Vector {
c := make(Vector, len(a))
for i := range a {
c[i] = a[i] + b[i]
}
return c
}
func main() {
parser := argparse.NewParser("cosigt", "genotyping loci in pangenome graphs using cosine distance")
p := parser.String("p", "paths", &argparse.Options{Required: true, Help: "gzip-compressed tsv file with path names and node coverages from odgi paths"})
g := parser.String("g", "gaf", &argparse.Options{Required: true, Help: "gzip-compressed gaf (graph alignment format) file for a sample from gafpack"})
b := parser.String("b", "blacklist", &argparse.Options{Required: false, Help: "txt file with names of paths to exclude (one per line)"})
c := parser.String("c", "cluster", &argparse.Options{Required: false, Help: "cluster json file as generated with cluster.r"})
o := parser.String("o", "output", &argparse.Options{Required: true, Help: "folder prefix for output files"})
i := parser.String("i", "id", &argparse.Options{Required: true, Help: "sample name"})
if err := parser.Parse(os.Args); err != nil {
log.Fatalf("Error parsing arguments: %v", err)
}
hapid, gcov, err := ReadGz(*p)
if err != nil {
log.Fatalf("Error reading paths file: %v", err)
}
_, bcov, err := ReadGz(*g)
if err != nil {
log.Fatalf("Error reading gaf file: %v", err)
}
var blck []string
if *b != "" {
blck, err = ReadBlacklist(*b)
if err != nil {
log.Fatalf("Error reading blacklist: %v", err)
}
}
clstr, err := ReadJSON(*c)
if err != nil {
log.Fatalf("Error reading cluster file: %v", err)
}
seen := sync.Map{} // Changed to use sync.Map for concurrency safety
m := sync.Map{} // Changed to use sync.Map for results
n := len(hapid)
k := 2 // fixed ploidy
numWorkers := runtime.NumCPU()
jobs := make(chan []int, numWorkers)
results := make(chan map[string]float64, numWorkers)
var wg sync.WaitGroup
// Start worker goroutines
for w := 0; w < numWorkers; w++ {
wg.Add(1)
go func() {
defer wg.Done()
for combo := range jobs {
localResults := make(map[string]float64)
h1, h2 := combo[0], combo[1]
if len(blck) == 0 || (!SliceContains(hapid[h1], blck) && !SliceContains(hapid[h2], blck)) {
sum := SumSlices(gcov[h1], gcov[h2])
indiv := hapid[h1] + "$" + hapid[h2]
localResults[indiv] = GetCosineSimilarity(sum, bcov[0])
_, seenH1 := seen.LoadOrStore(h1, true)
if !seenH1 {
sum = SumSlices(gcov[h1], gcov[h1])
indiv = hapid[h1] + "$" + hapid[h1]
localResults[indiv] = GetCosineSimilarity(sum, bcov[0])
}
_, seenH2 := seen.LoadOrStore(h2, true)
if !seenH2 {
sum = SumSlices(gcov[h2], gcov[h2])
indiv = hapid[h2] + "$" + hapid[h2]
localResults[indiv] = GetCosineSimilarity(sum, bcov[0])
}
}
results <- localResults
}
}()
}
// Generate combinations and send jobs
go func() {
gen := combin.NewCombinationGenerator(n, k)
for gen.Next() {
combo := gen.Combination(nil)
jobs <- combo
}
close(jobs)
}()
// Collect results
go func() {
for localResults := range results {
for k, v := range localResults {
m.Store(k, v) // Store result in sync.Map
}
}
close(results)
}()
wg.Wait()
// Sort results
keys := make([]string, 0)
m.Range(func(key, value interface{}) bool {
keys = append(keys, key.(string))
return true
})
sort.SliceStable(keys, func(i, j int) bool {
valI, _ := m.Load(keys[i])
valJ, _ := m.Load(keys[j])
return valI.(float64) > valJ.(float64)
})
// Write output
if err := WriteResults(&m, keys, clstr, *o, *i); err != nil {
log.Fatalf("Error writing results: %v", err)
}
}