/
main.go
212 lines (192 loc) · 6.43 KB
/
main.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
// Package phyloage calculates time estimates based on a phylogenetic tree.
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/yogischogi/phyloage/phylotree"
"github.com/yogischogi/phylofriend/genetic"
"github.com/yogischogi/phylofriend/genfiles"
)
func main() {
// Command line flags.
var (
treein = flag.String("treein", "", "Input filename for phylogenetic tree (.txt).")
treeout = flag.String("treeout", "", "Output filename for phylogenetic tree in TXT format.")
cal = flag.Float64("cal", 1, "Calibration factor for TMRCA calculation.")
offset = flag.Float64("offset", 0, "Offset is added to all calculated ages.")
topdown = flag.Bool("topdown", true, "Performs a top down recalculation.")
personsin = flag.String("personsin", "", "Input filename (.txt or .csv) or directory.")
mrin = flag.String("mrin", "", "Filename for the import of mutation rates.")
gentime = flag.Float64("gentime", 1, "Generation time in years.")
inspect = flag.String("inspect", "", "Comma separated list of SNP names to search for.")
statistics = flag.Bool("statistics", false, "Prints marker statistics.")
method = flag.String("method", "parsimony", "Method to calculate modal haplotypes: phylofriend or parsimony.")
stage = flag.Int("stage", 4, "Processing stage for parsimony algorithm: 1, 2, 3, 4.")
trace = flag.String("trace", "", "Comma separated list of STR names to print out trace information.")
subclade = flag.String("subclade", "", "Selects a specific branch of the tree.")
htmlout = flag.String("htmlout", "", "Output filename for persons in HTML format.")
model = flag.String("model", "hybrid", "Mutation model: hybrid or infinite.")
)
flag.Parse()
var (
persons []*genetic.Person
mutationRates genetic.YstrMarkers
stat *genetic.MarkerStatistics
err error
)
// Load phylogenetic tree from file.
if *treein == "" {
fmt.Printf("No filename for input tree specified.\r\n")
os.Exit(1)
}
tree, err := phylotree.NewFromFile(*treein)
if err != nil {
fmt.Printf("Error reading tree from file, %v.\r\n", err)
os.Exit(1)
}
// Select subclade.
if *subclade != "" {
tree = tree.Subclade(*subclade)
if tree == nil {
fmt.Printf("Error, could not find specified subclade %s.\r\n", *subclade)
os.Exit(1)
}
}
// Read mutation rates from file.
if *mrin != "" {
mutationRates, err = genfiles.ReadMutationRates(*mrin)
if err != nil {
fmt.Printf("Error reading mutation rates %v.\r\n", err)
os.Exit(1)
}
} else {
// Use default values.
mutationRates = genetic.DefaultMutationRates()
}
// Load genetic sample results.
if *personsin != "" {
filenames := strings.Split(*personsin, ",")
for _, filename := range filenames {
var pers []*genetic.Person
fileInfo, err := os.Stat(filename)
switch {
case err != nil:
fmt.Printf("Error, something is wrong with personsin, %v.\r\n", err)
os.Exit(1)
case fileInfo.IsDir():
pers, err = genfiles.ReadPersonsFromDir(filename)
case strings.HasSuffix(strings.ToLower(filename), ".csv"):
pers, err = genfiles.ReadPersonsFromCSV(filename, 0)
default:
pers, err = genfiles.ReadPersonsFromTXT(filename)
}
if err != nil {
fmt.Printf("Error loading persons data %v.\r\n", err)
os.Exit(1)
}
persons = append(persons, pers...)
}
tree.InsertPersons(persons)
// Calculate marker statistics.
if *statistics == true || *method == "parsimony" {
stat = genetic.NewStatistics(persons)
}
// Print marker statistics.
if *statistics == true {
fmt.Print(stat.String())
// XXX Temporary code.
// WriteToFile(stat)
}
var isInfiniteAlleles bool
switch *model {
case "infinite":
isInfiniteAlleles = true
case "hybrid":
isInfiniteAlleles = false
default:
fmt.Printf("Error, unknown mutation model: %s.\n", *model)
os.Exit(1)
}
// Calculate modal haplotypes.
switch *method {
case "phylofriend":
tree.CalculateModalHaplotypes()
case "parsimony":
tree.CalculateModalHaplotypesParsimony(stat, *stage, isInfiniteAlleles)
default:
fmt.Printf("Error, unknown method %q to calculate modal haplotypes.\r\n", *method)
os.Exit(1)
}
if isInfiniteAlleles == true {
tree.CalculateDistances(mutationRates, genetic.DistanceInfiniteAlleles)
} else {
tree.CalculateDistances(mutationRates, genetic.DistanceHybrid)
}
}
// Calculate the age of this clade and all subclades.
// If the STR-Count is provided in the original tree input
// file the calculation can be performed even without sample
// data.
tree.CalculateAge(*gentime, *cal, *offset)
// Top down recalculation for more realistic results.
if *topdown == true {
tree.RecalculateAge(*gentime, *cal, *offset)
}
// Save resulting tree to file or print it out.
if *treeout != "" {
date := time.Now().Format("2006 Jan 2")
var buffer bytes.Buffer
buffer.WriteString("// This tree was created by the phyloage program: https://github.com/yogischogi/phyloage\r\n")
buffer.WriteString("// Command used:\r\n// ")
for _, arg := range os.Args {
buffer.WriteString(arg)
buffer.WriteString(" ")
}
buffer.WriteString("\r\n")
buffer.WriteString("// " + date + "\r\n\r\n")
buffer.WriteString(tree.String())
err := ioutil.WriteFile(*treeout, buffer.Bytes(), os.ModePerm)
if err != nil {
fmt.Printf("Error writing tree to file, %v.\r\n", err)
os.Exit(1)
}
} else {
fmt.Printf("%v\r\n", tree)
}
// Write Persons' Y-STR values in HTML format.
if *htmlout != "" {
persons := tree.Persons()
err = genfiles.WritePersonsAsHTML(*htmlout, persons, genetic.MaxMarkers)
if err != nil {
fmt.Printf("Error writing persons data to HTML file, %v.\n", err)
}
}
// Print tree with values of specified STRs.
if *trace != "" {
snps := strings.Split(*trace, ",")
fmt.Printf("%s", tree.Trace(snps))
}
// Search for SNPs and print out information about the matching subclades.
if *inspect != "" {
searchTerms := strings.Split(*inspect, ",")
fmt.Printf("%s", tree.Inspect(searchTerms))
}
}
// XXX Temporary method to determine stable marker set.
func WriteToFile(statistics *genetic.MarkerStatistics) {
filename := "mutrates.txt"
minFreq := 1.0
nValuesMin := 1
nValuesMax := 5
stats := statistics.Select(minFreq, nValuesMin, nValuesMax)
err := ioutil.WriteFile(filename, []byte(stats.MutationRates()), os.ModePerm)
if err != nil {
fmt.Printf("Error writing mutation rates to file, %v.\r\n", err)
os.Exit(1)
}
}