func _main(c *cli.Context) {

	g_START_SEQUENCEID = c.Int("start-sequence-id")
	g_START_GRAPHJOINID = c.Int("start-graphjoin-id")
	g_FASTAID = c.Int("fasta-id")
	g_VARIANTSETID = c.Int("variantset-id")

	if c.Bool("pprof") {
		gProfileFlag = true
		gProfileFile = c.String("pprof-file")
	}

	if c.Bool("mprof") {
		gMemProfileFlag = true
		gMemProfileFile = c.String("mprof-file")
	}

	g_show_progress = c.Bool("progress")
	gVerboseFlag = c.Bool("Verbose")

	if c.Int("max-procs") > 0 {
		runtime.GOMAXPROCS(c.Int("max-procs"))
	}

	if gProfileFlag {
		prof_f, err := os.Create(gProfileFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Could not open profile file %s: %v\n", gProfileFile, err)
			os.Exit(2)
		}

		pprof.StartCPUProfile(prof_f)
		defer pprof.StopCPUProfile()
	}

	fasta_ofn := c.String("fasta")
	sequence_ofn := c.String("sequence")
	graphjoin_ofn := c.String("graphjoin")
	fasta_csv_ofn := c.String("fasta-csv")
	graphjoin_variantset_ofn := c.String("graphjoin-variantset")

	fasta_out, err := autoio.CreateWriter(fasta_ofn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { fasta_out.Flush(); fasta_out.Close() }()

	fasta_csv_out, err := autoio.CreateWriter(fasta_csv_ofn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { fasta_csv_out.Flush(); fasta_csv_out.Close() }()

	seq_out, err := autoio.CreateWriter(sequence_ofn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { seq_out.Flush(); seq_out.Close() }()

	gj_out, err := autoio.CreateWriter(graphjoin_ofn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { gj_out.Flush(); gj_out.Close() }()

	vs_gj_out, err := autoio.CreateWriter(graphjoin_variantset_ofn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { vs_gj_out.Flush(); vs_gj_out.Close() }()

	// Process input FastJ files

	ifns := c.StringSlice("input")
	for i := 0; i < len(ifns); i++ {

		if g_show_progress {
			fmt.Fprintf(os.Stderr, ">>> %s\n", ifns[i])
		}

		name := ifns[i]
		ifn := ifns[i]
		if strings.Contains(ifns[i], ",") {
			z := strings.SplitN(ifns[i], ",", 2)
			name = z[0]
			ifn = z[1]
		}

		e := import_fastj(name, ifn)
		if e != nil {
			log.Fatal(e)
		}
	}

	// Once the library has been created, rank
	// the resulting tiles.
	//
	rank_tile_lib()

	// Output a big FASTA file with all of our
	// sequence information.
	//
	emit_fasta(fasta_out.Writer)

	// And our FASTA.csv file
	//
	emit_fasta_sql_csv(fasta_csv_out.Writer, fasta_ofn)

	// Now output the SQL Sequences.
	//
	emit_sequences(seq_out.Writer)

	// Now emit the SQL GraphJoin
	//
	emit_graphjoin(gj_out.Writer)

	emit_graphjoin_variantset(vs_gj_out.Writer)

}
Esempio n. 2
0
func _main(c *cli.Context) {
	gShowKnotNocallInfoFlag = !c.Bool("hide-knot-low-quality")

	inp_slice := c.StringSlice("input")

	cglf_lib_location := c.String("cglf")

	action := c.String("action")
	if action == "debug" {
		//debug_read(c.String("cgf"))
		cgf.DebugRead(c.String("cgf"))
		return
	} else if action == "headercheck" {

		//header_bytes := cgf_default_header_bytes()
		header_bytes := cgf.CGFDefaultHeaderBytes()

		//hdri,dn := headerintermediate_from_bytes(header_bytes) ; _ = dn
		hdri, dn := cgf.HeaderIntermediateFromBytes(header_bytes)
		_ = dn
		//hdri_bytes := bytes_from_headerintermediate(hdri)
		hdri_bytes := cgf.BytesFromHeaderIntermediate(hdri)
		//hdri1,dn2 := headerintermediate_from_bytes(hdri_bytes) ; _ = dn2
		hdri1, dn2 := cgf.HeaderIntermediateFromBytes(hdri_bytes)
		_ = dn2

		//err := headerintermediate_cmp(hdri, hdri1)
		err := cgf.HeaderIntermediateCmp(hdri, hdri1)

		if err != nil {
			log.Fatal(err)
		}
		return
	} else if action == "header" {

		ocgf := c.String("output")

		//header_bytes := cgf_default_header_bytes()
		header_bytes := cgf.CGFDefaultHeaderBytes()

		f, err := os.Create(ocgf)
		if err != nil {
			log.Fatal(err)
		}

		f.Write(header_bytes)
		f.Sync()
		f.Close()

		return

	} else if action == "knot" {

		cglf_path := c.String("cglf")
		if len(cglf_path) == 0 {
			fmt.Fprintf(os.Stderr, "Provide CGLF\n")
			cli.ShowAppHelp(c)
			os.Exit(1)
		}

		cgf_bytes, e := ioutil.ReadFile(c.String("cgf"))
		if e != nil {
			log.Fatal(e)
		}

		//hdri,dn := headerintermediate_from_bytes(cgf_bytes[:])
		hdri, dn := cgf.HeaderIntermediateFromBytes(cgf_bytes[:])
		_ = hdri
		_ = dn

		//path,ver,step,e := parse_tilepos(c.String("tilepos"))
		path, ver, step, e := cgf.ParseTilepos(c.String("tilepos"))
		if e != nil {
			log.Fatal(e)
		}

		if path < 0 {
			log.Fatal("path must be positive")
		}
		if step < 0 {
			log.Fatal("step must be positive")
		}
		//if path >= len(hdri.step_per_path) { log.Fatal("path out of range (max ", len(hdri.step_per_path), " paths)") }
		//if step>= hdri.step_per_path[path] { log.Fatal("step out of range (max ", hdri.step_per_path[path], " steps)") }

		if path >= len(hdri.StepPerPath) {
			log.Fatal("path out of range (max ", len(hdri.StepPerPath), " paths)")
		}
		if step >= hdri.StepPerPath[path] {
			log.Fatal("step out of range (max ", hdri.StepPerPath[path], " steps)")
		}

		//pathi,_ := pathintermediate_from_bytes(hdri.path_bytes[path])
		pathi, _ := cgf.PathIntermediateFromBytes(hdri.PathBytes[path])

		knot := cgf.GetKnot(hdri.TileMap, pathi, step)
		if knot == nil {
			fmt.Printf("spanning tile?\n")
		} else {

			for i := 0; i < len(knot); i++ {
				phase_str := "A"
				if i == 1 {
					phase_str = "B"
				}

				for j := 0; j < len(knot[i]); j++ {
					fmt.Printf("%s %04x.%02x.%04x.%03x+%x",
						phase_str,
						path, ver,
						knot[i][j].Step,
						knot[i][j].VarId,
						knot[i][j].Span)

					seq := cgf.CGLFGetLibSeq(uint64(path),
						uint64(knot[i][j].Step),
						uint64(knot[i][j].VarId),
						uint64(knot[i][j].Span),
						cglf_path)

					if len(knot[i][j].NocallStartLen) > 0 {
						fmt.Printf("*{")
						for p := 0; p < len(knot[i][j].NocallStartLen); p += 2 {
							if p > 0 {
								fmt.Printf(";")
							}
							fmt.Printf("%d+%d",
								knot[i][j].NocallStartLen[p],
								knot[i][j].NocallStartLen[p+1])
						}
						fmt.Printf("}")

						//noc_seq := fill_noc_seq(seq, knot[i][j].NocallStartLen)
						noc_seq := cgf.FillNocSeq(seq, knot[i][j].NocallStartLen)
						noc_m5str := cgf.Md5sum2str(md5.Sum([]byte(noc_seq)))
						fmt.Printf(" %s\n%s\n", noc_m5str, noc_seq)
					} else {
						m5str := cgf.Md5sum2str(md5.Sum([]byte(seq)))
						fmt.Printf(" %s\n%s\n", m5str, seq)
					}

				}

			}

		}

		return

	} else if action == "fastj" {

		tilepos_str := c.String("tilepos")
		if len(tilepos_str) == 0 {
			log.Fatal("missing tilepos")
		}

		if use_SGLF {
			_sglf, e := cglf.LoadGenomeLibraryCSV(c.String("sglf"))
			if e != nil {
				log.Fatal(e)
			}

			for i := 0; i < len(inp_slice); i++ {
				//e = print_tile_sglf(inp_slice[i], tilepos_str, sglf)
				e = cgf.PrintTileSGLF(inp_slice[i], tilepos_str, _sglf)
				if e != nil {
					log.Fatal(e)
				}
			}
		} else {
			if len(c.String("cgf")) != 0 {
				inp_slice = append(inp_slice, c.String("cgf"))
			}

			for i := 0; i < len(inp_slice); i++ {
				//e := print_tile_cglf(inp_slice[i], tilepos_str, cglf_lib_location)
				e := cgf.PrintTileCGLF(inp_slice[i], tilepos_str, cglf_lib_location)
				if e != nil {
					log.Fatal(e)
				}
			}

		}

		return
	} else if action == "fastj-range" {

		tilepos_str := c.String("tilepos")

		pos_parts := strings.Split(tilepos_str, ".")
		if (len(pos_parts) != 2) && (len(pos_parts) != 3) {
			fmt.Fprintf(os.Stderr, "Invalid tilepos\n")
			cli.ShowAppHelp(c)
			os.Exit(1)
		}

		path_range, e := parseIntOption(pos_parts[0], 16)
		if e != nil {
			fmt.Fprintf(os.Stderr, "Invalid path in tilepos: %v\n", e)
			cli.ShowAppHelp(c)
			os.Exit(1)
		}

		pp := 1
		if len(pos_parts) == 3 {
			pp = 2
		}

		step_range, e := parseIntOption(pos_parts[pp], 16)
		if e != nil {
			fmt.Fprintf(os.Stderr, "Invalid step in tilepos: %v\n", e)
			cli.ShowAppHelp(c)
			os.Exit(1)
		}

		if len(tilepos_str) == 0 {
			log.Fatal("missing tilepos")
		}

		if len(c.String("sglf")) > 0 {
			use_SGLF = true
		}

		if use_SGLF {
			_sglf, e := cglf.LoadGenomeLibraryCSV(c.String("sglf"))
			_ = _sglf
			if e != nil {
				log.Fatal(e)
			}

			if len(c.String("cgf")) != 0 {
				inp_slice = append(inp_slice, c.String("cgf"))
			}

			for i := 0; i < len(inp_slice); i++ {
				cgf_bytes, e := ioutil.ReadFile(inp_slice[i])
				if e != nil {
					log.Fatal(e)
				}

				path := path_range[0][0]

				//hdri,_ := headerintermediate_from_bytes(cgf_bytes) ; _ = hdri
				//pathi,_ := pathintermediate_from_bytes(hdri.PathBytes[path]) ; _ = pathi

				hdri, _ := cgf.HeaderIntermediateFromBytes(cgf_bytes)
				_ = hdri
				pathi, _ := cgf.PathIntermediateFromBytes(hdri.PathBytes[path])
				_ = pathi

				//hdri,dn := headerintermediate_from_bytes(cgf_bytes)
				hdri, dn := cgf.HeaderIntermediateFromBytes(cgf_bytes)
				if dn < 0 {
					log.Fatal("could not construct header from bytes")
				}

				//patho,dn := pathintermediate_from_bytes(hdri.PathBytes[path])
				patho, dn := cgf.PathIntermediateFromBytes(hdri.PathBytes[path])
				if dn < 0 {
					log.Fatal("could not construct path")
				}

				tilemap_bytes, _ := cgf.CGFTilemapBytes(cgf_bytes)
				//tilemap := unpack_tilemap(tilemap_bytes)
				tilemap := cgf.UnpackTileMap(tilemap_bytes)

				for step_idx := 0; step_idx < len(step_range); step_idx++ {
					if step_range[step_idx][1] == -1 {
						//step_range[step_idx][1] = int64(hdri.step_per_path[path])
						step_range[step_idx][1] = int64(hdri.StepPerPath[path])
					}
				}

				for stepr_idx := 0; stepr_idx < len(step_range); stepr_idx++ {
					for step := step_range[stepr_idx][0]; step < step_range[stepr_idx][1]; step++ {
						//knot := GetKnot(tilemap, patho, int(step))
						//print_knot_fastj_sglf(knot, _sglf, uint64(path), 0, hdri)
						knot := cgf.GetKnot(tilemap, patho, int(step))
						cgf.PrintKnotFastjSGLF(knot, _sglf, uint64(path), 0, hdri)
					}
				}

			}
			return
		} else {
			if len(c.String("cgf")) != 0 {
				inp_slice = append(inp_slice, c.String("cgf"))
			}

			for i := 0; i < len(inp_slice); i++ {
				cgf_bytes, e := ioutil.ReadFile(inp_slice[i])
				if e != nil {
					log.Fatal(e)
				}

				path := path_range[0][0]

				_sglf := cglf.SGLF{}

				//populate_sglf_from_cglf(c.String("cglf"), &_sglf, uint64(path))
				cgf.PopulateSGLFFromCGLF(c.String("cglf"), &_sglf, uint64(path))

				os.Exit(0)

				//hdri,_ := headerintermediate_from_bytes(cgf_bytes) ; _ = hdri
				//pathi,_ := pathintermediate_from_bytes(hdri.PathBytes[path]) ; _ = pathi

				hdri, _ := cgf.HeaderIntermediateFromBytes(cgf_bytes)
				_ = hdri
				pathi, _ := cgf.PathIntermediateFromBytes(hdri.PathBytes[path])
				_ = pathi

				//hdri,dn := headerintermediate_from_bytes(cgf_bytes)
				hdri, dn := cgf.HeaderIntermediateFromBytes(cgf_bytes)
				if dn < 0 {
					log.Fatal("could not construct header from bytes")
				}

				//patho,dn := pathintermediate_from_bytes(hdri.PathBytes[path])
				patho, dn := cgf.PathIntermediateFromBytes(hdri.PathBytes[path])
				if dn < 0 {
					log.Fatal("could not construct path")
				}

				tilemap_bytes, _ := cgf.CGFTilemapBytes(cgf_bytes)
				//tilemap := unpack_tilemap(tilemap_bytes)
				tilemap := cgf.UnpackTileMap(tilemap_bytes)

				for step_idx := 0; step_idx < len(step_range); step_idx++ {
					if step_range[step_idx][1] == -1 {
						//step_range[step_idx][1] = int64(hdri.step_per_path[path])
						step_range[step_idx][1] = int64(hdri.StepPerPath[path])
					}
				}

				for stepr_idx := 0; stepr_idx < len(step_range); stepr_idx++ {
					for step := step_range[stepr_idx][0]; step < step_range[stepr_idx][1]; step++ {
						//knot := GetKnot(tilemap, patho, int(step))
						//print_knot_fastj_sglf(knot, _sglf, uint64(path), 0, hdri)
						knot := cgf.GetKnot(tilemap, patho, int(step))
						cgf.PrintKnotFastjSGLF(knot, _sglf, uint64(path), 0, hdri)
					}
				}

			}

		}

		return
	} else if action == "knot-z" {

		cgf_bytes, e := ioutil.ReadFile(c.String("cgf"))
		_ = cgf_bytes
		if e != nil {
			log.Fatal(e)
		}

		path, ver, step, e := cgf.ParseTilepos(c.String("tilepos"))
		_ = path
		_ = ver
		_ = step
		if e != nil {
			log.Fatal(e)
		}

		if path < 0 {
			log.Fatal("path must be positive")
		}
		if step < 0 {
			log.Fatal("step must be positive")
		}

		fmt.Printf("not implemented\n")

		return
	} else if action == "knot-2" {

		cgf_bytes, e := ioutil.ReadFile(c.String("cgf"))
		if e != nil {
			log.Fatal(e)
		}

		//hdri,dn := headerintermediate_from_bytes(cgf_bytes[:])
		hdri, dn := cgf.HeaderIntermediateFromBytes(cgf_bytes[:])
		_ = hdri
		_ = dn

		//path,ver,step,e := parse_tilepos(c.String("tilepos"))
		path, ver, step, e := cgf.ParseTilepos(c.String("tilepos"))
		if e != nil {
			log.Fatal(e)
		}

		if path < 0 {
			log.Fatal("path must be positive")
		}
		if step < 0 {
			log.Fatal("step must be positive")
		}
		//if path >= len(hdri.step_per_path) { log.Fatal("path out of range (max ", len(hdri.step_per_path), " paths)") }
		//if step>= hdri.step_per_path[path] { log.Fatal("step out of range (max ", hdri.step_per_path[path], " steps)") }

		if path >= len(hdri.StepPerPath) {
			log.Fatal("path out of range (max ", len(hdri.StepPerPath), " paths)")
		}
		if step >= hdri.StepPerPath[path] {
			log.Fatal("step out of range (max ", hdri.StepPerPath[path], " steps)")
		}

		//pathi,_ := pathintermediate_from_bytes(hdri.PathBytes[path])
		pathi, _ := cgf.PathIntermediateFromBytes(hdri.PathBytes[path])

		//knot := GetKnot(hdri.tilemap, pathi, step)
		knot := cgf.GetKnot(hdri.TileMap, pathi, step)
		if knot == nil {
			fmt.Printf("spanning tile?")
		} else {

			for i := 0; i < len(knot); i++ {
				for j := 0; j < len(knot[i]); j++ {
					if j > 0 {
						fmt.Printf(" ")
					}
					fmt.Printf("%04x.%02x.%04x.%03x+%x",
						path, ver,
						knot[i][j].Step,
						knot[i][j].VarId,
						knot[i][j].Span)

					if gShowKnotNocallInfoFlag {
						if len(knot[i][j].NocallStartLen) > 0 {
							fmt.Printf("*{")
							for p := 0; p < len(knot[i][j].NocallStartLen); p += 2 {
								if p > 0 {
									fmt.Printf(";")
								}
								fmt.Printf("%d+%d",
									knot[i][j].NocallStartLen[p],
									knot[i][j].NocallStartLen[p+1])
							}
							fmt.Printf("}")
						}
					}
				}
				fmt.Printf("\n")
			}

		}

		return

	} else if action == "sglfbarf" {

		//_sglf,e := LoadGenomeLibraryCSV(c.String("sglf"))
		_sglf, e := cglf.LoadGenomeLibraryCSV(c.String("sglf"))
		if e != nil {
			log.Fatal(e)
		}

		for path := range _sglf.LibInfo {
			for step := range _sglf.LibInfo[path] {
				for i := 0; i < len(_sglf.LibInfo[path][step]); i++ {
					fmt.Printf("%x,%x,%x.%x.%x+%x\n", path, step,
						_sglf.LibInfo[path][step][i].Path,
						_sglf.LibInfo[path][step][i].Step,
						_sglf.LibInfo[path][step][i].Variant,
						_sglf.LibInfo[path][step][i].Span)
				}
			}
		}

		return
	} else if action == "append" {

		_sglf, e := cglf.LoadGenomeLibraryCSV(c.String("sglf"))
		if e != nil {
			log.Fatal(e)
		}

		ain_slice := make([]autoio.AutoioHandle, 0, 8)
		for i := 0; i < len(inp_slice); i++ {
			inp_fn := inp_slice[i]
			ain, err := autoio.OpenReadScanner(inp_fn)
			_ = ain
			if err != nil {
				fmt.Fprintf(os.Stderr, "%v", err)
				os.Exit(1)
			}
			defer ain.Close()
			ain_slice = append(ain_slice, ain)
			break
		}

		path_str := c.String("path")
		path_u64, e := strconv.ParseInt(path_str, 16, 64)
		if e != nil {
			log.Fatal(e)
		}
		path := int(path_u64)

		cgf_bytes, e := ioutil.ReadFile(c.String("cgf"))
		if e != nil {
			log.Fatal(e)
		}

		//hdri,_ := headerintermediate_from_bytes(cgf_bytes[:])
		hdri, _ := cgf.HeaderIntermediateFromBytes(cgf_bytes[:])

		ctx := cgf.CGFContext{}
		_cgf := cgf.CGF{}
		_cgf.PathBytes = make([][]byte, 0, 1024)
		cgf.CGFFillHeader(&_cgf, cgf_bytes)

		ctx.CGF = &_cgf
		ctx.SGLF = &_sglf
		//CGFContext_construct_tilemap_lookup(&ctx)
		ctx.ConstructTileMapLookup()

		//allele_path,e := load_sample_fastj(&ain_slice[0])
		allele_path, e := cgf.LoadSampleFastj(&ain_slice[0])
		if e != nil {
			log.Fatal(e)
		}

		//PathBytes,e := emit_path_bytes(&ctx, path, allele_path)
		PathBytes, e := ctx.EmitPathBytes(path, allele_path)
		if e != nil {
			log.Fatal(e)
		}

		//headerintermediate_add_path(&hdri, path, PathBytes)
		//write_cgf_from_intermediate(c.String("output"), &hdri)

		cgf.HeaderIntermediateAddPath(&hdri, path, PathBytes)
		cgf.WriteCGFFromIntermediate(c.String("output"), &hdri)

		return
	}

	ain_slice := make([]autoio.AutoioHandle, 0, 8)
	for i := 0; i < len(inp_slice); i++ {
		inp_fn := inp_slice[i]
		ain, err := autoio.OpenReadScanner(inp_fn)
		_ = ain
		if err != nil {
			fmt.Fprintf(os.Stderr, "%v", err)
			os.Exit(1)
		}
		defer ain.Close()
		ain_slice = append(ain_slice, ain)
	}

	aout, err := autoio.CreateWriter(c.String("output"))
	_ = aout
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { aout.Flush(); aout.Close() }()

	if c.Bool("pprof") {
		gProfileFlag = true
		gProfileFile = c.String("pprof-file")
	}

	if c.Bool("mprof") {
		gMemProfileFlag = true
		gMemProfileFile = c.String("mprof-file")
	}

	gVerboseFlag = c.Bool("Verbose")

	if c.Int("max-procs") > 0 {
		runtime.GOMAXPROCS(c.Int("max-procs"))
	}

	if gProfileFlag {
		prof_f, err := os.Create(gProfileFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Could not open profile file %s: %v\n", gProfileFile, err)
			os.Exit(2)
		}

		pprof.StartCPUProfile(prof_f)
		defer pprof.StopCPUProfile()
	}

	_sglf, e := cglf.LoadGenomeLibraryCSV(c.String("sglf"))
	if e != nil {
		log.Fatal(e)
	}

	ctx := cgf.CGFContext{}
	_cgf := cgf.CGF{}

	//header_bytes := cgf_default_header_bytes()
	header_bytes := cgf.CGFDefaultHeaderBytes()
	cgf.CGFFillHeader(&_cgf, header_bytes)

	ctx.CGF = &_cgf
	ctx.SGLF = &_sglf
	//CGFContext_construct_tilemap_lookup(&ctx)
	ctx.ConstructTileMapLookup()

	for i := 0; i < len(ain_slice); i++ {
		ain := ain_slice[i]

		//allele_path,e := load_sample_fastj(&ain)
		allele_path, e := cgf.LoadSampleFastj(&ain)
		if e != nil {
			log.Fatal(e)
		}

		p := 0x2c5
		if i > 0 {
			p = 0x247
		}

		//e = update_vector_path_simple(&ctx, p, allele_path)
		e = ctx.UpdateVectorPathSimple(p, allele_path)
		if len(ctx.CGF.StepPerPath) < len(ain_slice) {
			ctx.CGF.StepPerPath = append(ctx.CGF.StepPerPath, uint64(len(_sglf.Lib[p])))
		}

	}

	ctx.CGF.PathCount = uint64(len(_cgf.Path))
	ctx.CGF.StepPerPath = make([]uint64, ctx.CGF.PathCount)
	for i := uint64(0); i < ctx.CGF.PathCount; i++ {
		ctx.CGF.StepPerPath[i] = uint64(len(_sglf.Lib[int(i)]))
	}

	//write_cgf(&ctx, "out.cgf")
	ctx.WriteCGF("out.cgf")

}
Esempio n. 3
0
func _main(c *cli.Context) {
	sequence_ifn := c.String("sequence")
	if len(sequence_ifn) == 0 {
		cli.ShowAppHelp(c)
	}

	g_ALLELE_ID = c.Int("start-allele-id")
	g_START_CALLSET_ID = c.Int("start-callset-id")
	g_START_VARIANTSET_ID = c.Int("start-variantset-id")
	g_VARIANTSET_NAME = c.String("variantset-name")

	ifns := c.StringSlice("input")
	if len(ifns) == 0 {
		cli.ShowAppHelp(c)
	}

	if c.Bool("pprof") {
		gProfileFlag = true
		gProfileFile = c.String("pprof-file")
	}

	if c.Bool("mprof") {
		gMemProfileFlag = true
		gMemProfileFile = c.String("mprof-file")
	}

	gVerboseFlag = c.Bool("Verbose")

	if c.Int("max-procs") > 0 {
		runtime.GOMAXPROCS(c.Int("max-procs"))
	}

	if gProfileFlag {
		prof_f, err := os.Create(gProfileFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Could not open profile file %s: %v\n", gProfileFile, err)
			os.Exit(2)
		}

		pprof.StartCPUProfile(prof_f)
		defer pprof.StopCPUProfile()
	}

	allele_ofn := c.String("allele")
	allele_path_item_ofn := c.String("allele-path")
	callset_ofn := c.String("callset")
	allele_call_ofn := c.String("allele-call")
	variantset_ofn := c.String("variantset")
	variantset_callset_ofn := c.String("variantset-callset-join")

	allele_out, err := autoio.CreateWriter(allele_ofn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { allele_out.Flush(); allele_out.Close() }()

	allele_path_item_out, err := autoio.CreateWriter(allele_path_item_ofn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { allele_path_item_out.Flush(); allele_path_item_out.Close() }()

	callset_out, err := autoio.CreateWriter(callset_ofn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { callset_out.Flush(); callset_out.Close() }()

	allele_call_out, err := autoio.CreateWriter(allele_call_ofn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { allele_call_out.Flush(); allele_call_out.Close() }()

	variantset_out, err := autoio.CreateWriter(variantset_ofn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { variantset_out.Flush(); variantset_out.Close() }()

	variantset_callset_out, err := autoio.CreateWriter(variantset_callset_ofn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { variantset_callset_out.Flush(); variantset_callset_out.Close() }()

	show_progress_flag := c.Bool("progress")

	// We only use one variant set
	//
	add_variantset(g_START_VARIANTSET_ID, g_VARIANTSET_NAME)

	// Process Sequence CSV file
	//
	import_sequence(sequence_ifn)

	// First populate the callset maps
	//
	for i := 0; i < len(ifns); i++ {
		name := ifns[i]
		if strings.Contains(ifns[i], ",") {
			z := strings.SplitN(ifns[i], ",", 2)
			name = z[0]
		}
		g_callset[name] = CallSet{g_START_CALLSET_ID + i, name, name}
	}

	// Process input FastJ files
	//
	for i := 0; i < len(ifns); i++ {

		name := ifns[i]
		ifn := ifns[i]
		if strings.Contains(ifns[i], ",") {
			z := strings.SplitN(ifns[i], ",", 2)
			name = z[0]
			ifn = z[1]
		}

		if show_progress_flag {
			fmt.Printf(">>>> %s %s\n", name, ifn)
		}

		e := import_fastj(name, ifn)
		if e != nil {
			log.Fatal(e)
		}
	}

	// Variant Sets
	//
	emit_variantset(variantset_out.Writer)

	// Output the CallSet
	//
	emit_callset(callset_out.Writer)

	emit_variantset_callset_join(variantset_callset_out.Writer)

	// Output Allele information
	//
	emit_allele(allele_out.Writer)

	//
	emit_allele_call(allele_call_out.Writer)

	// Output actual path
	//
	emit_allele_path_item(allele_path_item_out.Writer)

}
Esempio n. 4
0
func _main(c *cli.Context) {
	gLanternStat = LanternStatStruct{}
	gLanternStat.StartTime = time.Now()

	ctx := &LanternContext{}

	//DEBUG
	ctx.VerboseFlag = true
	ctx.PrettyAPIFlag = true
	ctx.VerboseAPIFlag = true

	e := _load_json_config(ctx, c.String("config"))
	if e != nil {
		log.Fatal(e)
	}

	tagset_pdh := "dad7041d432965cd07a4ad8e0aad1b6e"
	assembly_pdh := "aa39590be6f1812f0a792dd4c86678e8+1348"

	if ctx.VerboseFlag {
		log.Printf("loading assembly %v (tagset %v)\n", assembly_pdh, tagset_pdh)
	}

	e = load_Assembly(ctx, tagset_pdh, assembly_pdh)
	if e != nil {
		log.Fatal(e)
	}
	if ctx.VerboseFlag {
		log.Printf("assembly loaded\n")
	}

	//e = ctx.LoadCGFBytes(ctx.Config.O["cgf"].O["dir"].S)
	e = ctx.LoadCGFBytesConfig()
	if e != nil {
		log.Fatal(e)
	}
	if ctx.VerboseFlag {
		log.Printf("cgf bytes loaded\n")
	}

	ctx.LoadCGFIntermediate()

	for sglf_name, _ := range ctx.Config.O["tagset"].O[tagset_pdh].O["sglf"].O {
		_path, e := strconv.ParseInt(sglf_name, 16, 64)
		if e != nil {
			log.Fatal(e)
		}

		ipath := int(_path)
		ctx.LoadSGLF(ipath, ctx.Config.O["tagset"].O[tagset_pdh].O["sglf"].O[sglf_name].S)

	}

	log.Printf("PreCalc starting...\n")

	ctx.PreCalc()

	log.Printf("PreCalc done\n")

	/*
	  //DEBUG
	  fmt.Printf("testing...\n")
	  for pdh := range ctx.Assembly {
	    for path := range ctx.Assembly[pdh] {
	      for i:=0; i<len(ctx.Assembly[pdh][path]);  i++ {
	        fmt.Printf("%v %v %v\n", pdh, path, ctx.Assembly[pdh][path][i])
	      }
	    }

	    os.Exit(0)
	  }

	  for pdh := range ctx.AssemblyChrom {
	    for path := range ctx.AssemblyChrom[pdh] {
	      fmt.Printf("%v %v %v\n", pdh, path, ctx.AssemblyChrom[pdh][path])
	    }
	  }

	  fmt.Printf("cp\n")
	*/

	/*
	  if c.String("input") == "" {
	    fmt.Fprintf( os.Stderr, "Input required, exiting\n" )
	    cli.ShowAppHelp( c )
	    os.Exit(1)
	  }

	  ain,err := autoio.OpenScanner( c.String("input") ) ; _ = ain
	  if err!=nil {
	    fmt.Fprintf(os.Stderr, "%v", err)
	    os.Exit(1)
	  }
	  defer ain.Close()
	*/

	aout, err := autoio.CreateWriter(c.String("output"))
	_ = aout
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	defer func() { aout.Flush(); aout.Close() }()

	if c.Bool("pprof") {
		gProfileFlag = true
		gProfileFile = c.String("pprof-file")
	}

	if c.Bool("mprof") {
		gMemProfileFlag = true
		gMemProfileFile = c.String("mprof-file")
	}

	gVerboseFlag = c.Bool("Verbose")

	if c.Int("max-procs") > 0 {
		runtime.GOMAXPROCS(c.Int("max-procs"))
	}

	if gProfileFlag {
		//prof_f,err := os.Create( gProfileFile )
		var err error
		g_pprof, err = os.Create(gProfileFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Could not open profile file %s: %v\n", gProfileFile, err)
			os.Exit(2)
		}

		//pprof.StartCPUProfile( prof_f )
		//defer pprof.StopCPUProfile()
		pprof.StartCPUProfile(g_pprof)
	}

	// Start server
	//

	//listener,err := net.Listen("tcp", gPortStr)
	//if err!=nil { log.Fatal(err) }

	term := make(chan os.Signal, 1)
	go func(sig <-chan os.Signal) {
		s := <-sig
		if gVerboseFlag {
			fmt.Printf("caught signal: %v\n", s)
			//listener.Close()
		}
	}(term)
	signal.Notify(term, syscall.SIGTERM)
	signal.Notify(term, syscall.SIGHUP)

	// Set up routing
	//
	router := httprouter.New()

	router.POST("/", handle_json_req)
	router.GET("/", index)
	//router.GET("/status", handle_status)
	router.GET("/status", ctx.APIStatus)
	router.GET("/cgf-stats", ctx.CGFSimpleStats)

	//router.GET("/qux", func(w http.ResponseWriter, r *http.Request, params httprouter.Params) { ctx.Qux(w,r, params); } )
	router.GET("/qux", ctx.Qux)

	router.GET("/assemblies", ctx.APIAssemblies)
	router.GET("/assemblies/:id", ctx.APIAssembliesId)

	//router.GET("/callsets", handle_callsets)

	//router.GET("/tile-library/tag-sets", handle_tile_library_tag_sets)
	//router.GET("/tile-library/tag-sets/:tagset_id", handle_tile_library_tag_sets_id)
	//router.GET("/tile-library/tag-sets/:tagset_id/paths", handle_tile_library_tag_sets_id_paths)
	//router.GET("/tile-library/tag-sets/:tagset_id/paths/:path_id", handle_tile_library_tag_sets_id_paths_id)
	//router.GET("/tile-library/tag-sets/:tagset_id/tile-positions", handle_tile_library_tag_sets_id_tile_positions)
	//router.GET("/tile-library/tag-sets/:tagset_id/tile-positions/:tilepos_id", handle_tile_library_tag_sets_id_tile_positions_id)
	//router.GET("/tile-library/tag-sets/:tagset_id/tile-positions/:tilepos_id/locus", handle_tile_library_tag_sets_id_tile_positions_id_locus)

	router.GET("/tile-library/tag-sets", ctx.APITileLibraryTagSets)
	router.GET("/tile-library/tag-sets/:tagset_id", ctx.APITileLibraryTagSetsId)
	router.GET("/tile-library/tag-sets/:tagset_id/paths", ctx.APITileLibraryTagSetsIdPaths)
	router.GET("/tile-library/tag-sets/:tagset_id/paths/:path_id", ctx.APITileLibraryTagSetsIdPathsId)

	router.GET("/tile-library/tag-sets/:tagset_id/tile-positions", ctx.APITileLibraryTagSetsIdTilePositions)
	router.GET("/tile-library/tag-sets/:tagset_id/tile-positions/:tilepos_id", ctx.APITileLibraryTagSetsIdTilePositionsId)
	router.GET("/tile-library/tag-sets/:tagset_id/tile-positions/:tilepos_id/locus", ctx.APITileLibraryTagSetsIdTilePositionsIdLocus)

	router.GET("/tile-library/tag-sets/:tagset_id/tile-variants", ctx.APITileLibraryTagSetsIdTileVariants)
	router.GET("/tile-library/tag-sets/:tagset_id/tile-variants/:tilevariant_id", ctx.APITileLibraryTagSetsIdTileVariantsId)
	router.GET("/tile-library/tag-sets/:tagset_id/tile-variants/:tilevariant_id/locus", ctx.APITileLibraryTagSetsIdTileVariantsIdLocus)

	router.GET("/callsets", ctx.APICallsets)
	router.GET("/callsets/:callset_id", ctx.APICallsetsId)
	router.GET("/callsets/:callset_id/tile-variants", ctx.APICallsetsIdTileVariants)

	//router.GET("/tile-library/tag-sets/:tagset_id/tile-variants", handle_tile_library_tag_sets_id_tile_variants)
	//router.GET("/tile-library/tag-sets/:tagset_id/tile-variants/:tilevariant_id", handle_tile_library_tag_sets_id_tile_variants_id)
	//router.GET("/tile-library/tag-sets/:tagset_id/tile-variants/:tilevariant_id/locus", handle_tile_library_tag_sets_id_tile_variants_id_locus)

	//router.GET("/tile-library/tag-sets/:tagset_id/tile-variants/:tilevariant_id/subsequence", handle_tile_library_tag_sets_id_tile_variants_id_subsequence)

	//router.GET("/tile-library/tag-sets/:tagset_id/tile-variants/:tilevariant_id/annotations", handle_tile_library_tag_sets_id_tile_variants_id_annotations)

	/*
	  http.HandleFunc("/", handle_json_req)
	  http.HandleFunc("/status", handle_status)
	  http.HandleFunc("/assemblies/", handle_assemblies_id)
	  http.HandleFunc("/assemblies", handle_assemblies)
	*/

	if gVerboseFlag {
		fmt.Printf("listening: %v\n", gPortStr)
	}

	http.ListenAndServe(gPortStr, router)
	//listener,err := net.Listen("tcp", gPortStr)
	//if err!=nil { log.Fatal(err) }
	//defer listener.Close()
	//e := http.Serve(listener, router)

	if gVerboseFlag {
		fmt.Printf("shutting down\n")
	}

}
Esempio n. 5
0
func _main(c *cli.Context) {
	var e error
	action := "echo"

	msg_slice := c.StringSlice("Message")
	msg_str := ""
	for i := 0; i < len(msg_slice); i++ {
		msg_str += ">" + msg_slice[i]
	}

	//if c.String("action") != "" { action = c.String("action") }
	if len(c.String("action")) == 0 {
		cli.ShowAppHelp(c)
		return
	}

	action = c.String("action")

	if action == "diff-rotini" {
		_main_diff_to_rotini(c)
		return
	} else if action == "gff-rotini" {
		_main_gff_to_rotini(c)
		return
	} else if action == "gff-pasta" {
		_main_gff_to_pasta(c)
		return
	} else if action == "gvcf-rotini" {
		_main_gvcf_to_rotini(c)
		return
	} else if action == "cgivar-pasta" {
		_main_cgivar_to_pasta(c)
		return
	} else if action == "cgivar-rotini" {
		_main_cgivar_to_rotini(c)
		return
	} else if action == "fasta-pasta" {
		_main_fasta_to_pasta(c)
		return
	}

	infn_slice := c.StringSlice("input")

	var stream *bufio.Reader
	var stream_b *bufio.Reader

	g_debug = c.Bool("debug")

	gFullRefSeqFlag = c.Bool("full-sequence")
	gFullNocSeqFlag = c.Bool("full-nocall-sequence")

	n_inp_stream := 0

	if len(infn_slice) > 0 {
		fp := os.Stdin
		if infn_slice[0] != "-" {
			fp, e = os.Open(infn_slice[0])
			if e != nil {
				fmt.Fprintf(os.Stderr, "%v", e)
				os.Stderr.Sync()
				os.Exit(1)
			}
			defer fp.Close()
		}
		stream = bufio.NewReader(fp)

		n_inp_stream++
	}

	if len(infn_slice) > 1 {
		fp, e := os.Open(infn_slice[1])
		if e != nil {
			fmt.Fprintf(os.Stderr, "%v", e)
			os.Stderr.Sync()
			os.Exit(1)
		}
		defer fp.Close()
		stream_b = bufio.NewReader(fp)

		n_inp_stream++

		action = "interleave"
	}

	aout, err := autoio.CreateWriter(c.String("output"))
	_ = aout
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Stderr.Sync()
		os.Exit(1)
	}
	defer func() { aout.Flush(); aout.Close() }()

	if c.Bool("pprof") {
		gProfileFlag = true
		gProfileFile = c.String("pprof-file")
	}

	if c.Bool("mprof") {
		gMemProfileFlag = true
		gMemProfileFile = c.String("mprof-file")
	}

	gVerboseFlag = c.Bool("Verbose")

	if c.Int("max-procs") > 0 {
		runtime.GOMAXPROCS(c.Int("max-procs"))
	}

	if gProfileFlag {
		prof_f, err := os.Create(gProfileFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Could not open profile file %s: %v\n", gProfileFile, err)
			os.Stderr.Sync()
			os.Exit(2)
		}

		pprof.StartCPUProfile(prof_f)
		defer pprof.StopCPUProfile()
	}

	if (action != "ref-rstream") && (action != "rstream") && (n_inp_stream == 0) {

		if action == "interleave" {
			fmt.Fprintf(os.Stderr, "Provide input stream")
			cli.ShowAppHelp(c)
			os.Stderr.Sync()
			os.Exit(1)
		}

		stream = bufio.NewReader(os.Stdin)
	}

	//---

	if action == "echo" {
		echo_stream(stream)
	} else if action == "filter-pasta" {
		out := bufio.NewWriter(os.Stdout)
		pasta_filter(stream, out, c.Int("start"), c.Int("n"))
		out.Flush()
	} else if action == "filter-rotini" {
		out := bufio.NewWriter(os.Stdout)
		interleave_filter(stream, out, c.Int("start"), c.Int("n"))
		out.Flush()
	} else if action == "interleave" {
		pasta.InterleaveStreams(stream, stream_b, os.Stdout)
	} else if action == "ref-rstream" {

		r_ctx := random_stream_context_from_param(c.String("param"))
		random_ref_stream(r_ctx)

	} else if action == "rstream" {

		r_ctx := random_stream_context_from_param(c.String("param"))
		random_stream(r_ctx)

		//FASTA
	} else if action == "pasta-fasta" {

		fi := FASTAInfo{}
		fi.Init()

		out := bufio.NewWriter(os.Stdout)
		fi.Header(out)
		e := fi.Stream(stream, out)
		if e != nil {
			fmt.Fprintf(os.Stderr, "\nERROR: %v\n", e)
			os.Stderr.Sync()
			os.Exit(1)
		}
		fi.PrintEnd(out)

	} else if action == "diff-rotini" {

		//e:=diff_to_interleave(&stream)
		//if e!=nil { fmt.Fprintf(os.Stderr, "%v\n", e); return }

	} else if action == "rotini-diff" {

		e := interleave_to_diff(stream, simple_refvar_printer)
		if e != nil {
			fmt.Fprintf(os.Stderr, "%v\n", e)
			return
		}
	} else if action == "rotini" {
	} else if action == "pasta-ref" {
		e := pasta_to_haploid(stream, -1)
		if e != nil {
			fmt.Fprintf(os.Stderr, "\nERROR: %v\n", e)
			os.Stderr.Sync()
			os.Exit(1)
		}
	} else if action == "rotini-ref" {
		e := interleave_to_haploid(stream, -1)
		if e != nil {
			fmt.Fprintf(os.Stderr, "\nERROR: %v\n", e)
			os.Stderr.Sync()
			os.Exit(1)
		}
	} else if action == "rotini-alt0" {
		interleave_to_haploid(stream, 0)
	} else if action == "rotini-alt1" {
		interleave_to_haploid(stream, 1)
	} else if action == "rotini-gff" {

		gff := GFFRefVar{}
		gff.Init()

		e := interleave_to_diff_iface(stream, &gff, os.Stdout)
		if e != nil {
			fmt.Fprintf(os.Stderr, "%v\n", e)
			return
		}

	} else if action == "rotini-gvcf" {

		g := gvcf.GVCFRefVar{}
		g.Init()

		// We need the full reference sequence for beginning and ending bases
		//
		gFullRefSeqFlag = true

		e := interleave_to_diff_iface(stream, &g, os.Stdout)
		if e != nil {
			fmt.Fprintf(os.Stderr, "%v\n", e)
			return
		}

	} else if action == "rotini-cgivar" {

		cgivar := CGIRefVar{}
		cgivar.Init()

		e := interleave_to_diff_iface(stream, &cgivar, os.Stdout)
		if e != nil {
			fmt.Fprintf(os.Stderr, "%v\n", e)
			return
		}

	} else if action == "fastj-rotini" {

		//
		// FastJ to rotini
		//

		fp := os.Stdin
		if c.String("refstream") != "-" {
			fp, e = os.Open(c.String("refstream"))
			if e != nil {
				fmt.Fprintf(os.Stderr, "ERROR: opening reference stream: %v", e)
				os.Stderr.Sync()
				os.Exit(1)
			}
			defer fp.Close()
		}
		ref_stream := bufio.NewReader(fp)

		assembly_fp, e := os.Open(c.String("assembly"))
		if e != nil {
			fmt.Fprintf(os.Stderr, "ERROR: opening assembly stream: %v", e)
			os.Stderr.Sync()
			os.Exit(1)
		}
		defer assembly_fp.Close()
		assembly_stream := bufio.NewReader(assembly_fp)

		out := bufio.NewWriter(os.Stdout)

		fji := FastJInfo{}
		fji.RefPos = c.Int("start")
		fji.Chrom = c.String("chrom")

		e = fji.Pasta(stream, ref_stream, assembly_stream, out)
		if e != nil {
			fmt.Fprintf(os.Stderr, "ERROR: processing PASTA stream: %v\n", e)
			os.Stderr.Sync()
			os.Exit(1)
		}

	} else if action == "rotini-fastj" {

		//
		// rotini to FastJ
		//

		tag_fp, e := os.Open(c.String("tag"))
		if e != nil {
			fmt.Fprintf(os.Stderr, "%v", e)
			os.Stderr.Sync()
			os.Exit(1)
		}
		defer tag_fp.Close()

		assembly_fp, e := os.Open(c.String("assembly"))
		if e != nil {
			fmt.Fprintf(os.Stderr, "%v", e)
			os.Stderr.Sync()
			os.Exit(1)
		}
		defer assembly_fp.Close()

		tag_reader := bufio.NewReader(tag_fp)
		assembly_reader := bufio.NewReader(assembly_fp)

		fji := FastJInfo{}
		fji.RefPos = c.Int("start")
		fji.RefBuild = c.String("build")
		fji.Chrom = c.String("chrom")

		_tilepath, e := strconv.ParseUint(c.String("tilepath"), 16, 64)
		if e != nil {
			fmt.Fprintf(os.Stderr, "%v", e)
			os.Stderr.Sync()
			os.Exit(1)
		}
		fji.TagPath = int(_tilepath)

		out := bufio.NewWriter(os.Stdout)

		err := fji.Convert(stream, tag_reader, assembly_reader, out)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%v", err)
			os.Stderr.Sync()
			os.Exit(1)
		}

	} else {
		fmt.Printf("invalid action (%s)\n", action)
		os.Stderr.Sync()
		os.Exit(1)
	}

}