Example #1
0
// AddTree adds a new tree to the database.
func (tr *trees) addTree(phy *jdh.Phylogeny) (string, error) {
	id := strconv.FormatInt(tr.nxTree, 10)
	phy.Id = id
	if err := tr.valPhy(phy); err != nil {
		return "", err
	}
	tr.addValPhy(phy)
	tr.nxTree++
	tr.changed = true
	return id, nil
}
Example #2
0
// ValPhy validates that a tree is valid in the database, and set some
// canonical values. It returns an error if the tree is not valid.
func (tr *trees) valPhy(phy *jdh.Phylogeny) error {
	phy.Id = strings.TrimSpace(phy.Id)
	if len(phy.Id) == 0 {
		return errors.New("phylogeny without identification")
	}
	if _, ok := tr.ids[phy.Id]; ok {
		return fmt.Errorf("phylogeny id %s alredy in use", phy.Id)
	}
	phy.Name = strings.Join(strings.Fields(phy.Name), " ")
	phy.Root = ""
	ext := phy.Extern
	phy.Extern = nil
	for _, e := range ext {
		serv, id, err := jdh.ParseExtern(e)
		if err != nil {
			continue
		}
		if len(id) == 0 {
			continue
		}
		add := true
		for _, ex := range phy.Extern {
			if strings.HasPrefix(ex, serv) {
				add = false
				break
			}
		}
		if !add {
			continue
		}
		if _, ok := tr.ids[e]; !ok {
			phy.Extern = append(phy.Extern, e)
		}
	}
	return nil
}