-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseqLoGo.go
More file actions
260 lines (235 loc) · 5 KB
/
Copy pathseqLoGo.go
File metadata and controls
260 lines (235 loc) · 5 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
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
// ----------------------------
// Copyright (C) 2014 Carushi
// ----------------------------
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 3 as published by the Free Software Foundation.
// ----------------------------
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
)
var (
ifile = flag.String("input_file", "", "read sequences from input_file (defaults: Stdin)")
gcflag = flag.Bool("gc", false, "print gc contents")
anyCharType = flag.Bool("any", false, "count any characters")
readFasta = flag.Bool("fasta", false, "read fasta format")
strNum = flag.Int("str", 0, "compress all sequences and print only a number of optimized strNum strings")
)
// window is the initial capacity of table.
const window = 200
var baseToIndex = map[uint8]int{
'A': 1,
'C': 2,
'G': 3,
'T': 4,
'a': 1,
'c': 2,
'g': 3,
't': 4,
'U': 4,
'u': 4,
}
var indexToBase = map[int]uint8{
0: '-',
1: 'A',
2: 'C',
3: 'G',
4: 'T',
}
type table struct {
seqCount int
charMatrix [][]int
maxCharType int
}
func (tab *table) isBase() bool {
return (tab.maxCharType == 5)
}
func (tab *table) setLength(nrow int) {
if nrow >= len(tab.charMatrix) {
narr := make([][]int, nrow)
copy(narr, tab.charMatrix)
for i := len(tab.charMatrix); i < nrow; i++ {
narr[i] = make([]int, tab.maxCharType)
}
tab.charMatrix = narr
}
}
func sum(array []int) (a int) {
for _, s := range array {
a += s
}
return
}
func (tab *table) addChar(r int, l int) {
tab.charMatrix[r][l]++
}
func (tab *table) addSequence(str string) {
tab.setLength(len(str))
for i := len(str) - 1; i >= 0; i-- {
if tab.isBase() {
tab.addChar(i, baseToIndex[str[i]])
} else {
tab.addChar(i, int(str[i]))
}
}
tab.seqCount++
}
func (tab *table) printGCcontents() {
fmt.Printf("GC%%")
for _, temp := range tab.charMatrix {
fmt.Printf("\t%f", (float64(temp[2]+temp[3]) * 100.0 / float64(sum(temp))))
}
fmt.Println("")
}
func (tab *table) getAppearedChar() []int {
charList := make([]int, 0, tab.maxCharType)
for i := 0; i < tab.maxCharType; i++ {
for _, temp := range tab.charMatrix {
if temp[i] == 0 {
continue
}
charList = append(charList, i)
break
}
}
return charList
}
func (tab *table) printHead() []int {
if tab.isBase() {
fmt.Println("index\tN\tA\tC\tG\tT")
return []int{0, 1, 2, 3, 4}
}
charList := tab.getAppearedChar()
fmt.Printf("index")
for _, i := range charList {
fmt.Printf("\t%v", string(i))
}
fmt.Println("")
return charList
}
func (tab *table) printCounts() {
charList := tab.printHead()
for i, temp := range tab.charMatrix {
fmt.Printf("%v", i)
for _, index := range charList {
fmt.Printf("\t%v", temp[index])
}
fmt.Println("")
}
}
func (tab *table) extractSeq(freq [][]int) (string, [][]int) {
chars := make([]uint8, len(freq))
for i, temp := range freq {
chars[i] = uint8('-')
for j := range temp {
if temp[j] == 0 {
continue
}
if tab.isBase() {
chars[i] = indexToBase[j]
} else {
chars[i] = uint8(j)
}
temp[j]--
break
}
}
return string(chars), freq
}
func min(a int, b int) int {
if a > b {
return b
}
return a
}
func (tab *table) getCompressedFreqTable(count int) [][]int {
nmat := make([][]int, len(tab.charMatrix))
copy(nmat, tab.charMatrix)
for _, temp := range nmat {
for j := range temp {
temp[j] = (count * temp[j]) / tab.seqCount
}
}
return nmat
}
func (tab *table) printStrings(strNum int) { // printStrings
count := min(strNum, tab.seqCount)
freq := tab.getCompressedFreqTable(count)
for i, str := 0, ""; i < count; i++ {
str, freq = tab.extractSeq(freq)
fmt.Println(str)
}
}
func (tab *table) output(gcflag bool, strNum int) {
if gcflag {
if tab.isBase() {
tab.printGCcontents()
} else {
fmt.Println("Error: Cannot apply --gc with --any.")
}
} else if strNum > 0 {
tab.printStrings(strNum)
} else {
tab.printCounts()
// DrawLoGo(tab.charMatrix, "")
}
}
func scanFasta(scanner (*bufio.Scanner), tab *table) error {
str := ""
for scanner.Scan() {
tstr := scanner.Text()
if len(tstr) > 0 && tstr[0] == '>' {
tab.addSequence(str)
str = ""
continue
}
str += tstr
}
tab.addSequence(str)
return scanner.Err()
}
func scanText(ifile string, readFasta bool, tab *table) error {
var fp *os.File
var err error
if len(ifile) > 0 {
if fp, err = os.Open(ifile); err != nil {
return err
}
defer fp.Close()
} else {
fp = os.Stdin
}
scanner := bufio.NewScanner(fp)
if readFasta {
return scanFasta(scanner, tab)
}
for scanner.Scan() {
tab.addSequence(scanner.Text())
}
return scanner.Err()
}
func newTable(anyCharType bool) *table {
if anyCharType {
return &table{
charMatrix: make([][]int, 0, window),
maxCharType: 256,
}
}
return &table{
charMatrix: make([][]int, 0, window),
maxCharType: 5,
}
}
func main() {
flag.Parse()
tab := newTable(*anyCharType)
if err := scanText(*ifile, *readFasta, tab); err != nil {
log.Fatal(err)
}
tab.output(*gcflag, *strNum)
}