Skip to content
Snippets Groups Projects
Unverified Commit 60b5d2d3 authored by David Sehnal's avatar David Sehnal Committed by GitHub
Browse files

fix labels & optimize getData (#811)

parent eb749a2a
No related branches found
No related tags found
No related merge requests found
import { StructureElement, StructureProperties } from '../../../mol-model/structure'; import { StructureElement, StructureProperties } from '../../../mol-model/structure';
import { LociLabel } from '../../../mol-plugin-state/manager/loci-label'; import { LociLabel } from '../../../mol-plugin-state/manager/loci-label';
import { SbNcbrPartialChargesPropertyProvider } from './property'; import { SbNcbrPartialChargesPropertyProvider, hasPartialChargesCategories } from './property';
import { Loci } from '../../../mol-model/loci'; import { Loci } from '../../../mol-model/loci';
import { PluginContext } from '../../../mol-plugin/context'; import { PluginContext } from '../../../mol-plugin/context';
import { LociLabelProvider } from '../../../mol-plugin-state/manager/loci-label'; import { LociLabelProvider } from '../../../mol-plugin-state/manager/loci-label';
...@@ -11,6 +11,7 @@ export function SbNcbrPartialChargesLociLabelProvider(ctx: PluginContext): LociL ...@@ -11,6 +11,7 @@ export function SbNcbrPartialChargesLociLabelProvider(ctx: PluginContext): LociL
if (!StructureElement.Loci.is(loci)) return; if (!StructureElement.Loci.is(loci)) return;
const model = loci.structure.model; const model = loci.structure.model;
if (!hasPartialChargesCategories(model)) return;
const data = SbNcbrPartialChargesPropertyProvider.get(model).value; const data = SbNcbrPartialChargesPropertyProvider.get(model).value;
if (!data) return; if (!data) return;
......
...@@ -15,44 +15,60 @@ export interface SBNcbrPartialChargeData { ...@@ -15,44 +15,60 @@ export interface SBNcbrPartialChargeData {
maxAbsoluteAtomCharges: IdToCharge; maxAbsoluteAtomCharges: IdToCharge;
maxAbsoluteResidueCharges: IdToCharge; maxAbsoluteResidueCharges: IdToCharge;
maxAbsoluteAtomChargeAll: number; maxAbsoluteAtomChargeAll: number;
params: PartialChargesPropertyParams;
} }
const PartialChargesPropertyParams = { const PartialChargesPropertyParams = {
typeId: PD.Select<number>(0, [[0, '0']]), typeId: PD.Select<number>(0, [[0, '0']]),
}; };
type PartialChargesPropertyParams = typeof PartialChargesPropertyParams; type PartialChargesPropertyParams = typeof PartialChargesPropertyParams;
const defaultPartialChargesPropertyParams = PD.clone(PartialChargesPropertyParams); const DefaultPartialChargesPropertyParams = PD.clone(PartialChargesPropertyParams);
function getParams(model: Model) { function getParams(model: Model) {
const typeIdToMethod = getTypeIdToMethod(model); return getData(model).value?.params ?? DefaultPartialChargesPropertyParams;
const options = Array.from(typeIdToMethod.entries()).map(
([typeId, method]) => [typeId, method] as [number, string]
);
return {
typeId: PD.Select<number>(1, options),
};
} }
async function getData(model: Model): Promise<CustomProperty.Data<SBNcbrPartialChargeData | undefined>> { const PropertyKey = 'sb-ncbr-partial-charges-property-data';
if (!SbNcbrPartialChargesPropertyProvider.isApplicable(model)) return { value: undefined };
function getData(model: Model): CustomProperty.Data<SBNcbrPartialChargeData | undefined> {
const typeIdToMethod = getTypeIdToMethod(model); if (PropertyKey in model._staticPropertyData) {
const typeIdToAtomIdToCharge = getTypeIdToAtomIdToCharge(model); return model._staticPropertyData[PropertyKey];
const typeIdToResidueToCharge = getTypeIdToResidueIdToCharge(model, typeIdToAtomIdToCharge); }
const maxAbsoluteAtomCharges = getMaxAbsoluteCharges(typeIdToAtomIdToCharge);
const maxAbsoluteResidueCharges = getMaxAbsoluteCharges(typeIdToResidueToCharge); let data: CustomProperty.Data<SBNcbrPartialChargeData | undefined>;
const maxAbsoluteAtomChargeAll = getMaxAbsoluteAtomChargeAll(maxAbsoluteAtomCharges, maxAbsoluteResidueCharges);
if (!SbNcbrPartialChargesPropertyProvider.isApplicable(model)) {
return { data = { value: undefined };
value: { } else {
typeIdToMethod, const typeIdToMethod = getTypeIdToMethod(model);
typeIdToAtomIdToCharge, const typeIdToAtomIdToCharge = getTypeIdToAtomIdToCharge(model);
typeIdToResidueToCharge, const typeIdToResidueToCharge = getTypeIdToResidueIdToCharge(model, typeIdToAtomIdToCharge);
maxAbsoluteAtomCharges, const maxAbsoluteAtomCharges = getMaxAbsoluteCharges(typeIdToAtomIdToCharge);
maxAbsoluteResidueCharges, const maxAbsoluteResidueCharges = getMaxAbsoluteCharges(typeIdToResidueToCharge);
maxAbsoluteAtomChargeAll, const maxAbsoluteAtomChargeAll = getMaxAbsoluteAtomChargeAll(maxAbsoluteAtomCharges, maxAbsoluteResidueCharges);
},
}; const options = Array.from(typeIdToMethod.entries()).map(
([typeId, method]) => [typeId, method] as [number, string]
);
const params = {
typeId: PD.Select<number>(1, options),
};
data = {
value: {
typeIdToMethod,
typeIdToAtomIdToCharge,
typeIdToResidueToCharge,
maxAbsoluteAtomCharges,
maxAbsoluteResidueCharges,
maxAbsoluteAtomChargeAll,
params,
},
};
}
model._staticPropertyData[PropertyKey] = data;
return data;
} }
function getTypeIdToMethod(model: Model) { function getTypeIdToMethod(model: Model) {
...@@ -164,11 +180,11 @@ function getMaxAbsoluteAtomChargeAll( ...@@ -164,11 +180,11 @@ function getMaxAbsoluteAtomChargeAll(
export function hasPartialChargesCategories(model: Model): boolean { export function hasPartialChargesCategories(model: Model): boolean {
if (!model || !MmcifFormat.is(model.sourceData)) return false; if (!model || !MmcifFormat.is(model.sourceData)) return false;
const names = model.sourceData.data.frame.categoryNames; const { categories } = model.sourceData.data.frame;
return ( return (
names.includes('atom_site') && 'atom_site' in categories &&
names.includes('sb_ncbr_partial_atomic_charges') && 'sb_ncbr_partial_atomic_charges' in categories &&
names.includes('sb_ncbr_partial_atomic_charges_meta') 'sb_ncbr_partial_atomic_charges_meta' in categories
); );
} }
...@@ -178,11 +194,11 @@ SBNcbrPartialChargeData | undefined ...@@ -178,11 +194,11 @@ SBNcbrPartialChargeData | undefined
> = CustomModelProperty.createProvider({ > = CustomModelProperty.createProvider({
label: 'SB NCBR Partial Charges Property Provider', label: 'SB NCBR Partial Charges Property Provider',
descriptor: CustomPropertyDescriptor({ descriptor: CustomPropertyDescriptor({
name: 'sb-ncbr-property-provider', name: 'sb-ncbr-partial-charges-property-provider',
}), }),
type: 'static', type: 'static',
defaultParams: defaultPartialChargesPropertyParams, defaultParams: DefaultPartialChargesPropertyParams,
getParams: (data: Model) => getParams(data), getParams: (data: Model) => getParams(data),
isApplicable: (model: Model) => hasPartialChargesCategories(model), isApplicable: (model: Model) => hasPartialChargesCategories(model),
obtain: (_ctx: CustomProperty.Context, model: Model) => getData(model), obtain: (_ctx: CustomProperty.Context, model: Model) => Promise.resolve(getData(model)),
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment