Example #1
0
// IsInParentList search an id in the list of parents of the taxon.
func isInParentList(c *cmdapp.Command, db jdh.DB, id string, pIds []string) bool {
	args := new(jdh.Values)
	args.Add(jdh.TaxParents, id)
	pl, err := db.List(jdh.Taxonomy, args)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	defer pl.Close()
	for {
		p := &jdh.Taxon{}
		if err := pl.Scan(p); err != nil {
			if err == io.EOF {
				break
			}
			fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
			os.Exit(1)
		}
		for _, pid := range pIds {
			if p.Id == pid {
				return true
			}
		}
	}
	return false
}
Example #2
0
// TaxInDB returns true if a taxon is in the database.
func taxInDB(c *cmdapp.Command, db jdh.DB, name, parent string, rank jdh.Rank, valid bool) *jdh.Taxon {
	args := new(jdh.Values)
	args.Add(jdh.TaxName, name)
	if len(parent) != 0 {
		args.Add(jdh.TaxParent, parent)
	}
	if rank != jdh.Unranked {
		args.Add(jdh.TaxRank, rank.String())
	}
	l, err := db.List(jdh.Taxonomy, args)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	defer l.Close()
	for {
		tax := &jdh.Taxon{}
		if err := l.Scan(tax); err != nil {
			if err == io.EOF {
				return nil
			}
		}
		if len(tax.Id) > 0 {
			if tax.IsValid == valid {
				return tax
			}
		}
	}
}
Example #3
0
// SpeList returns an specimen list scanner.
func speList(c *cmdapp.Command, db jdh.DB, vals *jdh.Values) jdh.ListScanner {
	l, err := db.List(jdh.Specimens, vals)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	return l
}
Example #4
0
// GetTaxDesc return the list of descendants of a taxon.
func getTaxDesc(c *cmdapp.Command, db jdh.DB, id string, valid bool) jdh.ListScanner {
	args := new(jdh.Values)
	if valid {
		args.Add(jdh.TaxChildren, id)
	} else {
		args.Add(jdh.TaxSynonyms, id)
	}
	l, err := db.List(jdh.Taxonomy, args)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	return l
}
Example #5
0
// Specimen gets an specimen.
func specimen(c *cmdapp.Command, db jdh.DB, id string) *jdh.Specimen {
	sc, err := db.Get(jdh.Specimens, id)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	spe := &jdh.Specimen{}
	if err := sc.Scan(spe); err != nil {
		if err == io.EOF {
			return &jdh.Specimen{}
		}
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	return spe
}
Example #6
0
// Taxon gets a taxon.
func taxon(c *cmdapp.Command, db jdh.DB, id string) *jdh.Taxon {
	sc, err := db.Get(jdh.Taxonomy, id)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	tax := &jdh.Taxon{}
	if err := sc.Scan(tax); err != nil {
		if err == io.EOF {
			return &jdh.Taxon{}
		}
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	return tax
}
Example #7
0
// Phylogeny gets a phylogeny.
func phylogeny(c *cmdapp.Command, db jdh.DB, id string) *jdh.Phylogeny {
	sc, err := db.Get(jdh.Trees, id)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	phy := &jdh.Phylogeny{}
	if err := sc.Scan(phy); err != nil {
		if err == io.EOF {
			return &jdh.Phylogeny{}
		}
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	return phy
}
Example #8
0
// Dataset gets a dataset.
func dataset(c *cmdapp.Command, db jdh.DB, id string) *jdh.Dataset {
	sc, err := db.Get(jdh.Datasets, id)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	set := &jdh.Dataset{}
	if err := sc.Scan(set); err != nil {
		if err == io.EOF {
			return &jdh.Dataset{}
		}
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	return set
}
Example #9
0
// PhyloNode gets a node.
func phyloNode(c *cmdapp.Command, db jdh.DB, id string) *jdh.Node {
	sc, err := db.Get(jdh.Nodes, id)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	nod := &jdh.Node{}
	if err := sc.Scan(nod); err != nil {
		if err == io.EOF {
			return &jdh.Node{}
		}
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	return nod
}
Example #10
0
// Raster gets the raster distribution.
func raster(c *cmdapp.Command, db jdh.DB, id string) *jdh.Raster {
	sc, err := db.Get(jdh.RasDistros, id)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	ras := &jdh.Raster{}
	if err := sc.Scan(ras); err != nil {
		if err == io.EOF {
			return &jdh.Raster{}
		}
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	return ras
}
Example #11
0
func newTxList(tax *jdh.Taxon, db jdh.DB, syns bool) *txList {
	ls := &txList{
		db:   db,
		tax:  tax,
		syns: syns,
	}
	id := ""
	if tax == nil {
		ls.tax = &jdh.Taxon{
			Id:   "0",
			Name: "root",
		}
	} else {
		id = tax.Id
	}
	vals := new(jdh.Values)
	vals.Add(jdh.TaxChildren, id)
	pl, err := db.List(jdh.Taxonomy, vals)
	if err != nil {
		return ls
	}
	for {
		d := &jdh.Taxon{}
		if err := pl.Scan(d); err != nil {
			break
		}
		ls.desc = append(ls.desc, d)
	}
	if !syns {
		return ls
	}
	vals.Reset()
	vals.Add(jdh.TaxSynonyms, id)
	pl, err = db.List(jdh.Taxonomy, vals)
	if err != nil {
		return ls
	}
	for {
		s := &jdh.Taxon{}
		if err := pl.Scan(s); err != nil {
			break
		}
		ls.desc = append(ls.desc, s)
	}
	return ls
}
Example #12
0
func dsLsRun(c *cmdapp.Command, args []string) {
	var db jdh.DB
	if len(extDBFlag) != 0 {
		openExt(c, extDBFlag, "")
		db = extDB
	} else {
		openLocal(c)
		db = localDB
	}
	l, err := db.List(jdh.Datasets, new(jdh.Values))
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	for {
		set := &jdh.Dataset{}
		if err := l.Scan(set); err != nil {
			if err == io.EOF {
				break
			}
			fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
			os.Exit(1)
		}
		if machineFlag {
			fmt.Fprintf(os.Stdout, "%s\n", set.Id)
			continue
		}
		if verboseFlag {
			fmt.Fprintf(os.Stdout, "%s\t%s\t%sn", set.Id, set.Title, set.Url)
			continue
		}
		fmt.Fprintf(os.Stdout, "%s", set.Title)
		if urlFlag {
			fmt.Fprintf(os.Stdout, "\t%s", set.Url)
		}
		if citFlag {
			fmt.Fprintf(os.Stdout, "\t%s", set.Citation)
		}
		if licFlag {
			fmt.Fprintf(os.Stdout, "\t%s", set.License)
		}
		fmt.Fprintf(os.Stdout, "\n")
	}
}
Example #13
0
// PickTaxName search for a unique taxon name. If there are more taxons
// fullfilling the name, then it will print a list of the potential
// names and finish the program.
func pickTaxName(c *cmdapp.Command, db jdh.DB, name, parent string) *jdh.Taxon {
	args := new(jdh.Values)
	args.Add(jdh.TaxName, name)
	if len(parent) != 0 {
		args.Add(jdh.TaxParentName, parent)
	}
	l, err := db.List(jdh.Taxonomy, args)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	var tax *jdh.Taxon
	mult := false
	for {
		ot := &jdh.Taxon{}
		if err := l.Scan(ot); err != nil {
			if err == io.EOF {
				break
			}
			fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
			os.Exit(1)
		}
		if tax == nil {
			tax = ot
			continue
		}
		if !mult {
			fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr("ambiguos taxon name"))
			fmt.Fprintf(os.Stderr, "%s\t%s\n", tax.Id, tax.Name)
			mult = true
		}
		fmt.Fprintf(os.Stderr, "%s\t%s\n", ot.Id, ot.Name)
	}
	if mult {
		os.Exit(0)
	}
	if tax == nil {
		return &jdh.Taxon{}
	}
	return tax
}
Example #14
0
func txLsRankNav(c *cmdapp.Command, db jdh.DB, id string, rank jdh.Rank) {
	args := new(jdh.Values)
	args.Add(jdh.TaxChildren, id)
	l, err := db.List(jdh.Taxonomy, args)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
		os.Exit(1)
	}
	for {
		desc := &jdh.Taxon{}
		if err := l.Scan(desc); err != nil {
			if err == io.EOF {
				break
			}
			fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
			os.Exit(1)
		}
		if len(desc.Id) == 0 {
			continue
		}
		txLsRank(c, db, desc, rank)
	}
}
Example #15
0
func newSpList(tax *jdh.Taxon, db jdh.DB) *spList {
	ls := &spList{
		db:  db,
		sel: -1,
		tax: tax,
	}
	if taxonRank(cmd, db, tax) < jdh.Species {
		return ls
	}
	vals := new(jdh.Values)
	vals.Add(jdh.SpeTaxon, tax.Id)
	l, err := db.List(jdh.Specimens, vals)
	if err != nil {
		return ls
	}
	for {
		spe := &jdh.Specimen{}
		if err := l.Scan(spe); err != nil {
			break
		}
		ls.spe = append(ls.spe, spe)
	}
	return ls
}
Example #16
0
func txLsRun(c *cmdapp.Command, args []string) {
	var db jdh.DB
	if len(extDBFlag) != 0 {
		openExt(c, extDBFlag, "")
		db = extDB
	} else {
		openLocal(c)
		db = localDB
	}
	var tax *jdh.Taxon
	if len(idFlag) > 0 {
		tax = taxon(c, db, idFlag)
		if len(tax.Id) == 0 {
			return
		}
	} else if len(args) > 0 {
		if len(args) > 2 {
			fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr("too many arguments"))
			os.Exit(1)
		}
		pName := ""
		if len(args) > 1 {
			pName = args[1]
		}
		tax = pickTaxName(c, db, args[0], pName)
		if len(tax.Id) == 0 {
			return
		}
	}
	if ancsFlag {
		if tax == nil {
			os.Exit(0)
		}
		vals := new(jdh.Values)
		vals.Add(jdh.TaxParents, tax.Id)
		l, err := db.List(jdh.Taxonomy, vals)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr(err))
			os.Exit(1)
		}
		txLsProc(c, l)
		return
	}
	if len(rankFlag) > 0 {
		rank := jdh.GetRank(rankFlag)
		if (rank == jdh.Unranked) && (strings.ToLower(rankFlag) != rank.String()) {
			fmt.Fprintf(os.Stderr, "%s\n", c.ErrStr("unknown rank"))
			os.Exit(1)
		}
		txLsRank(c, db, tax, rank)
		return
	}
	if synonymFlag {
		if tax == nil {
			os.Exit(0)
		}
		l := getTaxDesc(c, db, tax.Id, false)
		txLsProc(c, l)
		return
	}
	id := ""
	if tax != nil {
		id = tax.Id
	}
	l := getTaxDesc(c, db, id, true)
	txLsProc(c, l)
}