generic-sequence-panel
Version:
read feature and sequence data and produce highlighted fasta
72 lines (71 loc) • 2.05 kB
JavaScript
const jb2ToJb1 = { refName: "seq_id" };
const jb1ToJb2 = { seq_id: "refName" };
/**
* wrapper to adapt nclist features to act like jbrowse 2 features
*/
export default class NCListFeature {
constructor(ncFeature, parent, id) {
this.ncFeature = ncFeature;
this.uniqueId = id || ncFeature.id();
this.parentHandle = parent;
}
set() {
throw new Error("not implemented");
}
jb2TagToJb1Tag(tag) {
// @ts-expect-error
const mapped = jb2ToJb1[tag] || tag;
return mapped.toLowerCase();
}
jb1TagToJb2Tag(tag) {
const t = tag.toLowerCase();
// @ts-expect-error
const mapped = jb1ToJb2[t] || t;
return mapped;
}
get(attrName) {
const attr = this.ncFeature.get(this.jb2TagToJb1Tag(attrName));
if (attr && attrName === "subfeatures") {
return attr.map((subfeature) => new NCListFeature(subfeature, this));
}
return attr;
}
/**
* Get an array listing which data keys are present in this feature.
*/
tags() {
return this.ncFeature.tags().map((t) => this.jb1TagToJb2Tag(t));
}
/**
* Get the unique ID of this feature.
*/
id() {
return this.uniqueId;
}
/**
* Get this feature's parent feature, or undefined if none.
*/
parent() {
return this.parentHandle;
}
/**
* Get an array of child features, or undefined if none.
*/
children() {
return this.get("subfeatures");
}
toJSON() {
const data = { uniqueId: this.id(), subfeatures: [] };
this.ncFeature.tags().forEach((tag) => {
const mappedTag = this.jb1TagToJb2Tag(tag);
const value = this.ncFeature.get(tag);
if (mappedTag === "subfeatures") {
data.subfeatures = (value || []).map((f) => new NCListFeature(f, this).toJSON());
}
else {
data[mappedTag] = value;
}
});
return data;
}
}