# Path: ensembler/core.py
# def get_targets_and_templates():
#     targets = get_targets()
#     templates_resolved_seq = get_templates_resolved_seq()
#     return targets, templates_resolved_seq
# 
# def get_templates_full_seq():
#     templates_full_seq_fasta_filename = os.path.abspath(os.path.join(
#         ensembler.core.default_project_dirnames.templates, 'templates-full-seq.fa'
#     ))
#     templates_full_seq = list(Bio.SeqIO.parse(templates_full_seq_fasta_filename, 'fasta'))
#     return templates_full_seq
# 
# Path: ensembler/core.py
# class MPIState:
# class DummyMPIState:
# class DummyMPIComm:
# class literal_str(str): pass
# class LogFile:
# class ManualOverrides:
# class TargetManualOverrides:
# class TemplateManualOverrides:
# class RefinementManualOverrides:
# class ProjectMetadata:
#     def __init__(self):
#     def __init__(self):
#     def Barrier(self):
#     def bcast(self, obj, root=0):
#     def gather(self, obj, root=0):
# def change_style(style, representer):
#     def new_representer(dumper, data):
# def get_utcnow_formatted():
# def strf_timedelta(delta_t):
# def check_project_toplevel_dir(raise_exception=True):
#     def __init__(self, log_filepath):
#     def log(self, new_log_data={}):
#     def __init__(self, manual_overrides_filepath=None):
#     def __init__(self, manual_overrides_yaml):
#     def __init__(self, manual_overrides_yaml):
#     def __init__(self, manual_overrides_yaml):
# def gen_metadata_filename(ensembler_stage, metadata_file_index):
#     def __init__(self, project_stage='init', target_id=None, project_toplevel_dir='.'):
#     def add_all_prev_metadata(self, project_stage):
#     def add_prev_metadata(self, project_stage):
#     def determine_latest_metadata_filepath(self, project_stage):
#     def metadata_dir_mapper(self, project_stage, target_id=None):
#     def metadata_file_basename_mapper(self, project_stage):
#     def determine_latest_metadata_file_index(self, project_stage):
#     def gen_metadata_filepath_from_dir_index_and_file_basename(self, dirpath, file_basename, index):
#     def add_data(self, data, project_stage=None):
#     def add_iteration_number_to_metadata(self, iter_number):
#     def write(self):
# def encode_url_query(uniprot_query):
#     def replace_all(text, replace_dict):
# def xpath_match_regex_case_sensitive(context, attrib_values, regex_str):
# def xpath_match_regex_case_insensitive(context, attrib_values, regex_str):
# def sequnwrap(sequence):
# def seqwrap(sequence, add_star=False):
# def construct_fasta_str(id, seq):
# def get_targets():
# def get_templates_resolved_seq():
# def get_templates_full_seq():
# def get_targets_and_templates():
# def find_loopmodel_executable():
# def check_ensembler_modeling_stage_first_model_file_exists(ensembler_stage, targetid):
# def check_ensembler_modeling_stage_metadata_exists(ensembler_stage, targetid):
# def check_ensembler_modeling_stage_complete(ensembler_stage, targetid):
# def get_most_advanced_ensembler_modeling_stage(targetid):
# def get_valid_model_ids(ensembler_stage, targetid):
# def select_templates_by_seqid_cutoff(targetid, seqid_cutoff=None):
# def select_templates_by_validation_score(targetid,
#                                          validation_score_cutoff=None,
#                                          validation_score_percentile=None
#                                          ):


import sys
import os
import re
import datetime
import shutil
import gzip
import glob
import tempfile
import traceback
import Bio.SeqUtils
import simtk.openmm
import yaml
import msmbuilder
import warnings
import ensembler
import ensembler.version
import Bio
import Bio.SeqIO
import Bio.pairwise2
import Bio.SubsMat.MatrixInfo
import mdtraj
import msmbuilder
    import modeller
    import modeller.automodel
        import subprocess32 as subprocess
        import subprocess
    import subprocess
        import pdbfixer
        import simtk.openmm.app
    import msmbuilder.cluster
from collections import namedtuple
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from ensembler.core import get_targets_and_templates, get_templates_full_seq
from ensembler.core import mpistate, logger
    loopmodel_templates(templates_resolved_seq, missing_residues_list, process_only_these_templates=process_only_these_templates, overwrite_structures=overwrite_structures)


def pdbfix_templates(templates_full_seq, process_only_these_templates=None, overwrite_structures=False):
    """
    Parameters
    ----------
    templates_full_seq: list of BioPython SeqRecord
        full UniProt sequence for span of the template (including unresolved residues)
    process_only_these_templates: list of str
    overwrite_structures: bool
    Returns
    -------
    missing_residues_list: list of list of OpenMM Residue
    """
    missing_residues_sublist = []
    ntemplates = len(templates_full_seq)
    for template_index in range(mpistate.rank, ntemplates, mpistate.size):
        template_full_seq = templates_full_seq[template_index]
        if process_only_these_templates and template_full_seq.id not in process_only_these_templates:
            missing_residues_sublist.append(None)
            continue
        missing_residues_sublist.append(pdbfix_template(template_full_seq, overwrite_structures=overwrite_structures))

    missing_residues_gathered = mpistate.comm.gather(missing_residues_sublist, root=0)

    missing_residues_list = []
    if mpistate.rank == 0:
        missing_residues_list = [None] * ntemplates
        for template_index in range(ntemplates):

# Path: ensembler/core.py
# def get_targets_and_templates():
#     targets = get_targets()
#     templates_resolved_seq = get_templates_resolved_seq()
#     return targets, templates_resolved_seq
# 
# def get_templates_full_seq():
#     templates_full_seq_fasta_filename = os.path.abspath(os.path.join(
#         ensembler.core.default_project_dirnames.templates, 'templates-full-seq.fa'
#     ))
#     templates_full_seq = list(Bio.SeqIO.parse(templates_full_seq_fasta_filename, 'fasta'))
#     return templates_full_seq
# 
# Path: ensembler/core.py
# class MPIState:
# class DummyMPIState:
# class DummyMPIComm:
# class literal_str(str): pass
# class LogFile:
# class ManualOverrides:
# class TargetManualOverrides:
# class TemplateManualOverrides:
# class RefinementManualOverrides:
# class ProjectMetadata:
#     def __init__(self):
#     def __init__(self):
#     def Barrier(self):
#     def bcast(self, obj, root=0):
#     def gather(self, obj, root=0):
# def change_style(style, representer):
#     def new_representer(dumper, data):
# def get_utcnow_formatted():
# def strf_timedelta(delta_t):
# def check_project_toplevel_dir(raise_exception=True):
#     def __init__(self, log_filepath):
#     def log(self, new_log_data={}):
#     def __init__(self, manual_overrides_filepath=None):
#     def __init__(self, manual_overrides_yaml):
#     def __init__(self, manual_overrides_yaml):
#     def __init__(self, manual_overrides_yaml):
# def gen_metadata_filename(ensembler_stage, metadata_file_index):
#     def __init__(self, project_stage='init', target_id=None, project_toplevel_dir='.'):
#     def add_all_prev_metadata(self, project_stage):
#     def add_prev_metadata(self, project_stage):
#     def determine_latest_metadata_filepath(self, project_stage):
#     def metadata_dir_mapper(self, project_stage, target_id=None):
#     def metadata_file_basename_mapper(self, project_stage):
#     def determine_latest_metadata_file_index(self, project_stage):
#     def gen_metadata_filepath_from_dir_index_and_file_basename(self, dirpath, file_basename, index):
#     def add_data(self, data, project_stage=None):
#     def add_iteration_number_to_metadata(self, iter_number):
#     def write(self):
# def encode_url_query(uniprot_query):
#     def replace_all(text, replace_dict):
# def xpath_match_regex_case_sensitive(context, attrib_values, regex_str):
# def xpath_match_regex_case_insensitive(context, attrib_values, regex_str):
# def sequnwrap(sequence):
# def seqwrap(sequence, add_star=False):
# def construct_fasta_str(id, seq):
# def get_targets():
# def get_templates_resolved_seq():
# def get_templates_full_seq():
# def get_targets_and_templates():
# def find_loopmodel_executable():
# def check_ensembler_modeling_stage_first_model_file_exists(ensembler_stage, targetid):
# def check_ensembler_modeling_stage_metadata_exists(ensembler_stage, targetid):
# def check_ensembler_modeling_stage_complete(ensembler_stage, targetid):
# def get_most_advanced_ensembler_modeling_stage(targetid):
# def get_valid_model_ids(ensembler_stage, targetid):
# def select_templates_by_seqid_cutoff(targetid, seqid_cutoff=None):
# def select_templates_by_validation_score(targetid,
#                                          validation_score_cutoff=None,
#                                          validation_score_percentile=None
#                                          ):


# Path: ensembler/modeling.py
import sys
import os
import re
import datetime
import shutil
import gzip
import glob
import tempfile
import traceback
import Bio.SeqUtils
import simtk.openmm
import yaml
import msmbuilder
import warnings
import ensembler
import ensembler.version
import Bio
import Bio.SeqIO
import Bio.pairwise2
import Bio.SubsMat.MatrixInfo
import mdtraj
import msmbuilder
    import modeller
    import modeller.automodel
        import subprocess32 as subprocess
        import subprocess
    import subprocess
        import pdbfixer
        import simtk.openmm.app
    import msmbuilder.cluster
from collections import namedtuple
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from ensembler.core import get_targets_and_templates, get_templates_full_seq
from ensembler.core import mpistate, logger

    loopmodel_templates(templates_resolved_seq, missing_residues_list, process_only_these_templates=process_only_these_templates, overwrite_structures=overwrite_structures)


def pdbfix_templates(templates_full_seq, process_only_these_templates=None, overwrite_structures=False):
    """
    Parameters
    ----------
    templates_full_seq: list of BioPython SeqRecord
        full UniProt sequence for span of the template (including unresolved residues)
    process_only_these_templates: list of str
    overwrite_structures: bool
    Returns
    -------
    missing_residues_list: list of list of OpenMM Residue
    """
    missing_residues_sublist = []
    ntemplates = len(templates_full_seq)
    for template_index in range(mpistate.rank, ntemplates, mpistate.size):
        template_full_seq = templates_full_seq[template_index]
        if process_only_these_templates and template_full_seq.id not in process_only_these_templates:
            missing_residues_sublist.append(None)
            continue
        missing_residues_sublist.append(pdbfix_template(template_full_seq, overwrite_structures=overwrite_structures))

    missing_residues_gathered = mpistate.comm.gather(missing_residues_sublist, root=0)

    missing_residues_list = []
    if mpistate.rank == 0:
        missing_residues_list = [None] * ntemplates
        for template_index in range(ntemplates):

            missing_residues_list[template_index] = missing_residues_gathered[template_index % mpistate.size][template_index // mpistate.size]