Repository navigation
Expand file tree
/
Copy pathexperiment.go
More file actions
218 lines (175 loc) · 5.39 KB
/
Copy pathexperiment.go
File metadata and controls
218 lines (175 loc) · 5.39 KB
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
package csvtoprism
import (
"bytes"
"encoding/csv"
"errors"
"fmt"
"io"
"strconv"
"strings"
"github.com/tealeg/xlsx"
)
func round(num float64) int {
if num < 0.0 {
return int(num - 0.5)
}
return int(num + 0.5)
}
// A RawSample is a set of data points associated with some identifier
type RawSample struct {
Name string `json:"name"`
Data [3]int `json:"data"`
}
// ParseRawSamplesXLS constructs a set of RawSamples from the given filename.
// It is epxected that the stream provided by the io.ReaderAt is in the format of an XLS file.
func ParseRawSamplesXLS(r io.ReaderAt, size int64) ([]RawSample, error) {
var buf bytes.Buffer
err := generateCSVFromXLSXReader(r, size, 0, &buf)
if err != nil {
return nil, err
}
return ParseRawSamplesCSV(&buf)
}
// ParseRawSamplesCSV constructs a set of RawSamples using data provided by the given io.Reader.
// It is expected that the stream provided by the io.Reader is in the format of a CSV file.
func ParseRawSamplesCSV(r io.Reader) ([]RawSample, error) {
data, identifiers, err := parseInputData(r)
if err != nil {
return nil, err
}
return parseRawSamplesFromRawData(data, identifiers)
}
// Adjust returns a new RawSample with the data points adjusted by the
// provided mean
func (r RawSample) Adjust(mean int) RawSample {
var adjusted RawSample
adjusted.Name = r.Name
for i := 0; i < 3; i++ {
adjusted.Data[i] = r.Data[i] - mean
}
return adjusted
}
// A ControlledSample is a set of RawSamples associated with a particular RawSample
// that represents the control group.
type ControlledSample struct {
Control RawSample `json:"control"`
Experimental []RawSample `json:"experimental"`
}
// Adjust returns a new ControlledSample where all of the data points
// are adjusted by the mean of the Control's data points.
func (c ControlledSample) Adjust() ControlledSample {
var adjusted ControlledSample
fmean := float64(c.Control.Data[0]+c.Control.Data[1]+c.Control.Data[2]) / 3.0
mean := round(fmean)
adjusted.Control = c.Control.Adjust(mean)
for _, e := range c.Experimental {
adjusted.Experimental = append(adjusted.Experimental, e.Adjust(mean))
}
return adjusted
}
// An Experiment consists of a name, generally representing the donor source,
// and a set of ControlledSamples.
type Experiment struct {
Name string `json:"name"`
Samples []ControlledSample `json:"samples"`
}
// Adjusted produces a new Experiment where all of the samples have had their data
// points adjusted by the mean of their control.
func (e Experiment) Adjusted() Experiment {
var adjusted Experiment
adjusted.Name = fmt.Sprintf("%s_Adjusted", e.Name)
for _, s := range e.Samples {
adjusted.Samples = append(adjusted.Samples, s.Adjust())
}
return adjusted
}
// WriteCSV dumps the experiment in CSV format suitable for import into Prism using the
// provided io.Writer
func (e Experiment) WriteCSV(w io.Writer) error {
outputFormat := make([][]string, 4)
outputFormat[0] = make([]string, 32)
outputFormat[1] = make([]string, 32)
outputFormat[2] = make([]string, 32)
outputFormat[3] = make([]string, 32)
col := 0
for _, sample := range e.Samples {
// First write the control
outputFormat[0][col] = sample.Control.Name
outputFormat[1][col] = strconv.Itoa(sample.Control.Data[0])
outputFormat[2][col] = strconv.Itoa(sample.Control.Data[1])
outputFormat[3][col] = strconv.Itoa(sample.Control.Data[2])
col++
// Now write the data poitns
for _, s := range sample.Experimental {
outputFormat[0][col] = s.Name
outputFormat[1][col] = strconv.Itoa(s.Data[0])
outputFormat[2][col] = strconv.Itoa(s.Data[1])
outputFormat[3][col] = strconv.Itoa(s.Data[2])
col++
}
}
cw := csv.NewWriter(w)
err := cw.WriteAll(outputFormat)
if err != nil {
return err
}
cw.Flush()
return nil
}
// ParseRawSamplesFromRawData takes flat slice representations of the data and
// sample identifiers and joins them together.
func parseRawSamplesFromRawData(data []int, identifiers []string) ([]RawSample, error) {
if len(data)%3 != 0 {
return nil, errors.New("must have a multiple of 3 data points")
}
samples := make([]RawSample, 0)
dCount := 0
for _, identifier := range identifiers {
var s RawSample
s.Name = identifier
for j := 0; j < 3; j++ {
s.Data[j] = data[dCount]
dCount++
}
samples = append(samples, s)
}
return samples, nil
}
func generateCSVFromXLSXReader(r io.ReaderAt, size int64, sheetIndex int, w io.Writer) error {
xlFile, err := xlsx.OpenReaderAt(r, size)
if err != nil {
return err
}
return generateCSV(xlFile, sheetIndex, w)
}
func generateCSVFromXLSXFile(excelFileName string, sheetIndex int, w io.Writer) error {
xlFile, err := xlsx.OpenFile(excelFileName)
if err != nil {
return err
}
return generateCSV(xlFile, sheetIndex, w)
}
func generateCSV(f *xlsx.File, sheetIndex int, w io.Writer) error {
sheetLen := len(f.Sheets)
switch {
case sheetLen == 0:
return errors.New("This XLSX file contains no sheets.")
case sheetIndex >= sheetLen:
return fmt.Errorf("No sheet %d available, please select a sheet between 0 and %d\n", sheetIndex, sheetLen-1)
}
sheet := f.Sheets[sheetIndex]
for _, row := range sheet.Rows {
var vals []string
if row != nil {
for _, cell := range row.Cells {
str, err := cell.String()
if err != nil {
vals = append(vals, err.Error())
}
vals = append(vals, fmt.Sprintf("%s", str))
}
fmt.Fprintf(w, strings.Join(vals, ",")+"\n")
}
}
return nil
}