# Path: ensembler/utils.py
# def file_exists_and_not_empty(filepath):
#     if os.path.exists(filepath):
#         if os.path.getsize(filepath) > 0:
#             return True
#     return False
# 
# def get_installed_resource_filename(relative_path):
#     """
#     Returns the installation path of a resource file shipped with the code.
# 
#     Parameters
#     ----------
#     name: str
#         Name of the file to load (relative to the `ensembler` main code directory).
# 
#     Returns
#     -------
#     installed_filepath: str
#         absolute path of the installed file
# 
#     Examples
#     --------
#     >>> pathname = get_installed_resource_filename('tests/example_project/meta0.yaml')
#     """
#     installed_filepath = resource_filename(ensembler.__name__, relative_path)
# 
#     if not os.path.exists(installed_filepath):
#         raise ValueError(
#             "Sorry! {0} does not exist."
#             "If you just added it, you'll have to re-install".format(relative_path)
#         )
# 
#     return installed_filepath
# 
# 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 gzip
import json
import sys
import os
import re
import shutil
import Bio.SeqUtils
import Bio.SeqIO
import ensembler
import ensembler.version
import ensembler.targetexplorer
import ensembler.uniprot
import ensembler.pdb
from lxml import etree
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from ensembler.utils import file_exists_and_not_empty, get_installed_resource_filename
from ensembler.core import mpistate, logger, manual_overrides_filename
    with open(project_pdb_filepath, 'w') as pdbgz_file:
        pdbgz_file.write(pdbgz_page)


def download_sifts_file(pdbid, project_sifts_filepath):
    logger.info('Downloading sifts file for: %s', pdbid)
    sifts_page = ensembler.pdb.retrieve_sifts(pdbid)
    with gzip.open(project_sifts_filepath, 'wb') as project_sifts_file:
        project_sifts_file.write(sifts_page)


def get_structure_files(selected_pdbchains, structure_dirs=None):
    if structure_dirs:
        for structure_dir in structure_dirs:
            if not os.path.exists(structure_dir):
                logger.warn('Structure directory {0} not found'.format(structure_dir))
    for pdbchain in selected_pdbchains:
        get_structure_files_for_single_pdbchain(pdbchain['pdbid'], structure_dirs)


def get_structure_files_for_single_pdbchain(pdbid, structure_dirs=None):
    if type(structure_dirs) != list:
        structure_dirs = []
    project_structures_dir = 'structures'
    for structure_type in ['pdb', 'sifts']:
        project_structure_filepath = os.path.join(
            project_structures_dir,
            structure_type,
            pdbid + structure_type_file_extension_mapper[structure_type]
        )

# Path: ensembler/utils.py
# def file_exists_and_not_empty(filepath):
#     if os.path.exists(filepath):
#         if os.path.getsize(filepath) > 0:
#             return True
#     return False
# 
# def get_installed_resource_filename(relative_path):
#     """
#     Returns the installation path of a resource file shipped with the code.
# 
#     Parameters
#     ----------
#     name: str
#         Name of the file to load (relative to the `ensembler` main code directory).
# 
#     Returns
#     -------
#     installed_filepath: str
#         absolute path of the installed file
# 
#     Examples
#     --------
#     >>> pathname = get_installed_resource_filename('tests/example_project/meta0.yaml')
#     """
#     installed_filepath = resource_filename(ensembler.__name__, relative_path)
# 
#     if not os.path.exists(installed_filepath):
#         raise ValueError(
#             "Sorry! {0} does not exist."
#             "If you just added it, you'll have to re-install".format(relative_path)
#         )
# 
#     return installed_filepath
# 
# 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/initproject.py
import gzip
import json
import sys
import os
import re
import shutil
import Bio.SeqUtils
import Bio.SeqIO
import ensembler
import ensembler.version
import ensembler.targetexplorer
import ensembler.uniprot
import ensembler.pdb
from lxml import etree
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from ensembler.utils import file_exists_and_not_empty, get_installed_resource_filename
from ensembler.core import mpistate, logger, manual_overrides_filename

    with open(project_pdb_filepath, 'w') as pdbgz_file:
        pdbgz_file.write(pdbgz_page)


def download_sifts_file(pdbid, project_sifts_filepath):
    logger.info('Downloading sifts file for: %s', pdbid)
    sifts_page = ensembler.pdb.retrieve_sifts(pdbid)
    with gzip.open(project_sifts_filepath, 'wb') as project_sifts_file:
        project_sifts_file.write(sifts_page)


def get_structure_files(selected_pdbchains, structure_dirs=None):
    if structure_dirs:
        for structure_dir in structure_dirs:
            if not os.path.exists(structure_dir):
                logger.warn('Structure directory {0} not found'.format(structure_dir))
    for pdbchain in selected_pdbchains:
        get_structure_files_for_single_pdbchain(pdbchain['pdbid'], structure_dirs)


def get_structure_files_for_single_pdbchain(pdbid, structure_dirs=None):
    if type(structure_dirs) != list:
        structure_dirs = []
    project_structures_dir = 'structures'
    for structure_type in ['pdb', 'sifts']:
        project_structure_filepath = os.path.join(
            project_structures_dir,
            structure_type,
            pdbid + structure_type_file_extension_mapper[structure_type]
        )

        if not file_exists_and_not_empty(project_structure_filepath):