-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
124 lines (120 loc) · 3.15 KB
/
parser.go
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
package main
import (
"bufio"
"os"
"regexp"
"strconv"
)
// ParsePDB takes as input a pdb file and returns a list of Atom objects
func ParsePDB(pdbFile string) []*Atom {
atoms := make([]*Atom, 0)
f, _ := os.Open(pdbFile)
defer f.Close()
scanner := bufio.NewScanner(f)
current_ind := 0
current_seq_ind := 0
for scanner.Scan() {
re := regexp.MustCompile(`\s+`)
line := scanner.Text()
parts := re.Split(line, -1)
if parts[0] != "ATOM" {
continue
}
number, _ := strconv.Atoi(parts[1])
element := parts[2]
amino := parts[3]
if amino == "MET" || len(amino) == 4 {
continue
}
chain := parts[4]
if onlyChainA && chain != "A" {
break
}
seqIndex, _ := strconv.Atoi(parts[5])
x, _ := strconv.ParseFloat(parts[6], 64)
y, _ := strconv.ParseFloat(parts[7], 64)
y *= -1.0
z, _ := strconv.ParseFloat(parts[8], 64)
// excludes atoms that are not part of the protein backbone
if element == "CA" || element == "N" || element == "O" || element == "S" {
radius := 0.0
// radii based on Pauling radii
if element == "CA" {
radius = 1.7
} else if element == "N" {
radius = 1.55
} else if element == "O" {
radius = 1.52
} else if element == "S" {
radius = 1.8
}
newAtom := &Atom{number, element, amino, chain, current_ind, x, y, z, radius}
atoms = append(atoms, newAtom)
}
if current_seq_ind != seqIndex {
current_seq_ind = seqIndex
current_ind++
}
}
return atoms
}
func ParseCamera(cameraFile string) *Camera {
f, _ := os.Open(cameraFile)
defer f.Close()
scanner := bufio.NewScanner(f)
cam := &Camera{}
for scanner.Scan() {
re := regexp.MustCompile(`\s+`)
line := scanner.Text()
parts := re.Split(line, -1)
if parts[0] == "CAMERA" {
continue
}
for i := 0; i < len(parts); i++ {
if parts[i] == "pos" {
cam.position.x, _ = strconv.ParseFloat(parts[i+1], 64)
cam.position.y, _ = strconv.ParseFloat(parts[i+2], 64)
cam.position.z, _ = strconv.ParseFloat(parts[i+3], 64)
break
} else if parts[i] == "focal_Length" {
cam.focalLength, _ = strconv.ParseFloat(parts[i+1], 64)
break
} else if parts[i] == "viewport_height" {
cam.viewportHeight, _ = strconv.ParseFloat(parts[i+1], 64)
break
} else if parts[i] == "camera_radius" {
cam.radius, _ = strconv.ParseFloat(parts[i+1], 64)
} else if parts[i] == "camera_yaw" {
cam.yaw, _ = strconv.ParseFloat(parts[i+1], 64)
} else if parts[i] == "camera_pitch" {
cam.pitch, _ = strconv.ParseFloat(parts[i+1], 64)
} else if parts[i] == "camera_speed" {
cam.speed, _ = strconv.ParseFloat(parts[i+1], 64)
}
}
}
return cam
}
func ParseLight(lightFile string) *Light {
f, _ := os.Open(lightFile)
defer f.Close()
scanner := bufio.NewScanner(f)
light := &Light{}
for scanner.Scan() {
re := regexp.MustCompile(`\s+`)
line := scanner.Text()
parts := re.Split(line, -1)
if parts[0] == "LIGHT" {
continue
}
for i := 0; i < len(parts); i++ {
if parts[i] == "pos" {
light.position.x, _ = strconv.ParseFloat(parts[i+1], 64)
light.position.y, _ = strconv.ParseFloat(parts[i+2], 64)
light.position.z, _ = strconv.ParseFloat(parts[i+3], 64)
break
}
}
}
return light
}