summaryrefslogtreecommitdiff
path: root/obo.go
blob: c02ab3e76b265a718a1c03f8ccc47949fb5cb8aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* See LICENSE file for copyright and license details. */

package obo

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strings"
)

type OboTermEntry struct {
	Id       string
	Altids   []string
	Name     string
	Def      string
	Xrefs    []string
	Synonyms []string
	IsA      []string
	Obsolete bool
}

// Use this function to filter the OboTermEntry pointer slice
func Filter(s []*OboTermEntry, fn func(*OboTermEntry) bool) []*OboTermEntry {
	var p []*OboTermEntry
	for _, i := range s {
		if fn(i) {
			p = append(p, i)
		}
	}
	return p
}

// This function prints a simple representation of the parsed Obo
// data. It is used for debugging purposes and may be removed at a later
// stage.
func Dump(oboent []*OboTermEntry, parentchildrenmap map[string][]*OboTermEntry) {
	var potentialroots []string
	for _, entry := range oboent {
		if len(entry.IsA) == 0 {
			potentialroots = append(potentialroots, entry.Id)
		}

		fmt.Printf("%s\n\tPT %s\n", entry.Id, entry.Name)
		if len(entry.Synonyms) > 0 {
			fmt.Print("\tSYN ")
			fmt.Print(strings.Join(entry.Synonyms, "\n\tSYN "))
			fmt.Print("\n")
		}
		if children, ok := parentchildrenmap[entry.Id]; ok {
			fmt.Print("\tNT ")
			for _, child := range children {
				fmt.Print(child.Id, "\n\tNT ")
			}
			fmt.Print("\n")
		}
		fmt.Print("\n")
	}
	fmt.Fprintf(os.Stderr, "Number of entries in the list: %d\n", len(oboent))
	fmt.Fprintf(os.Stderr, "Number of entries with children: %d\n", len(parentchildrenmap))
	fmt.Fprintf(os.Stderr, "Number of orphan nodes: %d\n", len(potentialroots))

	fmt.Print("root\n\tPT YourOntologyNameHere\n")
	for _, potroot := range potentialroots {
		fmt.Printf("\tNT %s\n", potroot)
	}
	fmt.Print("\n")
}

func parseObo(oboinput bufio.Reader, obochan chan *OboTermEntry, parentchildren map[string][]*OboTermEntry) {
	var entry *OboTermEntry
	var termsstarted bool

	lineno := 0
	rep := strings.NewReplacer("\"", "")
	defer close(obochan)

	for {
		line, err := oboinput.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			fmt.Printf("Error while reading obo file at line nr. %d: %v\n", lineno, err)
			os.Exit(1)
		}
		lineno++
		line = line[:len(line)-1] // chop \n
		if lineno%1000000 == 0 {
			fmt.Fprintf(os.Stderr, "Chopped line number: %d\n", lineno)
		}

		switch line {
		case "[Term]":
			termsstarted = true
			if entry != nil {
				obochan <- entry
			}

			entry = new(OboTermEntry)
			continue
		case "\n":
			continue
		case "[Typedef]":
			continue
		case "":
			continue
		default:
			if line[0] == '!' {
				continue
			}
		}

		if !termsstarted {
			continue
		}
		splitline := strings.SplitN(line, ":", 2)
		trimmedvalue := strings.Trim(splitline[1], " ")
		field := strings.Trim(splitline[0], " ")
		switch field {
		case "id":
			entry.Id = trimmedvalue
		case "name":
			entry.Name = trimmedvalue
		case "def":
			entry.Def = trimmedvalue
		case "alt_id":
			entry.Altids = append(entry.Altids, trimmedvalue)
		case "xref":
			entry.Xrefs = append(entry.Xrefs, trimmedvalue)
		case "synonym":
			syn := strings.SplitN(trimmedvalue, "\" ", 2)
			entry.Synonyms = append(entry.Synonyms, rep.Replace(syn[0]))
		case "is_a":
			isa := strings.SplitN(trimmedvalue, "!", 2)
			trimmedisa := strings.Trim(isa[0], " ")
			entry.IsA = append(entry.IsA, trimmedisa)
			if parentchildren != nil {
				parentchildren[trimmedisa] = append(parentchildren[trimmedisa], entry)
			}
		case "is_obsolete":
			entry.Obsolete = true
		}
	}
	obochan <- entry
}

// Parses a .obo file given as a bufio.Reader into a slice of
// OboTermEntry's. Hierarchical information is saved in a map and
// returned together with the slice.
func ParseToSlice(oboinput bufio.Reader, parentchildren map[string][]*OboTermEntry, obolist []*OboTermEntry) ([]*OboTermEntry, map[string][]*OboTermEntry) {
	var ent *OboTermEntry
	obochan := make(chan *OboTermEntry, 100)

	go parseObo(oboinput, obochan, parentchildren)

	for ent = range obochan {
		obolist = append(obolist, ent)
	}
	return obolist, parentchildren
}

// This function returns a channel on which pointers to the parsed
// OboTermEntry structs will be sent. Please note that this function
// does not return the hierarchy map. If you want to parse the .obo
// file asynchronously while still having access to the hierarchical
// information you will have to build the structure containing the
// hierarchical information yourself.
func ParseToChannel(oboinput bufio.Reader, obochan chan *OboTermEntry) chan *OboTermEntry {

	go parseObo(oboinput, obochan, nil)

	return obochan
}