16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 "github.com/james-bowman/nlp"
18 "github.com/kshedden/gonpy"
19 log "github.com/sirupsen/logrus"
20 "gonum.org/v1/gonum/mat"
23 type pythonPCA struct{}
25 func (cmd *pythonPCA) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
29 fmt.Fprintf(stderr, "%s\n", err)
32 flags := flag.NewFlagSet("", flag.ContinueOnError)
33 flags.SetOutput(stderr)
34 projectUUID := flags.String("project", "", "project `UUID` for output data")
35 inputFilename := flags.String("i", "-", "input `file`")
36 priority := flags.Int("priority", 500, "container request priority")
37 err = flags.Parse(args)
38 if err == flag.ErrHelp {
41 } else if err != nil {
45 runner := arvadosContainerRunner{
46 Name: "lightning pca",
47 Client: arvados.NewClientFromEnv(),
48 ProjectUUID: *projectUUID,
53 err = runner.TranslatePaths(inputFilename)
57 runner.Prog = "python3"
58 runner.Args = []string{"-c", `import sys
60 from sklearn.decomposition import PCA
61 scipy.save(sys.argv[2], PCA(n_components=4).fit_transform(scipy.load(sys.argv[1])))`, *inputFilename, "/mnt/output/pca.npy"}
63 output, err = runner.Run()
67 fmt.Fprintln(stdout, output+"/pca.npy")
75 func (cmd *goPCA) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
79 fmt.Fprintf(stderr, "%s\n", err)
82 flags := flag.NewFlagSet("", flag.ContinueOnError)
83 flags.SetOutput(stderr)
84 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
85 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
86 projectUUID := flags.String("project", "", "project `UUID` for output data")
87 priority := flags.Int("priority", 500, "container request priority")
88 inputFilename := flags.String("i", "-", "input `file`")
89 outputFilename := flags.String("o", "-", "output `file`")
90 components := flags.Int("components", 4, "number of components")
91 onehot := flags.Bool("one-hot", false, "recode tile variants as one-hot")
92 cmd.filter.Flags(flags)
93 err = flags.Parse(args)
94 if err == flag.ErrHelp {
97 } else if err != nil {
103 log.Println(http.ListenAndServe(*pprof, nil))
108 if *outputFilename != "-" {
109 err = errors.New("cannot specify output file in container mode: not implemented")
112 runner := arvadosContainerRunner{
113 Name: "lightning pca-go",
114 Client: arvados.NewClientFromEnv(),
115 ProjectUUID: *projectUUID,
116 RAM: 300000000000, // maybe 10x input size?
120 err = runner.TranslatePaths(inputFilename)
124 runner.Args = []string{"pca-go", "-local=true", fmt.Sprintf("-one-hot=%v", *onehot), "-i", *inputFilename, "-o", "/mnt/output/pca.npy"}
125 runner.Args = append(runner.Args, cmd.filter.Args()...)
127 output, err = runner.Run()
131 fmt.Fprintln(stdout, output+"/pca.npy")
135 var input io.ReadCloser
136 if *inputFilename == "-" {
137 input = ioutil.NopCloser(stdin)
139 input, err = os.Open(*inputFilename)
146 tilelib := &tileLibrary{
148 compactGenomes: map[string][]tileVariantID{},
150 err = tilelib.LoadGob(context.Background(), input, strings.HasSuffix(*inputFilename, ".gz"), nil)
159 log.Info("filtering")
160 cmd.filter.Apply(tilelib)
164 log.Print("converting cgs to array")
165 data, rows, cols := cgs2array(tilelib, cgnames(tilelib), lowqual(tilelib), nil, 0, len(tilelib.variant))
167 log.Printf("recode one-hot: %d rows, %d cols", rows, cols)
168 data, _, cols = recodeOnehot(data, cols)
172 log.Printf("creating matrix backed by array: %d rows, %d cols", rows, cols)
173 mtx := array2matrix(rows, cols, data).T()
176 transformer := nlp.NewPCA(*components)
178 log.Printf("transforming")
179 mtx, err = transformer.Transform(mtx)
185 rows, cols = mtx.Dims()
186 log.Printf("copying result to numpy output array: %d rows, %d cols", rows, cols)
187 out := make([]float64, rows*cols)
188 for i := 0; i < rows; i++ {
189 for j := 0; j < cols; j++ {
190 out[i*cols+j] = mtx.At(i, j)
194 var output io.WriteCloser
195 if *outputFilename == "-" {
196 output = nopCloser{stdout}
198 output, err = os.OpenFile(*outputFilename, os.O_CREATE|os.O_WRONLY, 0777)
204 bufw := bufio.NewWriter(output)
205 npw, err := gonpy.NewWriter(nopCloser{bufw})
209 npw.Shape = []int{rows, cols}
210 log.Printf("writing numpy: %d rows, %d cols", rows, cols)
211 npw.WriteFloat64(out)
224 func array2matrix(rows, cols int, data []int16) mat.Matrix {
225 floatdata := make([]float64, rows*cols)
226 for i, v := range data {
227 floatdata[i] = float64(v)
229 return mat.NewDense(rows, cols, floatdata)