/
greedo.go
executable file
·225 lines (219 loc) · 6.78 KB
/
greedo.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
package main
import (
"bufio"
"cophycollapse"
"flag"
"fmt"
"log"
"math"
"math/rand"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"time"
)
func postorder(curnode *cophycollapse.Node) {
for _, chld := range curnode.CHLD {
postorder(chld)
}
fmt.Println(curnode.NAME, curnode.CONTRT, curnode.LEN)
}
func main() {
treeArg := flag.String("t", "", "input tree")
traitArg := flag.String("m", "", "continuous traits")
mclArg := flag.String("start", "", "user-specified starting clusters")
genArg := flag.Int("gen", 500000, "number of MCMC generations to run")
kArg := flag.Int("K", 2, "maximum number of clusters")
minKArg := flag.Int("minK", 1, "minimum number of clusters")
printFreqArg := flag.Int("pr", 100, "Frequency with which to print to the screen")
searchArg := flag.Int("f", 3, "0\tOptimize branch lengths for a user-specified clustering\n1\tOutput distance matrices calculated for each cluster provided by the -start argument\n2\tCalculate the log-likelihood of the dataset on a particular topology\n3\tPerform cluster analysis")
//sampFreqArg := flag.Int("samp", 1, "Frequency with which to sample from the chain")
runNameArg := flag.String("o", "cophycollapse", "specify the prefix for outfile names")
critArg := flag.Int("c", 0, "Criterion to use for hill climbing:\n0\tAIC\n1\tBIC\n2\tAICc\n")
splitGenArg := flag.Int("split", 10, "Number of iterations to run at each splitting step")
profileArg := flag.Bool("prof", false, "indicate whether to run the go profiler (for development)")
//threadArg := flag.Int("T", 1, "maximum number of cores to use during run")
//workersArg := flag.Int("W", 4, "Number of Go workers to use for LL calculation concurrency")
clustArg := flag.Float64("a", 1.0, "concentration parameter for new cluster penalty")
flag.Parse()
if *profileArg == true {
f, err := os.Create("profile.prof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
//var ntax,ntraits int
runtime.GOMAXPROCS(2)
nwk := cophycollapse.ReadLine(*treeArg)[0]
tree := cophycollapse.ReadTree(nwk)
traits, _, ntraits := cophycollapse.ReadContinuous(*traitArg)
cophycollapse.MapContinuous(tree, traits, ntraits)
rand.Seed(time.Now().UTC().UnixNano())
if *searchArg == 2 {
cophycollapse.InitMissingValues(tree.PreorderArray())
cophycollapse.MissingTraitsEM(tree, 100)
LL := 0.0
for site := range tree.CONTRT {
LL += cophycollapse.SingleSiteLL(tree, site)
}
fmt.Println(LL)
fmt.Println(tree.Newick(true) + ";")
os.Exit(0)
}
for _, n := range tree.PreorderArray()[1:] {
r := rand.Float64()
n.LEN = r
}
cophycollapse.InitMissingValues(tree.PreorderArray())
cophycollapse.MissingTraitsEM(tree, 100) //going to optimize branch lengths to set mean parameter for tree length in dirichlet prior
//fmt.Println(tree.Newick(true))
cophycollapse.InitParallelPRNLEN(tree.PreorderArray())
//fmt.Println("Starting tree AIC/BIC:", cophycollapse.CalcTreeAIC(tree, *critArg))
//fmt.Println(tree.Newick(true))
treeOutFile := *runNameArg
if *searchArg == 3 {
search := cophycollapse.InitGreedyHC(tree, *genArg, *printFreqArg, *critArg, true, *kArg, treeOutFile, *splitGenArg, *clustArg, *minKArg)
//fmt.Println(search.ClusterString())
start := time.Now()
search.PerturbedRun()
elapsed := time.Since(start)
fmt.Println("COMPLETED ", *genArg, "ITERATIONS IN ", elapsed)
} else if *searchArg == 0 {
if *mclArg == "" {
fmt.Println("You need to specify a cluster input file to run this option")
os.Exit(1)
}
clusters := cophycollapse.ReadMCLoutput(*mclArg)
nodes := tree.PreorderArray()
clmap, err := os.Create("cluster_key_dist")
if err != nil {
log.Fatal(err)
}
w1 := bufio.NewWriter(clmap)
for lab, c := range clusters {
f, err := os.Create(strconv.Itoa(lab) + ".bl.tre")
if err != nil {
log.Fatal(err)
}
w := bufio.NewWriter(f)
for _, n := range nodes[1:] {
r := rand.Float64()
n.LEN = r
}
cophycollapse.ClusterMissingTraitsEM(tree, c, 100)
//sites := ""
//for _, site := range c.Sites {
// sites += strconv.Itoa(site) + "\t"
//}
fmt.Fprint(w, tree.Newick(true)+";")
err = w.Flush()
if err != nil {
log.Fatal(err)
}
f.Close()
var strsites []string
for _, site := range c.Sites {
strsites = append(strsites, strconv.Itoa(site))
}
fmt.Fprint(w1, "CLUSTER"+strconv.Itoa(lab)+"\t"+strings.Join(strsites, "\t")+"\n")
}
err = w1.Flush()
if err != nil {
log.Fatal(err)
}
clmap.Close()
} else if *searchArg == 1 {
if *mclArg == "" {
fmt.Println("You need to specify a cluster input file to run this option")
os.Exit(1)
}
clusters := cophycollapse.ReadMCLoutput(*mclArg)
nodes := tree.PreorderArray()
clmap, err := os.Create("cluster_key_dist")
if err != nil {
log.Fatal(err)
}
w1 := bufio.NewWriter(clmap)
for lab, c := range clusters {
f, err := os.Create(strconv.Itoa(lab) + ".dist.phy")
if err != nil {
log.Fatal(err)
}
f1, err := os.Create(strconv.Itoa(lab) + ".phy")
if err != nil {
log.Fatal(err)
}
w1 := bufio.NewWriter(f1)
w := bufio.NewWriter(f)
dm := cophycollapse.SubDM(nodes, c)
out := cophycollapse.DMtoPhylip(dm, nodes)
fmt.Fprint(w, strings.Join(out, "\n"))
fmt.Fprint(w1, c.WriteClusterPhylip(nodes))
err = w1.Flush()
if err != nil {
log.Fatal(err)
}
f1.Close()
err = w.Flush()
if err != nil {
log.Fatal(err)
}
f.Close()
var strsites []string
for _, site := range c.Sites {
strsites = append(strsites, strconv.Itoa(site))
}
fmt.Fprint(w1, "CLUSTER"+strconv.Itoa(lab)+"\t"+strings.Join(strsites, "\t")+"\n")
}
err = w1.Flush()
if err != nil {
log.Fatal(err)
}
clmap.Close()
} else if *searchArg == 4 {
if *mclArg == "" {
fmt.Println("You need to specify a cluster input file to run this option")
os.Exit(1)
}
clusters := cophycollapse.ReadMCLoutput(*mclArg)
nodes := tree.PreorderArray()
clustSiteLikes := make(map[int][]float64)
f, err := os.Create(*runNameArg + ".tab")
if err != nil {
log.Fatal(err)
}
w := bufio.NewWriter(f)
for lab, c := range clusters {
for _, n := range nodes[1:] {
r := rand.Float64()
n.LEN = r
}
cophycollapse.ClusterMissingTraitsEM(tree, c, 10)
sitelikes := cophycollapse.SitewiseLogLike(tree)
clustSiteLikes[lab] = sitelikes
}
for lab, c := range clusters {
for _, site := range c.Sites {
assignLL := clustSiteLikes[lab][site]
totalDens := math.Exp(assignLL)
for kk, likes := range clustSiteLikes {
if kk != lab {
curlike := likes[site]
totalDens += math.Exp(curlike)
}
}
fmt.Println(strconv.FormatFloat(math.Exp(assignLL)/totalDens, 'f', -1, 64))
fmt.Fprintln(w, strconv.FormatFloat(math.Exp(assignLL)/totalDens, 'f', -1, 64))
}
}
err = w.Flush()
if err != nil {
log.Fatal(err)
}
f.Close()
}
}