/
entity.ts
96 lines (82 loc) · 2.25 KB
/
entity.ts
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
/**
* @file Entity
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @private
*/
import Structure from './structure'
import {
UnknownEntity, PolymerEntity, NonPolymerEntity, MacrolideEntity, WaterEntity
} from './structure-constants.js'
import ChainProxy from '../proxy/chain-proxy'
function entityTypeFromString (string: string) {
string = string.toLowerCase()
switch (string) {
case 'polymer':
return PolymerEntity
case 'non-polymer':
return NonPolymerEntity
case 'macrolide':
return MacrolideEntity
case 'water':
return WaterEntity
default:
return UnknownEntity
}
}
const EntityTypeString = {
'polymer': PolymerEntity,
'non-polymer': NonPolymerEntity,
'macrolide': MacrolideEntity,
'water': WaterEntity,
}
type EntityTypeString = keyof typeof EntityTypeString
/**
* Entity of a {@link Structure}
*/
export default class Entity {
structure: Structure
index: number
description: string
entityType: number
chainIndexList: number[]
/**
* @param {Structure} structure - structure the entity belongs to
* @param {Integer} index - index within structure.entityList
* @param {String} description - entity description
* @param {String} type - entity type
* @param {Array} chainIndexList - entity chainIndexList
*/
constructor (structure: Structure, index: number, description = '', type?: EntityTypeString, chainIndexList = []) {
this.structure = structure
this.index = index
this.description = description
this.entityType = entityTypeFromString(type || '')
this.chainIndexList = chainIndexList
chainIndexList.forEach(function (ci: number) {
structure.chainStore.entityIndex[ ci ] = index
})
}
get type () { return 'Entity' }
getEntityType () {
return this.entityType
}
isPolymer () {
return this.entityType === PolymerEntity
}
isNonPolymer () {
return this.entityType === NonPolymerEntity
}
isMacrolide () {
return this.entityType === MacrolideEntity
}
isWater () {
return this.entityType === WaterEntity
}
eachChain (callback: (cp: ChainProxy) => any) {
const cp = this.structure.getChainProxy()
this.chainIndexList.forEach(function (index) {
cp.index = index
callback(cp)
})
}
}