UNPKG

3.28 kBJavaScriptView Raw
1import "source-map-support/register";
2// TODO should I get rid of the lib above for production browser build?
3/* pvjson: "id" is a required property.
4 * GPML2013a: GraphId is sometimes optional, e.g., for Groups or Anchors.
5 * GPML2017: supposed to make GraphId required.
6 *
7 * When converting GPML2013a, we fill in any missing GraphIds so that any
8 * element that MAY have a GraphId DOES have one.
9 * If the GraphId is already specified, we don't change it.
10 * If it is not specified, we want to generate one with these properties:
11 * 1) Stability/Purity: you can convert the same GPML file any number of times
12 * and always get the same pvjson output.
13 * 2) Uniqueness: don't clobber an existing GraphId in the pathway.
14 *
15 * The PathVisio algorithm for GraphId generation is basically:
16 *
17 * GraphId = RandomValue + IncrementingValue
18 *
19 * where IncrementingValue is generated by starting with a hex value of
20 * "0xa00" for the first GraphId and incrementing that value for each
21 * subsequent GraphId:
22 * https://github.com/PathVisio/pathvisio/blob/3cb194f120de550ef2e102877965bed3c54a6a75/modules/org.pathvisio.core/src/org/pathvisio/core/biopax/BiopaxElement.java#L245
23 *
24 * We want a stable output, so instead of using a random value, we use the
25 * namespace "pvjsgeneratedid", and since that's a string, we must append
26 * the IncrementingValue instead of adding it:
27 *
28 * GraphId = "pvjsgeneratedid" + IncrementingValue
29 */
30export class GraphIdManager {
31 constructor() {
32 this.namespace = "pvjsgeneratedid";
33 this.incrementingValueAsInt = parseInt("0xa00", 16);
34 }
35 generateAndRecord() {
36 this.incrementingValueAsInt += 1;
37 // NOTE: the namespace is not part of incrementingValueAsInt
38 return this.namespace + this.incrementingValueAsInt.toString(16);
39 }
40 recordExisting(graphIdAsHex) {
41 const { incrementingValueAsInt } = this;
42 const graphIdAsInt = parseInt(graphIdAsHex, 16);
43 // NOTE: this graphIdAsInt does not refer to exactly the same thing as PathVisio's
44 // IncrementingValue, because it's the sum of RandomValue and IncrementingValue.
45 if (graphIdAsInt > incrementingValueAsInt) {
46 this.incrementingValueAsInt = graphIdAsInt;
47 }
48 }
49}
50//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiR3JhcGhJZE1hbmFnZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvR3JhcGhJZE1hbmFnZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyw2QkFBNkIsQ0FBQztBQUNyQyx1RUFBdUU7QUFFdkU7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBMEJHO0FBQ0gsTUFBTSxPQUFPLGNBQWM7SUFHekI7UUFEQSxjQUFTLEdBQVcsaUJBQWlCLENBQUM7UUFFcEMsSUFBSSxDQUFDLHNCQUFzQixHQUFHLFFBQVEsQ0FBQyxPQUFPLEVBQUUsRUFBRSxDQUFDLENBQUM7SUFDdEQsQ0FBQztJQUVELGlCQUFpQjtRQUNmLElBQUksQ0FBQyxzQkFBc0IsSUFBSSxDQUFDLENBQUM7UUFDakMsNERBQTREO1FBQzVELE9BQU8sSUFBSSxDQUFDLFNBQVMsR0FBRyxJQUFJLENBQUMsc0JBQXNCLENBQUMsUUFBUSxDQUFDLEVBQUUsQ0FBQyxDQUFDO0lBQ25FLENBQUM7SUFFRCxjQUFjLENBQUMsWUFBWTtRQUN6QixNQUFNLEVBQUUsc0JBQXNCLEVBQUUsR0FBRyxJQUFJLENBQUM7UUFDeEMsTUFBTSxZQUFZLEdBQUcsUUFBUSxDQUFDLFlBQVksRUFBRSxFQUFFLENBQUMsQ0FBQztRQUNoRCxrRkFBa0Y7UUFDbEYsZ0ZBQWdGO1FBQ2hGLElBQUksWUFBWSxHQUFHLHNCQUFzQixFQUFFO1lBQ3pDLElBQUksQ0FBQyxzQkFBc0IsR0FBRyxZQUFZLENBQUM7U0FDNUM7SUFDSCxDQUFDO0NBQ0YifQ==
\No newline at end of file