SeqCombGo/parser.go

91 lines
1.6 KiB
Go
Raw Normal View History

2022-01-01 16:21:41 +08:00
package main
import (
"flag"
"fmt"
2022-01-03 12:38:07 +08:00
"gocomb/src"
2022-01-01 16:21:41 +08:00
"io/ioutil"
2022-01-03 12:38:07 +08:00
"os"
"text/template"
2022-01-01 16:21:41 +08:00
)
2022-01-03 12:38:07 +08:00
type Person struct {
Name string
DNA string
}
2022-01-01 16:21:41 +08:00
func main() {
2022-01-03 12:38:07 +08:00
file_flag := flag.String("o", "a.nex", "files name wait to out")
2022-01-01 16:21:41 +08:00
// 这里一定要是指针样子
flag.Parse()
file_names := flag.Args() // []string{"foo", "bar"}
// file_out := ""
fmt.Println("输出在这里", *file_flag)
2022-01-03 12:38:07 +08:00
nex_tmpl, err := template.New("nex").Parse(nex_tmpl.Nex_tmpl)
if err != nil {
panic("tmpl err")
}
new_file, err := os.OpenFile("a.nex", os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Println("open file error :", err)
return
}
defer new_file.Close()
2022-01-01 16:21:41 +08:00
for _, v := range file_names {
2022-01-03 12:38:07 +08:00
new_nex := read(v)
err := nex_tmpl.Execute(new_file, new_nex)
if err != nil {
fmt.Println("err at tmpl exec", err)
}
2022-01-01 16:21:41 +08:00
}
}
2022-01-03 12:38:07 +08:00
func read(file_name string) map[string]string {
2022-01-01 16:21:41 +08:00
f, err := ioutil.ReadFile("./" + file_name)
if err != nil {
fmt.Println(err)
2022-01-03 12:38:07 +08:00
return nil
2022-01-01 16:21:41 +08:00
}
// fmt.Println(f)
i := 0 // DNA行计数
j := 0 // 非序列行计数
seq := make(map[string]string)
section := ""
// fmt.Println('a', 'c', 'g', 't', '-', '\n', '\r')
for k, v := range f {
switch v {
case 'a', 'c', 'g', 't', '-':
if j != 0 {
continue
}
if i == 0 {
i = k
}
case '\n':
if i != 0 {
seq[section] = seq[section] + string(f[i:k])
i = 0
continue
}
section = string(f[j:k])
j = 0
default:
if j == 0 {
j = k + 1
}
}
}
2022-01-03 12:38:07 +08:00
// for k1, v1 := range seq {
// fmt.Println(k1)
// fmt.Println(v1)
// }
return seq
2022-01-01 16:21:41 +08:00
}