19 "github.com/kshedden/gonpy"
22 type gvcf2numpy struct {
28 func (cmd *gvcf2numpy) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
32 fmt.Fprintf(stderr, "%s\n", err)
35 flags := flag.NewFlagSet("", flag.ContinueOnError)
36 flags.SetOutput(stderr)
37 flags.StringVar(&cmd.tagLibraryFile, "tag-library", "", "tag library fasta `file`")
38 flags.StringVar(&cmd.refFile, "ref", "", "reference fasta `file`")
39 err = flags.Parse(args)
40 if err == flag.ErrHelp {
43 } else if err != nil {
45 } else if cmd.refFile == "" || cmd.tagLibraryFile == "" {
46 fmt.Fprintln(os.Stderr, "cannot run without -tag-library and -ref arguments")
48 } else if flags.NArg() == 0 {
54 infiles, err := listVCFFiles(flags.Args())
59 log.Printf("tag library %s load starting", cmd.tagLibraryFile)
60 f, err := os.Open(cmd.tagLibraryFile)
64 var rdr io.ReadCloser = f
65 if strings.HasSuffix(cmd.tagLibraryFile, ".gz") {
66 rdr, err = gzip.NewReader(f)
68 err = fmt.Errorf("%s: gzip: %s", cmd.tagLibraryFile, err)
73 err = taglib.Load(rdr)
78 err = fmt.Errorf("cannot tile: tag library is empty")
81 log.Printf("tag library %s load done", cmd.tagLibraryFile)
83 tilelib := tileLibrary{taglib: &taglib}
85 for range time.Tick(10 * time.Second) {
86 log.Printf("tilelib.Len() == %d", tilelib.Len())
89 tseqs, err := cmd.tileGVCFs(&tilelib, infiles)
93 err = cmd.printVariants(tseqs)
100 func listVCFFiles(paths []string) (files []string, err error) {
101 for _, path := range paths {
102 if fi, err := os.Stat(path); err != nil {
103 return nil, fmt.Errorf("%s: stat failed: %s", path, err)
104 } else if !fi.IsDir() {
105 files = append(files, path)
108 d, err := os.Open(path)
110 return nil, fmt.Errorf("%s: open failed: %s", path, err)
113 names, err := d.Readdirnames(0)
115 return nil, fmt.Errorf("%s: readdir failed: %s", path, err)
118 for _, name := range names {
119 if strings.HasSuffix(name, ".vcf") || strings.HasSuffix(name, ".vcf.gz") {
120 files = append(files, filepath.Join(path, name))
125 for _, file := range files {
126 if _, err := os.Stat(file + ".csi"); err == nil {
128 } else if _, err = os.Stat(file + ".tbi"); err == nil {
131 return nil, fmt.Errorf("%s: cannot read without .tbi or .csi index file", file)
137 func (cmd *gvcf2numpy) tileGVCFs(tilelib *tileLibrary, infiles []string) ([]tileSeq, error) {
138 starttime := time.Now()
139 errs := make(chan error, 1)
140 tseqs := make([]tileSeq, len(infiles)*2)
141 todo := make(chan func() error, len(infiles)*2)
142 var wg sync.WaitGroup
143 for i, infile := range infiles {
144 for phase := 0; phase < 2; phase++ {
145 i, infile, phase := i, infile, phase
146 todo <- func() (err error) {
147 log.Printf("%s phase %d starting", infile, phase+1)
148 defer log.Printf("%s phase %d done", infile, phase+1)
149 tseqs[i*2+phase], err = cmd.tileGVCF(tilelib, infile, phase)
155 for i := 0; i < runtime.NumCPU(); i++ {
159 for fn := range todo {
171 ttl := time.Now().Sub(starttime) * time.Duration(remain) / time.Duration(cap(todo)-remain)
172 eta := time.Now().Add(ttl)
173 log.Printf("progress %d/%d, eta %v (%v)", cap(todo)-remain, cap(todo), eta, ttl)
182 func (cmd *gvcf2numpy) printVariants(tseqs []tileSeq) error {
184 for _, tseq := range tseqs {
185 for _, path := range tseq {
186 for _, tvar := range path {
187 if maxtag < tvar.tag {
193 out := make([]uint16, len(tseqs)*int(maxtag+1))
194 for i := 0; i < len(tseqs)/2; i++ {
195 for phase := 0; phase < 2; phase++ {
196 for _, path := range tseqs[i*2+phase] {
197 for _, tvar := range path {
198 out[2*int(tvar.tag)+phase] = uint16(tvar.variant)
203 w := bufio.NewWriter(cmd.output)
204 npw, err := gonpy.NewWriter(nopCloser{w})
208 npw.Shape = []int{len(tseqs) / 2, 2 * (int(maxtag) + 1)}
213 type nopCloser struct {
217 func (nopCloser) Close() error { return nil }
219 func (cmd *gvcf2numpy) tileGVCF(tilelib *tileLibrary, infile string, phase int) (tileseq tileSeq, err error) {
220 args := []string{"bcftools", "consensus", "--fasta-ref", cmd.refFile, "-H", fmt.Sprint(phase + 1), infile}
221 indexsuffix := ".tbi"
222 if _, err := os.Stat(infile + ".csi"); err == nil {
225 if out, err := exec.Command("docker", "image", "ls", "-q", "lightning-runtime").Output(); err == nil && len(out) > 0 {
226 args = append([]string{
227 "docker", "run", "--rm",
229 "--volume=" + infile + ":" + infile + ":ro",
230 "--volume=" + infile + indexsuffix + ":" + infile + indexsuffix + ":ro",
231 "--volume=" + cmd.refFile + ":" + cmd.refFile + ":ro",
235 consensus := exec.Command(args[0], args[1:]...)
236 consensus.Stderr = os.Stderr
237 stdout, err := consensus.StdoutPipe()
241 err = consensus.Start()
245 tileseq, err = tilelib.TileFasta(fmt.Sprintf("%s phase %d", infile, phase+1), stdout)
253 err = consensus.Wait()
255 err = fmt.Errorf("%s phase %d: bcftools: %s", infile, phase, err)