# All files / src/parsers MMTF.ts

95.23% Statements 160/168
86.66% Branches 52/60
100% Functions 5/5
96.85% Lines 154/159

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316

    18x
    60x

    18x

    17745x
    16569x
    12819x

    18x

    6x
    6x
    6x
    6x

    6x
    6x

    6x

    6x

    6x

    6x
    6x

    6x
    6x
    6x
    6x

    6x
    6x
    6x
    6x
    6x
    6x
    6x
    6x

    6x
    6x
    6x

    6x
    6x
    1x
    1x
    1x
    1x
    1x

    6x

    6x
    6x
    6x

    6x
    6x
    30x
    30x

    6x

    6x
    6x
    6x
    6x

    6x
    6x

    30x
    30x

    30x
    30x

    30x
    30x
    30x

    3030x
    3030x
    3030x
    3030x
    3030x

    3030x
    3030x
    3030x
    3030x
    216x

    3030x
    3030x
    3030x
    3030x

    3030x
    3030x
    3030x

    3030x
    3030x
    3030x

    3030x
    3030x
    3030x
    3030x

    3030x

    3030x

    12126x
    12126x

    12126x
    12126x
    12126x

    12126x
    12126x
    906x

    12126x
    12126x
    12126x

    12126x
    435x
    435x

    11691x
    11691x
    11691x
    11691x
    11691x
    11691x
    11691x

    11691x
    11691x

    11691x

    3030x
    3030x
    9372x
    9372x
    9372x

    9372x
    9372x
    9372x
    9372x
    9372x
    8884x
    8884x
    8884x
    8884x

    3030x

    30x
    30x

    3030x

    30x

    6x
    6x
    1368x
    1368x
    1368x

    1368x

    1368x
    1368x
    1368x
    1368x
    1368x
    1359x
    1359x
    1359x
    1359x

    6x

    6x

    6x
    1x
    1x

    6x
    5x

    6x
     import { base64ToArray } from "../utilities";
    import { Matrix4 } from "../WebGL";
    import { computeSecondaryStructure } from "./utils/computeSecondaryStructure";
    import { processSymmetries } from "./utils/processSymmetries";

    interface MMTFobj {
        decode(data:Uint8Array|ArrayBuffer): any;
    }
    declare var MMTF: MMTFobj;

    var fromCharCode = function( charCodeArray ){
      return String.fromCharCode.apply( null, charCodeArray ).replace(/\0/g, '');
    };

    var convertSS = function(val) {
    //convert mmtf code to 3dmol code
    /*
      0:  pi helix
      1:  bend
      2:  alpha helix
      3:  sheet extended
      4:  3-10 helix
      5:  bridge
      6:  turn
      7:  coil
     */
      if(val == 0 || val == 2 || val == 4) return 'h';
      if(val == 3) return 's';
      return 'c';
    };

    let mmtfHETATMtypes = new Set([
    "D-SACCHARIDE",
    "D-SACCHARIDE 1,4 AND 1,4 LINKING",
    "D-SACCHARIDE 1,4 AND 1,6 LINKING",
    "L-SACCHARIDE",
    "L-SACCHARIDE 1,4 AND 1,4 LINKING",
    "L-SACCHARIDE 1,4 AND 1,6 LINKING",
    "NON-POLYMER",
    "OTHER",
    "PEPTIDE-LIKE",
    "SACCHARIDE" ]);

    /** @param bindata - binary UInt8Array buffer or a base64 encoded string
     *  @param ParserOptionsSpec
     *  @category Parsers
    */
    export function MMTFparser(bindata, options) {

      var noH = !options.keepH; // suppress hydrogens by default
      var selAltLoc = options.altLoc ? options.altLoc : 'A'; //default alternate location to select if present
      var ignoreStruct = !!options.noSecondaryStructure;
      var computeStruct = !options.noComputeSecondaryStructure;
      //extract symmetries - only take first assembly, apply to all models (ignoring changes for now)
      var noAssembly = !options.doAssembly; // don't assemble by default
      var assemblyIndex = options.assemblyIndex ? options.assemblyIndex : 0;

      if(typeof(bindata) == "string") {
          //assume base64 encoded
          bindata = base64ToArray(bindata);
      }

      var mmtfData = MMTF.decode( bindata );

      var atoms: any[][] & Record<string, any> = [[]];
      var modelData: any[] = atoms.modelData = [];

      // setup index counters
      var modelIndex = 0;
      var chainIndex = 0;
      var groupIndex = 0;
      var atomIndex = 0;

      // setup optional fields
      var secStructList = mmtfData.secStructList;
      var insCodeList = mmtfData.insCodeList;
      var sequenceIndexList = mmtfData.sequenceIndexList;
      var bFactorList = mmtfData.bFactorList;
      var altLocList = mmtfData.altLocList;
      var occupancyList = mmtfData.occupancyList;
      var bondAtomList = mmtfData.bondAtomList;
      var bondOrderList = mmtfData.bondOrderList;

      var numModels = mmtfData.numModels;
      Iif (numModels == 0) return atoms;
      if (!options.multimodel) numModels = 1; //first only
      // hoisted loop variables
      var i, j, k, kl, m, n;

      var symmetries: Matrix4[] = [];
      if(!noAssembly && mmtfData.bioAssemblyList && mmtfData.bioAssemblyList.length > 0) {
          var transforms = mmtfData.bioAssemblyList[assemblyIndex].transformList;
          for(i = 0, n = transforms.length; i < n; i++) {
              var matrix = new Matrix4(transforms[i].matrix);
              matrix.transpose();
              symmetries.push(matrix);
          }
      }
      var unitCell = null as Record<string, number> | null;
      //unit cell info
      if(mmtfData.unitCell) {
          var u = mmtfData.unitCell;
          unitCell = {'a' : u[0], 'b' : u[1], 'c' : u[2], 'alpha' : u[3], 'beta' : u[4], 'gamma' : u[5]};
      }

      let chainIsPolymer: boolean[] = [];
      mmtfData.entityList.forEach(entity => {
          entity.chainIndexList.forEach(ch => {
              chainIsPolymer[ch] = entity.type == "polymer";
          });
      });
      var bondAtomListStart = 0; //for current model
      //loop over models,
      for (m = 0; m < numModels; m++ ) {
          var modelChainCount = mmtfData.chainsPerModel[m];
          var matoms = atoms[atoms.length-1];
          var serialToIndex: number[] = []; // map to matoms index, needed for noh

          modelData.push({symmetries:symmetries, cryst: unitCell});
          for( i = 0; i < modelChainCount; ++i ){

              var chainGroupCount = mmtfData.groupsPerChain[ chainIndex ];
              var chainId = fromCharCode(
                  mmtfData.chainIdList.subarray( chainIndex * 4, chainIndex * 4 + 4 )
              );
              if(mmtfData.chainNameList) {
                  chainId = fromCharCode(
                          mmtfData.chainNameList.subarray( chainIndex * 4, chainIndex * 4 + 4 )
                  );
              }

              var startGroup = groupIndex;
              var prevSS = '';
              for( j = 0; j < chainGroupCount; ++j ){ //over residues (groups)

                  var groupData = mmtfData.groupList[ mmtfData.groupTypeList[ groupIndex ] ];
                  var groupAtomCount = groupData.atomNameList.length;
                  var secStruct = 0;
                  var secStructBegin = false;
                  var secStructEnd = false;

                  if( secStructList ){
                      secStruct = secStructList[ groupIndex ];
                      var sscode = convertSS(secStruct);
                      if(groupIndex  == 0 || sscode != prevSS) {
                          secStructBegin = true;
                      }
                      prevSS = sscode;
                      var nextgroup = groupIndex+1;
                      if(nextgroup >= secStructList.length || convertSS(secStructList[nextgroup] != sscode)) {
                          secStructEnd = true;
                      }
                  }
                  var insCode = null as string|null;
                  if( mmtfData.insCodeList ){
                      insCode = String.fromCharCode( insCodeList[ groupIndex ] );
                  }
                  var sequenceIndex = null;
                  if( sequenceIndexList ){
                      sequenceIndex = sequenceIndexList[ groupIndex ];
                  }

                  var groupId = mmtfData.groupIdList[ groupIndex ];
                  var groupName = groupData.groupName;
                  let groupType = groupData.chemCompType;
                  var startAtom = atomIndex;
                  //note the following is not identical to respecting HETATM records
                  //this information isn't available in MMTF.
                  let isHETATM =  mmtfHETATMtypes.has(groupType) || !chainIsPolymer[chainIndex];

                  for( k = 0; k < groupAtomCount; ++k ){

                      var element = groupData.elementList[ k ];
                      Iif(noH && element == 'H') {
                          atomIndex += 1;
                          continue;
                      }

                      var bFactor = '';
                      if( bFactorList ){
                          bFactor = bFactorList[ atomIndex ];
                      }
                      var altLoc = '';
                      if( altLocList && altLocList[ atomIndex ]){ //not zero
                          altLoc = String.fromCharCode( altLocList[ atomIndex ] );
                      }
                      var occupancy = '';
                      if( occupancyList ){
                          occupancy = occupancyList[ atomIndex ];
                      }

                      if (altLoc != '' && altLoc != selAltLoc && selAltLoc != '*') {
                          atomIndex += 1;
                          continue;
                      }

                      var atomId = mmtfData.atomIdList[ atomIndex ];
                      var atomName = groupData.atomNameList[ k ];
                      var atomCharge = 0;
                      Iif(groupData.atomChargeList) atomCharge = groupData.atomChargeList[ k ];
                      var xCoord = mmtfData.xCoordList[ atomIndex ];
                      var yCoord = mmtfData.yCoordList[ atomIndex ];
                      var zCoord = mmtfData.zCoordList[ atomIndex ];

                      serialToIndex[atomIndex] = matoms.length;
                      matoms.push({
                          'resn' : groupName,
                          'x' : xCoord,
                          'y' : yCoord,
                          'z' : zCoord,
                          'elem' : element,
                          'hetflag' : isHETATM,
                          'chain' : chainId,
                          'resi' : groupId,
                          'icode' : altLoc,
                          'rescode' : groupId + (altLoc != ' ' ? "^" + altLoc : ""), // combo
                          // resi
                          // and
                          // icode
                          'serial' : atomId,
                          'altLoc' : altLoc,
                          'index' : atomIndex,
                          'atom' : atomName,
                          'bonds' : [],
                          'ss' : convertSS(secStruct),
                          'ssbegin' : secStructBegin,
                          'ssend' : secStructEnd,
                          'bondOrder' : [],
                          'properties' : {charge: atomCharge, occupancy:occupancy},
                          'b' : bFactor,
                      });

                      atomIndex += 1;
                  }

                  // intra group bonds
                  var groupBondAtomList = groupData.bondAtomList;
                  for( k = 0, kl = groupData.bondOrderList.length; k < kl; ++k ){
                      var atomIndex1 = startAtom + groupBondAtomList[ k * 2 ];
                      var atomIndex2 = startAtom + groupBondAtomList[ k * 2 + 1 ];
                      var bondOrder = groupData.bondOrderList[ k ];

                      //I assume bonds are only recorded once
                      var i1 = serialToIndex[atomIndex1];
                      var i2 = serialToIndex[atomIndex2];
                      var a1 = matoms[i1];
                      var a2 = matoms[i2];
                      if(a1 && a2) {
                          a1.bonds.push(i2);
                          a1.bondOrder.push(bondOrder);
                          a2.bonds.push(i1);
                          a2.bondOrder.push(bondOrder);
                      }
                  }

                  groupIndex += 1;
              }

              //reset for bonds
              groupIndex = startGroup;
              for( j = 0; j < chainGroupCount; ++j ){ //over residues (groups)

                  groupIndex += 1;

              }

              chainIndex += 1;
          }

          // inter group bonds
          if( bondAtomList ){
              for(let k = bondAtomListStart, kl = bondAtomList.length; k < kl; k += 2 ){
                   let atomIndex1 = bondAtomList[ k ];
                   let atomIndex2 = bondAtomList[ k + 1 ];
                   let bondOrder = bondOrderList ? bondOrderList[ k / 2 ] : 1;

                   Iif(atomIndex1 >= atomIndex) {
                       bondAtomListStart = k;
                       break; //on next model
                   }
                   //I assume bonds are only recorded once
                   let i1 = serialToIndex[atomIndex1];
                   let i2 = serialToIndex[atomIndex2];
                   let a1 = matoms[i1];
                   let a2 = matoms[i2];
                   if(a1 && a2) {
                       a1.bonds.push(i2);
                       a1.bondOrder.push(bondOrder);
                       a2.bonds.push(i1);
                       a2.bondOrder.push(bondOrder);
                   }
              }
          }

          Iif (options.multimodel) {
              Iif (!options.onemol) atoms.push([]);
          }

          modelIndex += 1;
      }

      if(!noAssembly) {
          for (let n = 0; n < atoms.length; n++) {
              processSymmetries(modelData[n].symmetries, atoms[n], options,modelData[n].cryst);
          }
      }

      if (computeStruct  && !ignoreStruct) {
          computeSecondaryStructure(atoms,options.hbondCutoff);
      }

      return atoms;
    };