97 lines
3.1 KiB
Go
97 lines
3.1 KiB
Go
package drugs
|
|
|
|
import (
|
|
"github.com/antchfx/xmlquery"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
|
|
func LoadDatabase(path string) XMLDrugsDatabase {
|
|
f, err := os.Open(path)
|
|
if err == nil {
|
|
doc, err := xmlquery.Parse(f)
|
|
if err == nil {
|
|
return XMLDrugsDatabase{doc}
|
|
}
|
|
}
|
|
return XMLDrugsDatabase{}
|
|
}
|
|
|
|
|
|
func populate_druginfo(node *xmlquery.Node) (di DrugInfo) {
|
|
di.ID = node.SelectElement("drugbank-id").InnerText()
|
|
di.Name = node.SelectElement("name").InnerText()
|
|
di.HalfLife = node.SelectElement("half-life").InnerText()
|
|
di.Description = node.SelectElement("description").InnerText()
|
|
di.Indication = node.SelectElement("indication").InnerText()
|
|
di.Pharmacodynamics = node.SelectElement("pharmacodynamics").InnerText()
|
|
di.Toxicity = node.SelectElement("toxicity").InnerText()
|
|
di.Absorption = node.SelectElement("absorption").InnerText()
|
|
di.MechanismOfAction = node.SelectElement("mechanism-of-action").InnerText()
|
|
|
|
interactions := node.SelectElement("drug-interactions").SelectElements("drug-interaction")
|
|
for _, interaction := range interactions {
|
|
di.Interactions = append(di.Interactions, DrugInteraction{interaction.SelectElement("drugbank-id").InnerText()})
|
|
}
|
|
|
|
di.DrugType = node.SelectAttr("type")
|
|
|
|
classification := node.SelectElement("classification")
|
|
if classification != nil {
|
|
di.Classification = Classification{
|
|
classification.SelectElement("description").InnerText(),
|
|
classification.SelectElement("direct-parent").InnerText(),
|
|
classification.SelectElement("kingdom").InnerText(),
|
|
classification.SelectElement("superclass").InnerText(),
|
|
classification.SelectElement("class").InnerText(),
|
|
classification.SelectElement("subclass").InnerText()}
|
|
}
|
|
groups := node.SelectElement("groups").SelectElements("group")
|
|
di.Groups = make(map[string]bool)
|
|
for _, group := range groups {
|
|
di.Groups[group.InnerText()] = true
|
|
}
|
|
|
|
salts := node.SelectElement("salts").SelectElements("salt")
|
|
for _, salt := range salts {
|
|
di.Salts = append(di.Salts, Salt{salt.SelectElement("drugbank-id").InnerText(),
|
|
salt.SelectElement("name").InnerText()})
|
|
}
|
|
|
|
pathways := node.SelectElement("pathways").SelectElements("pathway")
|
|
for _, pathway := range pathways {
|
|
pathwaysdrugs := []string{}
|
|
drugs := pathway.SelectElement("drugs").SelectElements("drug")
|
|
for _,drug := range drugs {
|
|
pathwaysdrugs = append(pathwaysdrugs, drug.SelectElement("drugbank-id").InnerText())
|
|
}
|
|
di.Pathways = append(di.Pathways, PathWay{pathway.SelectElement("smpdb-id").InnerText(), pathwaysdrugs})
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
|
|
type XMLDrugsDatabase struct {
|
|
root *xmlquery.Node
|
|
}
|
|
|
|
func (dd XMLDrugsDatabase) QueryDatabaseByID(ID string) (di DrugInfo) {
|
|
result := xmlquery.FindOne(dd.root, "//drugbank/drug[./drugbank-id='"+ID+"']")
|
|
return populate_druginfo(result)
|
|
}
|
|
|
|
func (dd XMLDrugsDatabase) QueryDatabaseByIndex(index int) (di DrugInfo) {
|
|
result := xmlquery.FindOne(dd.root, "//drugbank/drug["+strconv.Itoa(index)+"]")
|
|
return populate_druginfo(result)
|
|
}
|
|
|
|
func (dd XMLDrugsDatabase) QueryDatabaseByName(name string) (di DrugInfo) {
|
|
result := xmlquery.Find(dd.root, "//drugbank/drug[./name='"+name+"']")
|
|
if len(result) < 1 {
|
|
return
|
|
}
|
|
return populate_druginfo(result[0])
|
|
}
|