-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCF_functions.go
More file actions
427 lines (365 loc) · 12.7 KB
/
Copy pathCF_functions.go
File metadata and controls
427 lines (365 loc) · 12.7 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Group 2: Ab Initio Secondary Structure Prediction of Proteins
// Date: 12th December, 2024
// Members: Arth Banka, Riti Bhatia, Sanchitha Kuthethoor, Sumeet Kothare
package main
// ChouFasmanPredictSS()
// Input: a slice of runes, each elements corresponds to an amino acid residue
// Output: a string that predicts the secondary structure of a protein by employing the Chou-Fasman model
func ChouFasmanPredictSS(sequence []rune) string {
length := len(sequence)
result := make([]rune, length)
// Default for all residues is coil
for i := range result {
result[i] = 'C'
}
// First, predict for each structure type respectively
helixRegions := PredictHelix(sequence)
sheetRegions := PredictSheet(sequence)
turnRegions := PredictTurn(sequence)
// Resolve the overlapping regions and assign structure types to the result slice accordingly
ClassifyOverlap(sequence, helixRegions, sheetRegions, turnRegions, result)
return string(result)
}
// PredictHelix()
// Input: a slice of runes sequence
// Output: a slice of Region datatypes of all the regions with high propensities of being an alpha helix
func PredictHelix(sequence []rune) []Region {
var regions []Region
// Slide a 6-residue window across the sequence
for i := 0; i <= len(sequence)-6; i++ {
if IsHelix(sequence[i : i+6]) { // Check 6-residue window
// If it's a potential helix nucleation site, extend it from either sides
start, end, score := ExtendHelix(sequence, i)
regions = append(regions, Region{start, end, score, 'H'}) //Append the helix region
i = end - 1 // Skip ahead to avoid overlapping predictions
}
}
return regions
}
// IsHelix()
// Input: a window of length 6 (slice of runes)
// Output: a boolean after checking if the window (nucleation site) has a high propensity of forming an alpha helix
func IsHelix(window []rune) bool {
if len(window) < 6 {
return false
}
formerCount := 0.0
breakerCount := 0
avgPropensity := 0.0
// Analyze each amino acid in the window
for _, aa := range window {
// Immediately return false if Proline is found
if aa == 'P' {
return false
}
prop := propensities[aa]
avgPropensity += prop.alphaHelix
// Count helix formers and breakers
if prop.alphaHelix >= 1.05 {
formerCount += 1.0 // Strong former
} else if prop.alphaHelix >= 1.0 {
formerCount += 0.5 // Weak former
} else if prop.alphaHelix <= 0.69 {
breakerCount++
} else if aa == 'P' {
return false
}
}
// Calculate the average propensity
avgPropensity /= float64(len(window))
// Check if the window meets helix formation criteria
return formerCount >= 4.0 &&
breakerCount < 2 &&
avgPropensity >= 1.03
}
// PredictSheet()
// Input: a slice of runes sequence
// Output: a slice of Region datatypes of all the regions with high propensities of being a beta sheet
func PredictSheet(sequence []rune) []Region {
var regions []Region
// Slide a 5-residue window across the sequence
for i := 0; i <= len(sequence)-5; i++ { // 5 residue window
if IsSheet(sequence[i : i+5]) {
// If it's a potential sheet nucleation site, extend the region
start, end, score := ExtendSheet(sequence, i)
regions = append(regions, Region{start, end, score, 'E'}) // Append sheet region
i = end - 1
}
}
return regions
}
// IsSheet()
// Input: a window of length 5 (slice of runes)
// Output: a boolean after checking if the window (nucleation site) has a high propensity of forming a beta sheet
func IsSheet(window []rune) bool {
if len(window) < 5 {
return false
}
formerCount := 0
breakerCount := 0
avgPropensity := 0.0
// Analyze each amino acid in the window
for _, aa := range window {
prop := propensities[aa]
avgPropensity += prop.betaSheet
// Count sheet formers and breakers
if prop.betaSheet >= 1.0 {
formerCount++ // Former
} else if prop.betaSheet <= 0.75 {
breakerCount++ // Breaker
}
}
// Calculate average propensity
avgPropensity /= float64(len(window))
// Check if the window meets sheet formation criteria
return formerCount >= 3 &&
breakerCount <= 1 &&
avgPropensity >= 1.05
}
// ExtendHelix()
// Input: the sequence which is a slice of runes, and a int start corresponding to an index value in the slice
// Output: the start and end ints of the helix, and a float corresponding to the score (avg propensity)
func ExtendHelix(sequence []rune, start int) (int, int, float64) {
// Start with initial 6-residue nucleation site
currentStart := start
currentEnd := start + 6
// Continue extending as long as possible in either direction
for {
canStillExtend := false
// Try extending forward if not at sequence end
if currentEnd < len(sequence) {
forwardRegion := sequence[currentStart : currentEnd+1]
forwardProp := CalculateAveragePropensity(forwardRegion, 'H')
// Check for tetrapeptide breakers in the forward direction
hasBreaker := false
if currentEnd+4 <= len(sequence) {
for i := currentEnd - 3; i <= currentEnd; i++ {
tetrapeptide := sequence[i : i+4]
tetraProp := CalculateAveragePropensity(tetrapeptide, 'H')
if tetraProp < 1.00 {
hasBreaker = true
break // If set of tetrapeptide breakers identified, then break the loop
}
}
}
// Extend if meets propensity and no breakers
if forwardProp >= 1.03 && !hasBreaker {
currentEnd++
canStillExtend = true
}
}
// Try extending backward if not at sequence start
if currentStart > 0 {
backwardRegion := sequence[currentStart-1 : currentEnd]
backwardProp := CalculateAveragePropensity(backwardRegion, 'H')
// Check for tetrapeptide breakers in the backward direction
hasBreaker := false
if currentStart >= 3 {
for i := currentStart - 3; i <= currentStart; i++ {
if i+4 <= currentEnd {
tetrapeptide := sequence[i : i+4]
tetraProp := CalculateAveragePropensity(tetrapeptide, 'H')
if tetraProp < 1.00 {
hasBreaker = true
break
}
}
}
}
// Extend if meets propensity and no breakers
if backwardProp >= 1.03 && !hasBreaker {
currentStart--
canStillExtend = true
}
}
// Stop if no more extension possible
if !canStillExtend {
break
}
}
// Calculate final region score
finalRegion := sequence[currentStart:currentEnd]
totalScore := CalculateAveragePropensity(finalRegion, 'H')
return currentStart, currentEnd, totalScore
}
// ExtendSheet()
// Input: the sequence which is a slice of runes, and a int start corresponding to an index value in the slice
// Output: the start and end ints of the sheet, and a float corresponding to the score (avg propensity)
func ExtendSheet(sequence []rune, start int) (int, int, float64) {
// Start with initial 5-residue nucleus
currentStart := start
currentEnd := start + 5
// Continue extending as long as possible in either direction
for {
canStillExtend := false
// Try extending forward if not at sequence end
if currentEnd < len(sequence) {
forwardRegion := sequence[currentStart : currentEnd+1]
forwardProp := CalculateAveragePropensity(forwardRegion, 'E')
// Check for tetrapeptide breakers in the forward direction
hasBreaker := false
if currentEnd+4 <= len(sequence) {
for i := currentEnd - 3; i <= currentEnd; i++ {
tetrapeptide := sequence[i : i+4]
tetraProp := CalculateAveragePropensity(tetrapeptide, 'E')
if tetraProp < 1.00 {
hasBreaker = true
break
}
}
}
// Extend if meets propensity and no breakers
if forwardProp >= 1.05 && !hasBreaker {
currentEnd++
canStillExtend = true
}
}
// Try extending backward if not at sequence start
if currentStart > 0 {
backwardRegion := sequence[currentStart-1 : currentEnd]
backwardProp := CalculateAveragePropensity(backwardRegion, 'E')
// Check for tetrapeptide breakers in the backward direction
hasBreaker := false
if currentStart >= 3 {
for i := currentStart - 3; i <= currentStart; i++ {
if i+4 <= currentEnd {
tetrapeptide := sequence[i : i+4]
tetraProp := CalculateAveragePropensity(tetrapeptide, 'E')
if tetraProp < 1.00 {
hasBreaker = true
break
}
}
}
}
// Extend if meets propensity and no breakers
if backwardProp >= 1.05 && !hasBreaker {
currentStart--
canStillExtend = true
}
}
// Stop if no more extension possible
if !canStillExtend {
break
}
}
// Calculate final region score
finalRegion := sequence[currentStart:currentEnd]
totalScore := CalculateAveragePropensity(finalRegion, 'E')
return currentStart, currentEnd, totalScore
}
// PredictTurn()
// Input: sequence which is a slice of runes
// Output: a slice of Region datatypes of all the regions likely of being turns
func PredictTurn(sequence []rune) []Region {
var regions []Region
// Slide a 4-residue window across the sequence
for i := 0; i <= len(sequence)-4; i++ {
if IsTurn(sequence[i : i+4]) { // 4 residue window
regions = append(regions, Region{i, i + 4, CalculateAveragePropensity(sequence[i:i+4], 'T'), 'T'}) //Append the calculated score
}
}
return regions
}
// IsTurn()
// Input: a window of length 4 (slice of runes)
// Output: a boolean after checking if the window (nucleation site) has a high propensity of being a turn
func IsTurn(window []rune) bool {
if len(window) < 4 {
return false
}
// Calculate positional probability of tetrapeptide being a turn
pt := bendProbabilitiesTable[window[0]].p1 *
bendProbabilitiesTable[window[1]].p2 *
bendProbabilitiesTable[window[2]].p3 *
bendProbabilitiesTable[window[3]].p4
// Ensure the two middle residues meet a minimum bend probability
if propensities[window[1]].turn < 0.5 || propensities[window[2]].turn < 0.5 {
return false
}
// Calculate the average turn propensity
avgPropensity := CalculateAveragePropensity(window, 'T')
// Check if the window meets turn formation criteria
return avgPropensity >= 1.0 && pt >= 0.000075
}
// ClassifyOverlap()
// Input: sequence as a slice of runes, the helix Regions, sheet Regions, and turn Regions which are all slices of Region datatypes
// and result which is a slice of rines as well
// Output: it does not output anything, but assigns the overlapping helix/sheet/turn regions based on which has the highest propensity scores
func ClassifyOverlap(sequence []rune, helixRegions, sheetRegions, turnRegions []Region, result []rune) {
// Combine all regions into a single slice
allRegions := make([]Region, 0)
allRegions = append(allRegions, helixRegions...)
allRegions = append(allRegions, sheetRegions...)
allRegions = append(allRegions, turnRegions...)
// First mark all regions with their respective structures
for _, region := range allRegions {
for i := region.start; i < region.end; i++ {
result[i] = region.structure
}
}
// Handle overlaps by comparing regions and selecting based on score
for i, region1 := range allRegions {
// Only compare with regions that come after region1 to avoid redundant comparisons
for _, region2 := range allRegions[i+1:] {
if Overlap(region1, region2) {
overlapStart := Max(region1.start, region2.start)
overlapEnd := Min(region1.end, region2.end)
// Choose structure with higher score
structure := region1.structure
if region2.score > region1.score {
structure = region2.structure
}
// Update the overlapping region
for i := overlapStart; i < overlapEnd; i++ {
result[i] = structure
}
}
}
}
}
// CalculateAveragePropensity()
// Input: window which is a slice of runes, and a structureType rune
// Output: the avergage propensities of all the residues in the window for being that structure type as a float64
func CalculateAveragePropensity(window []rune, structureType rune) float64 {
if len(window) == 0 {
return 0.0
}
sum := 0.0
for _, aa := range window {
prop := propensities[aa]
switch structureType {
case 'H':
sum += prop.alphaHelix
case 'E':
sum += prop.betaSheet
case 'T':
sum += prop.turn
}
}
return sum / float64(len(window))
}
// Overlap()
// Input: two Regions
// Output: a boolean corresponding to whether the two regions overlap with each other
func Overlap(r1, r2 Region) bool {
return r1.start < r2.end && r2.start < r1.end
}
// Max()
// Input: two integers a and b
// Ouput: the largest integer between the two
func Max(a, b int) int {
if a > b {
return a
}
return b
}
// Min()
// Input: two integers a and b
// Ouput: the smallest integer between the two
func Min(a, b int) int {
if a < b {
return a
}
return b
}