尝试输出到nex

This commit is contained in:
qaqland 2022-01-03 12:38:07 +08:00
parent 9f1bd76e27
commit fdd99fbb04
3 changed files with 55 additions and 9 deletions

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module gocomb
go 1.17

View file

@ -3,27 +3,54 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"gocomb/src"
"io/ioutil" "io/ioutil"
"os"
"text/template"
) )
type Person struct {
Name string
DNA string
}
func main() { func main() {
file_flag := flag.String("o", "a.ast", "files name wait to out")
file_flag := flag.String("o", "a.nex", "files name wait to out")
// 这里一定要是指针样子 // 这里一定要是指针样子
flag.Parse() flag.Parse()
file_names := flag.Args() // []string{"foo", "bar"} file_names := flag.Args() // []string{"foo", "bar"}
// file_out := "" // file_out := ""
fmt.Println("输出在这里", *file_flag) fmt.Println("输出在这里", *file_flag)
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()
for _, v := range file_names { for _, v := range file_names {
// fmt.Println(k, v) new_nex := read(v)
read(v) err := nex_tmpl.Execute(new_file, new_nex)
if err != nil {
fmt.Println("err at tmpl exec", err)
}
} }
} }
func read(file_name string) { func read(file_name string) map[string]string {
f, err := ioutil.ReadFile("./" + file_name) f, err := ioutil.ReadFile("./" + file_name)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return nil
} }
// fmt.Println(f) // fmt.Println(f)
@ -55,8 +82,9 @@ func read(file_name string) {
} }
} }
} }
for k1, v1 := range seq { // for k1, v1 := range seq {
fmt.Println(k1) // fmt.Println(k1)
fmt.Println(v1) // fmt.Println(v1)
} // }
return seq
} }

15
src/nex_tmpl.go Normal file
View file

@ -0,0 +1,15 @@
package nex_tmpl
const Nex_tmpl = `#NEXUS
BEGIN DATA;
DIMENSIONS NTAX={} NCHAR={};
FORMAT DATATYPE=DNA GAP=- MISSING=? ; #DATATYPE={}
MATRIX{{ range $k, $v := . }}
'{{ $k }}' {{ $v }}{{ end }}
;
END;
BEGIN SETS;
CHARSET 16S = 1-811;
CHARSET CO1 = 812-1421;
END;
`