Browse Source

add label_seq_id when undefined- serial no, 1-based for each chain, see #72

Alexander Rose 4 years ago
parent
commit
3a200fe2d7
2 changed files with 32 additions and 7 deletions
  1. 16 2
      src/mol-model-formats/structure/basic/atomic.ts
  2. 16 5
      src/mol-theme/color/sequence-id.ts

+ 16 - 2
src/mol-model-formats/structure/basic/atomic.ts

@@ -67,13 +67,27 @@ function createHierarchyData(atom_site: AtomSite, sourceIndex: Column<number>, o
     });
 
     const residues = Table.view(atom_site, ResiduesSchema, offsets.residues);
+    const chains = Table.view(atom_site, ChainsSchema, offsets.chains);
+
+    if (!residues.label_seq_id.isDefined) {
+        const seqIds = new Int32Array(residues.label_seq_id.rowCount);
+        const { residues: residueOffsets, chains: chainOffsets } = offsets;
+        let cI = 0;
+        let seqId = 0;
+        for (let i = 0, il = seqIds.length; i < il; ++i) {
+            if (chainOffsets[cI] > residueOffsets[i]) {
+                cI += 1;
+                seqId = 0;
+            }
+            seqIds[i] = ++seqId;
+        }
+        residues.label_seq_id = Column.ofIntArray(seqIds);
+    }
 
     // Optimize the numeric columns
     Table.columnToArray(residues, 'label_seq_id', Int32Array);
     Table.columnToArray(residues, 'auth_seq_id', Int32Array);
 
-    const chains = Table.view(atom_site, ChainsSchema, offsets.chains);
-
     // Fix possibly missing auth_/label_ columns
     substUndefinedColumn(atoms, 'label_atom_id', 'auth_atom_id');
     substUndefinedColumn(atoms, 'label_comp_id', 'auth_comp_id');

+ 16 - 5
src/mol-theme/color/sequence-id.ts

@@ -58,9 +58,14 @@ function getSequenceLength(unit: Unit, element: ElementIndex) {
             break;
     }
     if (entityId === '') return 0;
+
     const entityIndex = model.entities.getEntityIndex(entityId);
     if (entityIndex === -1) return 0;
-    return model.sequence.byEntityKey[entityIndex].sequence.length;
+
+    const entity = model.sequence.byEntityKey[entityIndex];
+    if (entity === undefined) return 0;
+
+    return entity.sequence.length;
 }
 
 export function SequenceIdColorTheme(ctx: ThemeDataContext, props: PD.Values<SequenceIdColorThemeParams>): ColorTheme<SequenceIdColorThemeParams> {
@@ -74,15 +79,21 @@ export function SequenceIdColorTheme(ctx: ThemeDataContext, props: PD.Values<Seq
             const { unit, element } = location;
             const seq_id = getSeqId(unit, element);
             if (seq_id > 0) {
-                scale.setDomain(0, getSequenceLength(unit, element) - 1);
-                return scale.color(seq_id);
+                const seqLen = getSequenceLength(unit, element);
+                if (seqLen) {
+                    scale.setDomain(0, seqLen - 1);
+                    return scale.color(seq_id);
+                }
             }
         } else if (Bond.isLocation(location)) {
             const { aUnit, aIndex } = location;
             const seq_id = getSeqId(aUnit, aUnit.elements[aIndex]);
             if (seq_id > 0) {
-                scale.setDomain(0, getSequenceLength(aUnit, aUnit.elements[aIndex]) - 1);
-                return scale.color(seq_id);
+                const seqLen = getSequenceLength(aUnit, aUnit.elements[aIndex]);
+                if (seqLen) {
+                    scale.setDomain(0, seqLen - 1);
+                    return scale.color(seq_id);
+                }
             }
         }
         return DefaultColor;