-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathego.go
348 lines (345 loc) · 7.52 KB
/
ego.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"bufio"
"flag"
"fmt"
"github.com/evolbioinf/clio"
"github.com/evolbioinf/gin/util"
"io"
"log"
"math"
"os"
"sort"
"strings"
"text/tabwriter"
)
type gosym struct {
g string
s []string
}
type gosymSlice []*gosym
type GOterm struct {
a, d, c string
o, e, pm, pp float64
}
type GOslice []*GOterm
func (g gosymSlice) Len() int {
return len(g)
}
func (g gosymSlice) Swap(i, j int) {
g[i], g[j] = g[j], g[i]
}
func (g gosymSlice) Less(i, j int) bool {
l1 := len(g[i].s)
l2 := len(g[j].s)
if l1 != l2 {
return l1 < l2
}
return g[i].g < g[j].g
}
func (g GOslice) Len() int { return len(g) }
func (g GOslice) Swap(i, j int) {
g[i], g[j] = g[j], g[i]
}
func (g GOslice) Less(i, j int) bool {
if g[i].pm != g[j].pm {
return g[i].pm < g[j].pm
}
er1 := g[i].o / g[i].e
er2 := g[j].o / g[j].e
if er1 != er2 {
return er1 > er2
}
return g[i].a < g[j].a
}
func gid2go(ids map[string]bool,
id2go map[string]map[string]bool) map[string]int {
og := make(map[string]int)
for id, _ := range ids {
gm := id2go[id]
if gm == nil {
continue
}
for g, _ := range gm {
og[g]++
}
}
return og
}
func scan(r io.Reader, args ...interface{}) {
obsGOcounts := args[0].(map[string]int)
id2go := args[1].(map[string]map[string]bool)
go2descr := args[2].(map[string]string)
go2cat := args[3].(map[string]string)
minOcc := args[4].(int)
raw := args[5].(bool)
sc := bufio.NewScanner(r)
n := 0
ids := make(map[string]bool)
expGOcounts := make(map[string]int)
eCounts := make(map[string]int)
eVals := make(map[string][]int)
for sc.Scan() {
fields := strings.Fields(sc.Text())
if len(fields) > 1 {
log.Fatalf("can't parse %q", sc.Text())
}
if sc.Text() == "" {
n++
eg := gid2go(ids, id2go)
for k, o := range obsGOcounts {
e := eg[k]
expGOcounts[k] += e
eVals[k] = append(eVals[k], e)
if e >= o {
eCounts[k]++
}
}
for id, _ := range ids {
delete(ids, id)
}
} else {
ids[fields[0]] = true
}
}
n++
eg := gid2go(ids, id2go)
for k, o := range obsGOcounts {
e := eg[k]
expGOcounts[k] += e
eVals[k] = append(eVals[k], e)
if e >= o {
eCounts[k]++
}
}
GOterms := make([]*GOterm, 0)
for a, o := range obsGOcounts {
gt := new(GOterm)
pm := float64(eCounts[a]) / float64(n)
if pm == 0 {
pm = -1
}
var mean, sd float64
data := eVals[a]
nn := len(data)
for i := 0; i < nn; i++ {
mean += float64(data[i])
}
mean /= float64(nn)
v := 0.0
for i := 0; i < nn; i++ {
s := mean - float64(data[i])
v += s * s
}
v /= float64(nn - 1)
sd = math.Sqrt(v)
x := (float64(o) - mean) / (sd * math.Sqrt(2))
pp := 1.0 - 0.5*(1.0+math.Erf(x))
gt.pm = pm
gt.pp = pp
gt.a = a
gt.d = go2descr[a]
gt.c = go2cat[a]
gt.o = float64(o)
gt.e = float64(expGOcounts[a]) / float64(n)
GOterms = append(GOterms, gt)
}
sort.Sort(GOslice(GOterms))
w := tabwriter.NewWriter(os.Stdout, 1, 0, 2, ' ', 0)
fmt.Fprintf(w, "#GO\tO\tE\tO/E\tP_m(n=%.1e)\t", float64(n))
fmt.Fprint(w, "P_p\tCategory\tDescription\n")
for _, g := range GOterms {
if int(g.o) < minOcc {
continue
}
fmt.Fprintf(w,
"%s\t%d\t%.3g\t%.3g\t%.3g\t%.3g\t%s\t%s\n",
g.a, int(g.o), g.e, g.o/g.e, g.pm, g.pp, g.c, g.d)
}
w.Flush()
if raw {
for _, term := range GOterms {
a := term.a
fmt.Printf("R %s", a)
counts := eVals[a]
for _, count := range counts {
fmt.Printf(" %d", count)
}
fmt.Printf("\n")
}
}
}
func main() {
util.Name("ego")
u := "ego [-h] -o gene2go [option]... " +
"obsID.txt [expID.txt]..."
p := "Calculate enrichment of GO terms for " +
"observed list of gene IDs, given a " +
"large number of expected ID lists."
e := "ego -o gene2go obsID.txt expID.txt"
clio.Usage(u, p, e)
var optV = flag.Bool("v", false, "version")
var optO = flag.String("o", "", "file of mapping "+
"gene IDs to GO terms")
var optG = flag.String("g", "",
"GFF file to print GO/sym table")
var optM = flag.Int("m", 10, "minimum occupancy of GO term")
var optR = flag.Bool("r", false, "print raw gene counts")
flag.Parse()
if *optV {
util.Version()
}
if *optO == "" {
m := "please enter a file mapping gene IDs " +
"to GO accessions using -o"
log.Fatal(m)
}
f := util.Open(*optO)
defer f.Close()
sc := bufio.NewScanner(f)
id2go := make(map[string]map[string]bool)
go2descr := make(map[string]string)
go2cat := make(map[string]string)
for sc.Scan() {
if sc.Text()[0] == '#' {
continue
}
fields := strings.Split(sc.Text(), "\t")
if len(fields) != 8 {
m := "gene2go file has %d columns instead of " +
"the expected 8; are you using " +
"the correct file?"
log.Fatalf(m, len(fields))
}
geneId := fields[1]
goId := fields[2]
goDescr := fields[5]
goCat := fields[7]
if id2go[geneId] == nil {
id2go[geneId] = make(map[string]bool)
}
id2go[geneId][goId] = true
li := strings.LastIndex(goDescr, "[")
if li > -1 && strings.Contains(goDescr, "GO:") {
if li > 0 {
goDescr = goDescr[:li-1]
} else {
goDescr = goDescr[:li]
}
}
go2descr[goId] = goDescr
go2cat[goId] = goCat
}
var id2sym map[string]string
if *optG != "" {
id2sym = make(map[string]string)
f := util.Open(*optG)
sc := bufio.NewScanner(f)
for sc.Scan() {
t := sc.Text()
if t[0] == '#' {
continue
}
fields := strings.Split(t, "\t")
if len(fields) != 9 {
m := "expecting 9 fields in GFF file, " +
"but you have %d"
log.Fatalf(m, len(fields))
}
if fields[2] == "gene" {
sym := ""
gid := ""
attributes := strings.Split(fields[8], ";")
for _, attribute := range attributes {
kv := strings.Split(attribute, "=")
if kv[0] == "Dbxref" {
ids := strings.Split(kv[1], ",")
for _, id := range ids {
arr := strings.Split(id, ":")
if arr[0] == "GeneID" {
gid = arr[1]
}
}
}
if kv[0] == "Name" {
sym = kv[1]
}
}
if gid == "" {
fmt.Fprintf(os.Stderr,
"couldn't find gene ID in %q\n",
attributes)
}
if sym == "" {
fmt.Fprintf(os.Stderr, "couldn't find name in %q\n",
attributes)
} else if gid == "" {
log.Fatalf("found name but no gene ID in %q\n",
attributes)
}
id2sym[gid] = sym
}
}
}
files := flag.Args()
if len(files) < 1 {
log.Fatal("please enter a file with observed IDs")
}
f = util.Open(files[0])
defer f.Close()
sc = bufio.NewScanner(f)
ids := make(map[string]bool)
for sc.Scan() {
fields := strings.Fields(sc.Text())
if len(fields) > 1 {
m := "gene IDs should be " +
"in a single column"
log.Fatal(m)
}
ids[fields[0]] = true
}
if *optG != "" {
g2s := make(map[string][]string)
for id, _ := range ids {
sym := id2sym[id]
gterms := id2go[id]
for gterm, _ := range gterms {
sl := g2s[gterm]
if sl == nil {
sl = make([]string, 0)
}
sl = append(sl, sym)
g2s[gterm] = sl
}
}
var gosyms []*gosym
for g, symbols := range g2s {
gs := new(gosym)
gs.g = g
sort.Strings(symbols)
gs.s = symbols
gosyms = append(gosyms, gs)
}
sort.Sort(gosymSlice(gosyms))
w := tabwriter.NewWriter(os.Stdout, 1, 0, 2, ' ', 0)
for i := len(gosyms) - 1; i >= 0; i-- {
gosym := gosyms[i]
fmt.Fprintf(w, "%s\t%d\t%s\t%s\t", gosym.g,
len(gosym.s), go2cat[gosym.g],
go2descr[gosym.g])
for i, symbol := range gosym.s {
if i > 0 {
fmt.Fprintf(w, " ")
}
fmt.Fprintf(w, "%s", symbol)
}
fmt.Fprintf(w, "\n")
}
w.Flush()
os.Exit(0)
}
obsGOcounts := gid2go(ids, id2go)
files = files[1:]
clio.ParseFiles(files, scan, obsGOcounts,
id2go, go2descr, go2cat, *optM, *optR)
}