def start_coot_and_wait(pdb_file,
                        map_file,
                        data_file,
                        work_dir=None,
                        coot_cmd="coot",
                        needs_rebuild=False,
                        log=None):
    if (log is None): log = sys.stdout
    if (work_dir is None): work_dir = os.getcwd()
    if (not os.path.isdir(work_dir)):
        os.makedirs(work_dir)
    import mmtbx.maps.utils
    from libtbx.str_utils import make_header
    from libtbx import easy_run
    from libtbx import group_args
    import cootbx
    base_script = __file__.replace(".pyc", ".py")
    os.chdir(work_dir)
    if (os.path.exists("coot_out_tmp.pdb")):
        os.remove("coot_out_tmp.pdb")
    if (os.path.exists("coot_out.pdb")):
        os.remove("coot_out.pdb")
    f = open("edit_in_coot.py", "w")
    f.write(open(base_script).read())
    f.write("\n")
    f.write("import coot\n")
    cootbx.write_disable_nomenclature_errors(f)
    f.write("m = manager(\"%s\", \"%s\", needs_rebuild=%s)\n" %
            (pdb_file, map_file, needs_rebuild))
    f.close()
    make_header("Interactive editing in Coot", log)
    easy_run.call("\"%s\" --no-state-script --script edit_in_coot.py &" %
                  coot_cmd)
    print >> log, "  Waiting for coot_out_tmp.pdb to appear at %s" % \
      str(time.asctime())
    base_dir = os.path.dirname(pdb_file)
    tmp_file = os.path.join(base_dir, "coot_out_tmp.pdb")
    edit_file = os.path.join(base_dir, "coot_tmp_edits.pdb")
    maps_file = os.path.join(base_dir, ".NEW_MAPS")
    while (True):
        if (os.path.isfile(tmp_file)):
            print >> log, "  Coot editing complete at %s" % str(time.asctime())
            break
        elif (os.path.isfile(maps_file)):
            t1 = time.time()
            assert os.path.isfile(edit_file)
            mmtbx.maps.utils.create_map_from_pdb_and_mtz(
                pdb_file=edit_file,
                mtz_file=data_file,
                output_file=os.path.join(base_dir, "maps_for_coot.mtz"),
                fill=True,
                out=log)
            t2 = time.time()
            print >> log, "Calculated new map coefficients in %.1fs" % (t2 -
                                                                        t1)
            os.remove(maps_file)
        else:
            time.sleep(t_wait / 1000.)
    shutil.move(tmp_file, "coot_out.pdb")
    mmtbx.maps.utils.create_map_from_pdb_and_mtz(
        pdb_file="coot_out.pdb",
        mtz_file=data_file,
        output_file="coot_out_maps.mtz",
        fill=True,
        out=log)
    new_model = os.path.join(work_dir, "coot_out.pdb")
    new_map = os.path.join(work_dir, "coot_out_maps.mtz")
    skip_rebuild = None
    if (needs_rebuild):
        if (os.path.isfile(os.path.join(base_dir, "NO_BUILD"))):
            skip_rebuild = True
        else:
            skip_rebuild = False
    return group_args(pdb_file=new_model,
                      map_file=new_map,
                      skip_rebuild=skip_rebuild)
def start_coot_and_wait(pdb_file,
                        map_file,
                        ligand_files,
                        ligand_ccs,
                        cif_files=(),
                        work_dir=None,
                        coot_cmd="coot",
                        log=None):
    from iotbx import file_reader
    from libtbx.str_utils import make_header
    from libtbx import easy_run
    import cootbx
    assert (len(ligand_files) > 0) and (len(ligand_files) == len(ligand_ccs))
    if (log is None): log = sys.stdout
    cwd = os.getcwd()
    if (work_dir is None): work_dir = cwd
    if (not os.path.isdir(work_dir)):
        os.makedirs(work_dir)
    os.chdir(work_dir)
    base_script = __file__.replace(".pyc", ".py")
    ligand_xyzs = []
    for pdb_file in ligand_files:
        pdb_file = to_str(pdb_file)
        pdb_in = file_reader.any_file(pdb_file, force_type="pdb")
        pdb_in.assert_file_type("pdb")
        coords = pdb_in.file_object.atoms().extract_xyz()
        ligand_xyzs.append(coords.mean())
    ligand_info = zip(ligand_files, ligand_ccs, ligand_xyzs)
    f = open("edit_in_coot.py", "w")
    f.write(open(base_script).read())
    f.write("\n")
    f.write("import coot\n")
    cootbx.write_disable_nomenclature_errors(f)
    f.write("read_pdb(\"%s\")\n" % to_str(pdb_file))
    f.write("auto_read_make_and_draw_maps(\"%s\")\n" % to_str(map_file))
    for cif_file in cif_files:
        f.write("read_cif_dictionary(\"%s\")\n" % to_str(cif_file))
    f.write("m = manager(%s)\n" % str(ligand_info))
    f.close()
    make_header("Ligand selection in Coot", log)
    rc = easy_run.call("\"%s\" --no-state-script --script edit_in_coot.py &" %
                       coot_cmd)
    if (rc != 0):
        raise RuntimeError("Launching Coot failed with status %d" % rc)
    print >> log, "  Waiting for user input at %s" % str(time.asctime())
    out_file = ".COOT_LIGANDS"
    output_files = output_ccs = None
    while (True):
        if (os.path.isfile(out_file)):
            print >> log, "  Coot editing complete at %s" % str(time.asctime())
            ligand_indices = [int(i) for i in open(out_file).read().split()]
            output_files = []
            for i in ligand_indices:
                ligand_file = os.path.join(work_dir,
                                           "coot_ligand_out_%d.pdb" % (i + 1))
                output_files.append(ligand_file)
            output_ccs = [ligand_ccs[i] for i in ligand_indices]
            break
        else:
            time.sleep(t_wait / 1000.)
    assert (output_files is not None)
    os.chdir(cwd)
    return output_files, output_ccs
def start_coot_and_wait (
    pdb_file,
    map_file,
    ligand_files,
    ligand_ccs,
    cif_files=(),
    work_dir=None,
    coot_cmd="coot",
    log=None) :
  from iotbx import file_reader
  from libtbx.str_utils import make_header
  from libtbx import easy_run
  import cootbx
  assert (len(ligand_files) > 0) and (len(ligand_files) == len(ligand_ccs))
  if (log is None) : log = sys.stdout
  cwd = os.getcwd()
  if (work_dir is None) : work_dir = cwd
  if (not os.path.isdir(work_dir)) :
    os.makedirs(work_dir)
  os.chdir(work_dir)
  base_script = __file__.replace(".pyc", ".py")
  ligand_xyzs = []
  for pdb_file in ligand_files :
    pdb_file = to_str(pdb_file)
    pdb_in = file_reader.any_file(pdb_file, force_type="pdb")
    pdb_in.assert_file_type("pdb")
    coords = pdb_in.file_object.atoms().extract_xyz()
    ligand_xyzs.append(coords.mean())
  ligand_info = zip(ligand_files, ligand_ccs, ligand_xyzs)
  f = open("edit_in_coot.py", "w")
  f.write(open(base_script).read())
  f.write("\n")
  f.write("import coot\n")
  cootbx.write_disable_nomenclature_errors(f)
  f.write("read_pdb(\"%s\")\n" % to_str(pdb_file))
  f.write("auto_read_make_and_draw_maps(\"%s\")\n" % to_str(map_file))
  for cif_file in cif_files :
    f.write("read_cif_dictionary(\"%s\")\n" % to_str(cif_file))
  f.write("m = manager(%s)\n" % str(ligand_info))
  f.close()
  make_header("Ligand selection in Coot", log)
  rc = easy_run.call("\"%s\" --no-state-script --script edit_in_coot.py &" %
    coot_cmd)
  if (rc != 0) :
    raise RuntimeError("Launching Coot failed with status %d" % rc)
  print >> log, "  Waiting for user input at %s" % str(time.asctime())
  out_file = ".COOT_LIGANDS"
  output_files = output_ccs = None
  while (True) :
    if (os.path.isfile(out_file)) :
      print >> log, "  Coot editing complete at %s" % str(time.asctime())
      ligand_indices = [ int(i) for i in open(out_file).read().split() ]
      output_files = []
      for i in ligand_indices :
        ligand_file = os.path.join(work_dir, "coot_ligand_out_%d.pdb" % (i+1))
        output_files.append(ligand_file)
      output_ccs = [ ligand_ccs[i] for i in ligand_indices ]
      break
    else :
      time.sleep(t_wait / 1000.)
  assert (output_files is not None)
  os.chdir(cwd)
  return output_files, output_ccs
def start_coot_and_wait (
    pdb_file,
    map_file,
    data_file,
    work_dir=None,
    coot_cmd="coot",
    needs_rebuild=False,
    log=None) :
  if (log is None) : log = sys.stdout
  if (work_dir is None) : work_dir = os.getcwd()
  if (not os.path.isdir(work_dir)) :
    os.makedirs(work_dir)
  import mmtbx.maps.utils
  from libtbx.str_utils import make_header
  from libtbx import easy_run
  from libtbx import group_args
  import cootbx
  base_script = __file__.replace(".pyc", ".py")
  os.chdir(work_dir)
  if (os.path.exists("coot_out_tmp.pdb")) :
    os.remove("coot_out_tmp.pdb")
  if (os.path.exists("coot_out.pdb")) :
    os.remove("coot_out.pdb")
  f = open("edit_in_coot.py", "w")
  f.write(open(base_script).read())
  f.write("\n")
  f.write("import coot\n")
  cootbx.write_disable_nomenclature_errors(f)
  f.write("m = manager(\"%s\", \"%s\", needs_rebuild=%s)\n" %
    (pdb_file, map_file, needs_rebuild))
  f.close()
  make_header("Interactive editing in Coot", log)
  easy_run.call("\"%s\" --no-state-script --script edit_in_coot.py &" %
    coot_cmd)
  print >> log, "  Waiting for coot_out_tmp.pdb to appear at %s" % \
    str(time.asctime())
  base_dir = os.path.dirname(pdb_file)
  tmp_file = os.path.join(base_dir, "coot_out_tmp.pdb")
  edit_file = os.path.join(base_dir, "coot_tmp_edits.pdb")
  maps_file = os.path.join(base_dir, ".NEW_MAPS")
  while (True) :
    if (os.path.isfile(tmp_file)) :
      print >> log, "  Coot editing complete at %s" % str(time.asctime())
      break
    elif (os.path.isfile(maps_file)) :
      t1 = time.time()
      assert os.path.isfile(edit_file)
      mmtbx.maps.utils.create_map_from_pdb_and_mtz(
        pdb_file=edit_file,
        mtz_file=data_file,
        output_file=os.path.join(base_dir, "maps_for_coot.mtz"),
        fill=True,
        out=log)
      t2 = time.time()
      print >> log, "Calculated new map coefficients in %.1fs" % (t2-t1)
      os.remove(maps_file)
    else :
      time.sleep(t_wait/1000.)
  shutil.move(tmp_file, "coot_out.pdb")
  mmtbx.maps.utils.create_map_from_pdb_and_mtz(
    pdb_file="coot_out.pdb",
    mtz_file=data_file,
    output_file="coot_out_maps.mtz",
    fill=True,
    out=log)
  new_model = os.path.join(work_dir, "coot_out.pdb")
  new_map = os.path.join(work_dir, "coot_out_maps.mtz")
  skip_rebuild = None
  if (needs_rebuild) :
    if (os.path.isfile(os.path.join(base_dir, "NO_BUILD"))) :
      skip_rebuild = True
    else :
      skip_rebuild = False
  return group_args(
    pdb_file=new_model,
    map_file=new_map,
    skip_rebuild=skip_rebuild)