-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature.js
More file actions
60 lines (52 loc) · 1.6 KB
/
feature.js
File metadata and controls
60 lines (52 loc) · 1.6 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
import _ from 'underscore';
import SequenceRange from './range';
class SequenceFeature {
// name: String;
// desc: String;
// ranges: Array; // Array of `SequenceRange`s
// _type: String;
// _id: Integer; // May be undefined
// note, gene, protein, product, bound_moiety, may also be present.
constructor({name, desc, ranges, _type, _id, other}) {
this.name = name || '';
this.desc = desc || '';
this.ranges = _.map((ranges || []), (range) => {
return (range instanceof SequenceRange) ? range : new SequenceRange(range);
});
this._type = _type || '';
this._id = _id || _.uniqueId();
_.each(_.pairs(other || {}), (pair) => {
let key = pair[0];
if(this[key]) throw new Error(`Attribute "${key}" already exists on SequenceFeature.`);
this[key] = pair[1];
});
}
}
/**
* @method newFromOld
* Creates a SequenceFeature instance from the old feature objects.
* @param {Object} oldFeature
* @return {SequenceFeature}
*/
SequenceFeature.newFromOld = function(oldFeature) {
oldFeature = _.deepClone(oldFeature);
var ranges = _.map(oldFeature.ranges, function(range) {
return SequenceRange.newFromOld(range);
});
var other = _.reduce(_.pairs(oldFeature), function(accum, val) {
let key = val[0];
if(!_.contains(['name', '_id', 'desc', '_type', 'ranges'], key)) {
accum[key] = val[1];
}
return accum;
}, {});
return new SequenceFeature({
name: oldFeature.name,
desc: oldFeature.desc,
ranges: ranges,
_type: oldFeature._type,
_id: oldFeature._id,
other: other
});
};
export default SequenceFeature;