Color PCA plot.
[lightning.git] / gvcf2numpy.go
1 package main
2
3 import (
4         "bufio"
5         "compress/gzip"
6         "flag"
7         "fmt"
8         "io"
9         "log"
10         "net/http"
11         _ "net/http/pprof"
12         "os"
13         "os/exec"
14         "path/filepath"
15         "regexp"
16         "runtime"
17         "sort"
18         "strings"
19         "sync"
20         "sync/atomic"
21         "time"
22
23         "github.com/kshedden/gonpy"
24 )
25
26 type gvcf2numpy struct {
27         tagLibraryFile string
28         refFile        string
29         output         io.Writer
30 }
31
32 func (cmd *gvcf2numpy) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
33         var err error
34         defer func() {
35                 if err != nil {
36                         fmt.Fprintf(stderr, "%s\n", err)
37                 }
38         }()
39         flags := flag.NewFlagSet("", flag.ContinueOnError)
40         flags.SetOutput(stderr)
41         flags.StringVar(&cmd.tagLibraryFile, "tag-library", "", "tag library fasta `file`")
42         flags.StringVar(&cmd.refFile, "ref", "", "reference fasta `file`")
43         pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
44         err = flags.Parse(args)
45         if err == flag.ErrHelp {
46                 err = nil
47                 return 0
48         } else if err != nil {
49                 return 2
50         } else if cmd.refFile == "" || cmd.tagLibraryFile == "" {
51                 fmt.Fprintln(os.Stderr, "cannot run without -tag-library and -ref arguments")
52                 return 2
53         } else if flags.NArg() == 0 {
54                 flags.Usage()
55                 return 2
56         }
57         cmd.output = stdout
58
59         if *pprof != "" {
60                 go func() {
61                         log.Println(http.ListenAndServe(*pprof, nil))
62                 }()
63         }
64
65         infiles, err := listInputFiles(flags.Args())
66         if err != nil {
67                 return 1
68         }
69
70         tilelib, err := cmd.loadTileLibrary()
71         if err != nil {
72                 return 1
73         }
74         go func() {
75                 for range time.Tick(10 * time.Second) {
76                         log.Printf("tilelib.Len() == %d", tilelib.Len())
77                 }
78         }()
79         variants, err := cmd.tileGVCFs(tilelib, infiles)
80         if err != nil {
81                 return 1
82         }
83         err = cmd.printVariants(variants)
84         if err != nil {
85                 return 1
86         }
87         return 0
88 }
89
90 func (cmd *gvcf2numpy) tileFasta(tilelib *tileLibrary, infile string) (tileSeq, error) {
91         var input io.ReadCloser
92         input, err := os.Open(infile)
93         if err != nil {
94                 return nil, err
95         }
96         defer input.Close()
97         if strings.HasSuffix(infile, ".gz") {
98                 input, err = gzip.NewReader(input)
99                 if err != nil {
100                         return nil, err
101                 }
102                 defer input.Close()
103         }
104         return tilelib.TileFasta(infile, input)
105 }
106
107 func (cmd *gvcf2numpy) loadTileLibrary() (*tileLibrary, error) {
108         log.Printf("tag library %s load starting", cmd.tagLibraryFile)
109         f, err := os.Open(cmd.tagLibraryFile)
110         if err != nil {
111                 return nil, err
112         }
113         defer f.Close()
114         var rdr io.ReadCloser = f
115         if strings.HasSuffix(cmd.tagLibraryFile, ".gz") {
116                 rdr, err = gzip.NewReader(f)
117                 if err != nil {
118                         return nil, fmt.Errorf("%s: gzip: %s", cmd.tagLibraryFile, err)
119                 }
120                 defer rdr.Close()
121         }
122         var taglib tagLibrary
123         err = taglib.Load(rdr)
124         if err != nil {
125                 return nil, err
126         }
127         if taglib.Len() < 1 {
128                 return nil, fmt.Errorf("cannot tile: tag library is empty")
129         }
130         log.Printf("tag library %s load done", cmd.tagLibraryFile)
131         return &tileLibrary{taglib: &taglib}, nil
132 }
133
134 func listInputFiles(paths []string) (files []string, err error) {
135         for _, path := range paths {
136                 if fi, err := os.Stat(path); err != nil {
137                         return nil, fmt.Errorf("%s: stat failed: %s", path, err)
138                 } else if !fi.IsDir() {
139                         if !strings.HasSuffix(path, ".2.fasta") || strings.HasSuffix(path, ".2.fasta.gz") {
140                                 files = append(files, path)
141                         }
142                         continue
143                 }
144                 d, err := os.Open(path)
145                 if err != nil {
146                         return nil, fmt.Errorf("%s: open failed: %s", path, err)
147                 }
148                 defer d.Close()
149                 names, err := d.Readdirnames(0)
150                 if err != nil {
151                         return nil, fmt.Errorf("%s: readdir failed: %s", path, err)
152                 }
153                 sort.Strings(names)
154                 for _, name := range names {
155                         if strings.HasSuffix(name, ".vcf") || strings.HasSuffix(name, ".vcf.gz") {
156                                 files = append(files, filepath.Join(path, name))
157                         } else if strings.HasSuffix(name, ".1.fasta") || strings.HasSuffix(name, ".1.fasta.gz") {
158                                 files = append(files, filepath.Join(path, name))
159                         }
160                 }
161                 d.Close()
162         }
163         for _, file := range files {
164                 if strings.HasSuffix(file, ".1.fasta") || strings.HasSuffix(file, ".1.fasta.gz") {
165                         continue
166                 } else if _, err := os.Stat(file + ".csi"); err == nil {
167                         continue
168                 } else if _, err = os.Stat(file + ".tbi"); err == nil {
169                         continue
170                 } else {
171                         return nil, fmt.Errorf("%s: cannot read without .tbi or .csi index file", file)
172                 }
173         }
174         return
175 }
176
177 func (cmd *gvcf2numpy) tileGVCFs(tilelib *tileLibrary, infiles []string) ([][]tileVariantID, error) {
178         starttime := time.Now()
179         errs := make(chan error, 1)
180         variants := make([][]tileVariantID, len(infiles)*2)
181         todo := make(chan func() error, len(infiles)*2)
182         var wg sync.WaitGroup
183         for i, infile := range infiles {
184                 i, infile := i, infile
185                 if strings.HasSuffix(infile, ".1.fasta") || strings.HasSuffix(infile, ".1.fasta.gz") {
186                         todo <- func() error {
187                                 log.Printf("%s starting", infile)
188                                 defer log.Printf("%s done", infile)
189                                 tseqs, err := cmd.tileFasta(tilelib, infile)
190                                 variants[i*2] = tseqs.Variants()
191                                 return err
192                         }
193                         infile2 := regexp.MustCompile(`\.1\.fasta(\.gz)?$`).ReplaceAllString(infile, `.2.fasta$1`)
194                         todo <- func() error {
195                                 log.Printf("%s starting", infile2)
196                                 defer log.Printf("%s done", infile2)
197                                 tseqs, err := cmd.tileFasta(tilelib, infile2)
198                                 variants[i*2+1] = tseqs.Variants()
199                                 return err
200                         }
201                 } else {
202                         for phase := 0; phase < 2; phase++ {
203                                 phase := phase
204                                 todo <- func() error {
205                                         log.Printf("%s phase %d starting", infile, phase+1)
206                                         defer log.Printf("%s phase %d done", infile, phase+1)
207                                         tseqs, err := cmd.tileGVCF(tilelib, infile, phase)
208                                         variants[i*2+phase] = tseqs.Variants()
209                                         return err
210                                 }
211                         }
212                 }
213         }
214         go close(todo)
215         running := int64(runtime.NumCPU())
216         for i := 0; i < runtime.NumCPU(); i++ {
217                 wg.Add(1)
218                 go func() {
219                         defer wg.Done()
220                         defer atomic.AddInt64(&running, -1)
221                         for fn := range todo {
222                                 if len(errs) > 0 {
223                                         return
224                                 }
225                                 err := fn()
226                                 if err != nil {
227                                         select {
228                                         case errs <- err:
229                                         default:
230                                         }
231                                 }
232                                 remain := len(todo) + int(atomic.LoadInt64(&running)) - 1
233                                 ttl := time.Now().Sub(starttime) * time.Duration(remain) / time.Duration(cap(todo)-remain)
234                                 eta := time.Now().Add(ttl)
235                                 log.Printf("progress %d/%d, eta %v (%v)", cap(todo)-remain, cap(todo), eta, ttl)
236                         }
237                 }()
238         }
239         wg.Wait()
240         go close(errs)
241         return variants, <-errs
242 }
243
244 func (cmd *gvcf2numpy) printVariants(variants [][]tileVariantID) error {
245         maxlen := 0
246         for _, v := range variants {
247                 if maxlen < len(v) {
248                         maxlen = len(v)
249                 }
250         }
251         rows := len(variants) / 2
252         cols := maxlen * 2
253         out := make([]uint16, rows*cols)
254         for row := 0; row < len(variants)/2; row++ {
255                 for phase := 0; phase < 2; phase++ {
256                         for tag, variant := range variants[row*2+phase] {
257                                 out[row*cols+2*int(tag)+phase] = uint16(variant)
258                         }
259                 }
260         }
261         w := bufio.NewWriter(cmd.output)
262         npw, err := gonpy.NewWriter(nopCloser{w})
263         if err != nil {
264                 return err
265         }
266         npw.Shape = []int{rows, cols}
267         npw.WriteUint16(out)
268         return w.Flush()
269 }
270
271 type nopCloser struct {
272         io.Writer
273 }
274
275 func (nopCloser) Close() error { return nil }
276
277 func (cmd *gvcf2numpy) tileGVCF(tilelib *tileLibrary, infile string, phase int) (tileseq tileSeq, err error) {
278         args := []string{"bcftools", "consensus", "--fasta-ref", cmd.refFile, "-H", fmt.Sprint(phase + 1), infile}
279         indexsuffix := ".tbi"
280         if _, err := os.Stat(infile + ".csi"); err == nil {
281                 indexsuffix = ".csi"
282         }
283         if out, err := exec.Command("docker", "image", "ls", "-q", "lightning-runtime").Output(); err == nil && len(out) > 0 {
284                 args = append([]string{
285                         "docker", "run", "--rm",
286                         "--log-driver=none",
287                         "--volume=" + infile + ":" + infile + ":ro",
288                         "--volume=" + infile + indexsuffix + ":" + infile + indexsuffix + ":ro",
289                         "--volume=" + cmd.refFile + ":" + cmd.refFile + ":ro",
290                         "lightning-runtime",
291                 }, args...)
292         }
293         consensus := exec.Command(args[0], args[1:]...)
294         consensus.Stderr = os.Stderr
295         stdout, err := consensus.StdoutPipe()
296         defer stdout.Close()
297         if err != nil {
298                 return
299         }
300         err = consensus.Start()
301         if err != nil {
302                 return
303         }
304         defer consensus.Wait()
305         tileseq, err = tilelib.TileFasta(fmt.Sprintf("%s phase %d", infile, phase+1), stdout)
306         if err != nil {
307                 return
308         }
309         err = stdout.Close()
310         if err != nil {
311                 return
312         }
313         err = consensus.Wait()
314         if err != nil {
315                 err = fmt.Errorf("%s phase %d: bcftools: %s", infile, phase, err)
316                 return
317         }
318         return
319 }