Esempio n. 1
0
    def run(self):

        model = self.data_manager.get_model()
        # Temporarly disable
        # self.params.output.prefix=self.data_manager.get_default_model_name().split('.')[0]
        # self.params.output.suffix='_helix'
        # self.data_manager.set_default_output_filename(self.get_default_output_filename())

        self.rama_z = rama_z.rama_z(model=model, log=self.logger)

        # self.data_manager.write_model_file(model)
        # self.params.output.suffix='_sheet'
        # self.data_manager.set_default_output_filename(self.get_default_output_filename())
        # self.data_manager.write_model_file(model)

        result = self.get_results()
        if result is None:
            print("Calculation of z-score failed for some reason",
                  file=self.logger)
        else:
            for k in ["whole", "helix", "sheet", "loop"]:
                rc = k[0].upper()
                v = result.get(rc, None)
                if v is None:
                    print("z-score %-5s: None, residues: %d" %
                          (k, result['residue_counts'][rc]),
                          file=self.logger)
                else:
                    print("z-score %-5s: %6.3f (%5.3f), residues: %d" %
                          (k, v[0], v[1], result['residue_counts'][rc]),
                          file=self.logger)
Esempio n. 2
0
def check_function():
    inp = iotbx.pdb.input(fname)
    model = mmtbx.model.manager(model_input=inp)
    zs = rama_z(model, log=null_out())
    z_scores = zs.get_z_scores()
    ss_cont = zs.get_residue_counts()
    # print (z_scores)
    # print (ss_cont)
    expected_z = {
        'H': -2.7111077448509953,
        'S': None,
        'L': -0.7253113914835396,
        'W': -1.0306993840276084
    }
    expeted_ss = {'H': 277, 'S': 0, 'L': 15041, 'W': 15318}
    for k in expected_z:
        if z_scores[k] is not None:
            assert approx_equal(z_scores[k][0], expected_z[k])
            if k == 'W':
                assert 0.01 < z_scores[k][1] < 0.1, z_scores[k][1]
        if k != 'weighted_mean':
            assert approx_equal(ss_cont[k], expeted_ss[k])
    # check how separate scores translate to whole
    h_score = (z_scores['H'][0] * zs.calibration_values['H'][1] +
               zs.calibration_values['H'][0]) * ss_cont['H']
    l_score = (z_scores['L'][0] * zs.calibration_values['L'][1] +
               zs.calibration_values['L'][0]) * ss_cont['L']
    w_score = ((h_score + l_score) / (ss_cont['H'] + ss_cont['L']) -
               zs.calibration_values['W'][0]) / zs.calibration_values['W'][1]
    # print ("reconstructed:", w_score, z_scores['W'][0])
    assert approx_equal(w_score, z_scores['W'][0])
Esempio n. 3
0
def check_function():
    inp = iotbx.pdb.input(fname)
    model = mmtbx.model.manager(model_input=inp)
    zs = rama_z([model], log=null_out())
    z_scores = zs.get_z_scores()
    ss_cont = zs.get_residue_counts()
    # print (z_scores)
    # print (ss_cont)
    expected_z = {
        'H': None,
        'S': (-0.057428666470734, 0.5840017477579902),
        'L': (-0.3588028726184504, 0.6941226745661744),
        'W': (-0.4019606027769244, 0.6621289642029733)
    }
    expeted_ss = {'H': 0, 'S': 63, 'L': 71, 'W': 134}
    for k in expected_z:
        if z_scores[k] is not None:
            assert approx_equal(z_scores[k], expected_z[k], eps=1e-5)
            assert approx_equal(ss_cont[k], expeted_ss[k])
    # check how separate scores translate to whole
    s_score = (z_scores['S'][0] * zs.calibration_values['S'][1] +
               zs.calibration_values['S'][0]) * ss_cont['S']
    l_score = (z_scores['L'][0] * zs.calibration_values['L'][1] +
               zs.calibration_values['L'][0]) * ss_cont['L']
    w_score = ((s_score + l_score) / (ss_cont['S'] + ss_cont['L']) -
               zs.calibration_values['W'][0]) / zs.calibration_values['W'][1]
    # print ("reconstructed:", w_score, z_scores['W'][0])
    assert approx_equal(w_score, z_scores['W'][0])
Esempio n. 4
0
    def run(self):
        models = []
        for model_name in self.data_manager.get_model_names():
            models.append(self.data_manager.get_model(model_name))

        # model = self.data_manager.get_model()
        self.inp_fn = os.path.basename(
            self.data_manager.get_default_model_name())[:-4]
        self.rama_z = rama_z.rama_z(models=models, log=self.logger)
        if len(models) == 1:
            model = models[0]
            cs = model.crystal_symmetry()
            if cs is None:
                model = shift_and_box_model(model)
            self._write_plots_if_needed(model,
                                        label='whole',
                                        type_of_plot='whole')
            helix_sel, sheet_sel, loop_sel = self.rama_z.get_ss_selections()
            if model.get_hierarchy().models_size() != 1:
                print(
                    "Warning! Outputting partial models and plots are not supported \
  for multi-model files",
                    file=self.logger)
            else:
                for sel, label in [(helix_sel, "helix"), (sheet_sel, "sheet"),
                                   (loop_sel, "loop")]:
                    selected_model = model.select(sel)
                    if self.params.write_HSL_models:
                        pdb_str = selected_model.model_as_pdb()
                        fn = "%s" % self.get_default_output_filename(
                            prefix='%s_' % self.inp_fn,
                            suffix=label,
                            serial=Auto)
                        print("Writing out partial model: %s" % fn,
                              file=self.logger)
                        self.data_manager.write_model_file(selected_model,
                                                           filename=fn)
                    self._write_plots_if_needed(selected_model,
                                                label,
                                                type_of_plot='HSL')
        result = self.get_results()
        res_info = self.rama_z.get_detailed_values()
        print("Individual residues info:", file=self.logger)
        print("Residue name, type, SS, (phi, psi), Z", file=self.logger)
        for i in res_info:
            print('%4s %10s %1s (%7.2f, %7.2f) %7.4f' %
                  (i[2], res_type_labels[i[1]], i[3], i[4], i[5], i[6]),
                  file=self.logger)

        print(result.as_string(prefix=""), file=self.logger)
Esempio n. 5
0
    def run(self):
        model = self.data_manager.get_model()
        self.inp_fn = os.path.basename(
            self.data_manager.get_default_model_name())[:-4]
        self.rama_z = rama_z.rama_z(model=model, log=self.logger)

        self._write_plots_if_needed(model, label='whole', type_of_plot='whole')
        helix_sel, sheet_sel, loop_sel = self.rama_z.get_ss_selections()
        for sel, label in [(helix_sel, "helix"), (sheet_sel, "sheet"),
                           (loop_sel, "loop")]:
            selected_model = model.select(sel)
            if self.params.write_HSL_models:
                pdb_str = selected_model.model_as_pdb()
                fn = "%s" % self.get_default_output_filename(
                    prefix='%s_' % self.inp_fn, suffix=label, serial=Auto)
                print("Writing out partial model: %s" % fn, file=self.logger)
                self.data_manager.write_model_file(selected_model, filename=fn)
            self._write_plots_if_needed(selected_model,
                                        label,
                                        type_of_plot='HSL')
        result = self.get_results()
        if result is None:
            print("Calculation of z-score failed for some reason",
                  file=self.logger)
        else:
            for k in ["whole", "helix", "sheet", "loop"]:
                rc = k[0].upper()
                v = result.get(rc, None)
                if v is None:
                    print("z-score %-5s: None, residues: %d" %
                          (k, result['residue_counts'][rc]),
                          file=self.logger)
                else:
                    print("z-score %-5s: %5.2f (%4.2f), residues: %d" %
                          (k, v[0], v[1], result['residue_counts'][rc]),
                          file=self.logger)
Esempio n. 6
0
  def get_statistics(self):
    if self.json_data['success_composition'] is False:
      return
    make_sub_header('Get statistics', out=self.logger)
    self.save_json()
    success = True
    self.json_data['success_statistics'] = None

    sc, r_sc = None, None
    self.pickle_data.min_reso = None
    self.pickle_data.d_inv = None
    self.pickle_data.r_d_inv = None
    self.pickle_data.d_inv_half = None
    self.pickle_data.fsc_model = None
    self.pickle_data.r_fsc_model = None
    self.pickle_data.fsc_half = None

    if self.json_data['restraints_file']:
      cif_file = self.json_data['restraints_file']
      cif_objects = monomer_library.server.read_cif(file_name=to_str(cif_file))
      cif_objects = [(cif_file, cif_objects)]
    else: cif_objects = None

    for p, fn in zip(('', 'r_'),
      [self.json_data['pdb_file_updated'], self.json_data['pdb_file_refined']]):
    # TODO which file to use for initial!!
      if (not fn or not os.path.isfile(fn)): continue

      if p=='': print('Initial model', file=self.logger)
      if p=='r_': print('\nRefined model', file=self.logger)

      dm = DataManager()

      dm.process_real_map_file(self.json_data['map_file'])
      map_inp = dm.get_real_map(self.json_data['map_file'])
      #map_inp   = iotbx.ccp4_map.map_reader(file_name = self.json_data['map_file'])
      map_inp_1, map_inp_2 = None, None
      if self.json_data['map_file_1'] is not None:
        #map_inp_1 = iotbx.ccp4_map.map_reader(file_name = self.json_data['map_file_1'])
        dm.process_real_map_file(self.json_data['map_file_1'])
        map_inp_1 = dm.get_real_map(self.json_data['map_file_1'])
        #map_data_1 = map_inp_1.map_data()
      if self.json_data['map_file_2'] is not None:
        dm.process_real_map_file(self.json_data['map_file_2'])
        map_inp_2 = dm.get_real_map(self.json_data['map_file_2'])
        #map_inp_2 = iotbx.ccp4_map.map_reader(file_name = self.json_data['map_file_2'])
        #map_data_2 = map_inp_2.map_data()

      print('\tGet model class with restraints...', file=self.logger)
      pdb_inp = iotbx.pdb.input(file_name = fn)
      try:
        model = mmtbx.model.manager(
          model_input       = pdb_inp,
          restraint_objects = cif_objects,
          build_grm         = True,
          stop_for_unknowns = False,
          crystal_symmetry  = map_inp.crystal_symmetry(),
          log               = null_out())
      except Exception as e:
        #success = False
        self.json_data['success_statistics'] = False
        msg = traceback.format_exc()
        print(msg, file=self.logger)
        self.write_log(
          step = 'Statistics: Model class (with restraints) from pdb ',
          msg  = msg)
        self.save_json()
        continue

      if not success: continue

      # Emringer
      if self.resolution < 4.0:
        print('\tCalculate Emringer score', file=self.logger)
        try:
          emringer_score = self.get_emringer_score(
            model   = model.deep_copy(),
            map_inp = map_inp.deep_copy())
          self.json_data[p+'emringer'] = emringer_score
        except Exception as e:
          msg = traceback.format_exc()
          print(msg, file=self.logger)
          self.write_log(step = 'EMRinger failed ', msg = msg)
          # TODO: save as success_statistics False?

      # Rama-z score
      try:
        rama_z_score = rama_z.rama_z(
        models = [model],
        log = self.logger)
        z_scores = rama_z_score.get_z_scores()
        self.json_data[p+'z_score'] = z_scores['W'][0]
      except Exception as e:
        msg = traceback.format_exc()
        print(msg, file=self.logger)
        self.write_log(step = 'Rama z-score failed ', msg = msg)

#      base = map_and_model.input(
#        map_manager   = map_inp,
#        map_manager_1 = map_inp_1,
#        map_manager_2 = map_inp_2,
#        model         = model,
#        box           = True)

      checked = map_model_manager(
        map_manager         = map_inp,
        map_manager_1       = map_inp_1,
        map_manager_2       = map_inp_2,
        model      = model,
        wrapping   = None)
      checked.box_all_maps_around_model_and_shift_origin()

      params = validation_cryoem.master_params().extract()
      params.resolution = self.resolution
      params.slim = False

      if (p == ''):
        print('\tGet map parameters...', file=self.logger)
        self.get_map_parameters(base = checked)

      print('\tRun Mtriage...', file=self.logger)
      try:
        o = validation_cryoem.validation(
          model      = checked.model(),
          map_data   = checked.map_data(),
          map_data_1 = checked.map_data_1(),
          map_data_2 = checked.map_data_2(),
          params     = params)
      except Exception as e:
        # success = False
        self.json_data['success_statistics'] = False
        msg = traceback.format_exc()
        print(msg, file=self.logger)
        self.write_log(step = 'Statistics ', msg = msg)
        self.save_json()
        continue

      print('\tStore info in pkl...', file=self.logger)
      o_dict = self.fill_dictionary(o)
      if (p == ''):
        self.json_data['o'] = o_dict
        sc = o.model.get_sites_cart()
      elif (p == 'r_'):
        self.json_data['o_refined'] = o_dict
        r_sc = o.model.get_sites_cart()
      if o is not None:
        if o.model_vs_data is not None:
          if o.model_vs_data.cc is not None:
            if o.model_vs_data.cc.cc_per_chain is not None:
              if (p == ''):
                self.pickle_data.cc_per_chain = o.model_vs_data.cc.cc_per_chain
              if (p == 'r_'):
                self.pickle_data.r_cc_per_chain = o.model_vs_data.cc.cc_per_chain
            if o.model_vs_data.cc.cc_per_residue is not None:
              if (p == ''):
                self.pickle_data.cc_per_residue = o.model_vs_data.cc.cc_per_residue
              if (p == 'r_'):
                self.pickle_data.r_cc_per_residue = o.model_vs_data.cc.cc_per_residue
        if o.model_stats.geometry.ramachandran.ramalyze is not None:
          rl = o.model_stats.geometry.ramachandran.ramalyze
          if (p == ''): self.pickle_data.ramalyze = rl
          if (p == 'r_'): self.pickle_data.r_ramalyze = rl
      # values for the curve go up to very high reso, get highest reso limit
#      if o is not None:
#        reso_list = [self.json_data['d_pdb'],self.json_data['d_cif'],
#          self.json_data['d_map'], self.json_data['o']['d99'],
#          self.json_data['o']['d_model'], self.json_data['o']['d_fsc']]
#      else:
#        reso_list = [self.json_data['d_pdb'],self.json_data['d_cif'],
#          self.json_data['d_map']]
#      min_reso = min([elem for elem in reso_list if elem is not None])
#      self.pickle_data.min_reso = min_reso
      min_reso = self.resolution
      self.pickle_data.min_reso = self.resolution
      # apply some arbitrary buffer
      min_reso = min_reso - 0.5
      if o is not None:
        masked = o.data_stats.masked
        if (masked.fsc_curve_model is not None):
          if ((masked.fsc_curve_model.d_inv is not None) and
              (masked.fsc_curve_model.fsc is not None)):
            if (p == ''):
              self.pickle_data.d_inv = masked.fsc_curve_model.d_inv
              self.pickle_data.fsc_model   = masked.fsc_curve_model.fsc
            if (p == 'r_'):
              self.pickle_data.r_d_inv = masked.fsc_curve_model.d_inv
              self.pickle_data.r_fsc_model   = masked.fsc_curve_model.fsc
        if (p == ''):
          if (masked.fsc_curve is not None):
            if (masked.fsc_curve.fsc is not None):
              if ((masked.fsc_curve.fsc.d_inv is not None) and
                  (masked.fsc_curve.fsc.fsc is not None)):
                self.pickle_data.d_inv_half = masked.fsc_curve.fsc.d_inv
                self.pickle_data.fsc_half   = masked.fsc_curve.fsc.fsc

      if o is not None:
        print("\nd99                  :", o.data_stats.masked.d99)
        print("d_model              :", o.data_stats.masked.d_model)
        print("d_fsc                :", o.data_stats.masked.d_fsc)
        print("ramachandran.outliers:", o.model_stats.geometry.ramachandran.outliers)
        print("cc_mask              :", o.model_vs_data.cc.cc_mask)
        print("cc_box               :", o.model_vs_data.cc.cc_box)

    if sc is not None and r_sc is not None:
      self.json_data['rmsd'] = sc.rms_difference(r_sc)

    if self.json_data['success_statistics'] is not False:
      self.json_data['success_statistics'] = success
Esempio n. 7
0
 def rama_z_score(self):
   return rama_z.rama_z(models = [self.model], log = null_out()).get_result()