def add_model(s1, s2):
    ''' add chains from s2 to existing s1'''
    s1 = s1.deep_copy()
    s1_ph = s1.get_hierarchy()  # working hierarchy
    from mmtbx.secondary_structure.find_ss_from_ca import get_chain_ids
    existing_chain_ids = get_chain_ids(s1_ph)
    for model_mm_2 in s2.get_hierarchy().models()[:1]:
        for chain in model_mm_2.chains():
            assert chain.id not in existing_chain_ids  # duplicate chains in add_model
            new_chain = chain.detached_copy()
            for model_mm in s1_ph.models()[:1]:
                model_mm.append_chain(new_chain)

    s1.reset_after_changing_hierarchy()
    return s1
def get_chain_id(model, chain_id, log=sys.stdout):
    from mmtbx.secondary_structure.find_ss_from_ca import get_chain_ids
    chain_ids = get_chain_ids(model.get_hierarchy())
    if len(chain_ids) == 1:
        chain_id = chain_ids[0]
    elif chain_id is not None:
        if chain_id in chain_ids:
            print("Working on chain '%s' only" % (chain_id), file=log)
        else:
            raise Sorry("Chain %s is not in the chains present (%s)" %
                        (chain_id, " ".join(chain_ids)))
    else:
        chain_id = chain_ids[0]
        print("Working on chain '%s' only (skipping other chains: %s)" %
              (chain_id, " ".join(chain_ids[1:])),
              file=log)
def tst_05():
    from mmtbx.validation.chain_comparison import \
         extract_unique_part_of_hierarchy as euph
    from mmtbx.secondary_structure.find_ss_from_ca import get_chain_ids

    print("Testing extraction of unique part and unique matching")
    for m in [modela, modelb, modelaa, modelaab, modelaabaab]:
        import iotbx.pdb
        from cctbx.array_family import flex
        model_pdb_inp = iotbx.pdb.input(source_info='model',
                                        lines=flex.split_lines(m))
        crystal_symmetry = model_pdb_inp.crystal_symmetry()
        model_hierarchy = model_pdb_inp.construct_hierarchy()

        print("\nExtraction of unique MODEL with %s residues" %
              (model_hierarchy.overall_counts().n_residues))
        query_hierarchy = iotbx.pdb.input(
            source_info='query',
            lines=flex.split_lines(target)).construct_hierarchy()
        unique_hierarchy = euph(model_hierarchy, target_ph=query_hierarchy)
        print("FINAL chain ids: %s \n" %
              (" ".join(get_chain_ids(unique_hierarchy))))
    print("OK")