-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
208 lines (180 loc) · 4.22 KB
/
project.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
// Copyright © 2023 J. Salvador Arias <jsalarias@gmail.com>
// All rights reserved.
// Distributed under BSD2 license that can be found in the LICENSE file.
// Package project implements reading and writing
// of PhyGeo project files.
//
// A PhyGeo project is a tab-delimited file (TSV)
// used to store the different data files
// required by PhyGeo commands.
package project
import (
"bufio"
"encoding/csv"
"errors"
"fmt"
"io"
"os"
"slices"
"strings"
"time"
)
// Dataset is a keyword to identify
// the type of a dataset file in a project.
type Dataset string
// Valid dataset types.
const (
// File for the plate motion model
// of the paleogeographic reconstruction.
GeoMotion Dataset = "geomotion"
// File for pixel prior probability values.
PixPrior Dataset = "pixprior"
// File for presence-absence points
// of the taxa in the project.
Points Dataset = "points"
// File for geographic distribution ranges
// of the taxa in the project.
Ranges Dataset = "ranges"
// File for the landscape pixel values
// at different time stages.
Landscape Dataset = "landscape"
// File for phylogenetic trees.
Trees Dataset = "trees"
)
// A Project represents a collection of paths
// for particular datasets.
type Project struct {
paths map[Dataset]string
}
// New creates a new empty project.
func New() *Project {
return &Project{
paths: make(map[Dataset]string),
}
}
var header = []string{
"dataset",
"path",
}
// Read reads a project file from a TSV file.
//
// The TSV must contain the following fields:
//
// - dataset, for the kind of file
// - path, for the path of the file
//
// Here is an example file:
//
// # phygeo project files
// dataset path
// geomotion geo-motion.tab
// pixprior pix-prior.tab
// ranges ranges.tab
// landscape landscape.tab
// trees trees.tab
func Read(name string) (*Project, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
tsv := csv.NewReader(f)
tsv.Comma = '\t'
tsv.Comment = '#'
head, err := tsv.Read()
if err != nil {
return nil, fmt.Errorf("on file %q: header: %v", name, err)
}
fields := make(map[string]int, len(head))
for i, h := range head {
h = strings.ToLower(h)
fields[h] = i
}
for _, h := range header {
if _, ok := fields[h]; !ok {
return nil, fmt.Errorf("on file %q: expecting field %q", name, h)
}
}
p := New()
for {
row, err := tsv.Read()
if errors.Is(err, io.EOF) {
break
}
ln, _ := tsv.FieldPos(0)
if err != nil {
return nil, fmt.Errorf("on file %q: on row %d: %v", name, ln, err)
}
f := "dataset"
s := Dataset(row[fields[f]])
f = "path"
path := row[fields[f]]
p.paths[s] = path
}
return p, nil
}
// Add adds a filepath of a dataset to a given project.
// It returns the previous value
// for the dataset.
func (p *Project) Add(set Dataset, path string) string {
prev := p.paths[set]
if path == "" {
delete(p.paths, set)
return prev
}
p.paths[set] = path
return prev
}
// Path returns the path of the given dataset.
func (p *Project) Path(set Dataset) string {
return p.paths[set]
}
// Sets returns the datasets defined on a project.
func (p *Project) Sets() []Dataset {
var sets []Dataset
for s := range p.paths {
sets = append(sets, s)
}
slices.Sort(sets)
return sets
}
// Write writes a project into a file with the indicated name.
func (p *Project) Write(name string) (err error) {
f, err := os.Create(name)
if err != nil {
return err
}
defer func() {
e := f.Close()
if e != nil && err == nil {
err = e
}
}()
bw := bufio.NewWriter(f)
fmt.Fprintf(bw, "# phygeo project files\n")
fmt.Fprintf(bw, "# data save on: %s\n", time.Now().Format(time.RFC3339))
tsv := csv.NewWriter(bw)
tsv.Comma = '\t'
tsv.UseCRLF = true
if err := tsv.Write(header); err != nil {
return fmt.Errorf("on file %q: while writing header: %v", name, err)
}
sets := p.Sets()
for _, s := range sets {
row := []string{
string(s),
p.paths[s],
}
if err := tsv.Write(row); err != nil {
return fmt.Errorf("on file %q: %v", name, err)
}
}
tsv.Flush()
if err := tsv.Error(); err != nil {
return fmt.Errorf("on file %q: while writing data: %v", name, err)
}
if err := bw.Flush(); err != nil {
return fmt.Errorf("on file %q: while writing data: %v", name, err)
}
return nil
}