# Path: ssbio/utils.py
# def percentage_to_float(x):
#     """Convert a string representation of a percentage to float.
# 
#     >>> percentage_to_float('55%')
#     0.55
# 
#     Args:
#         x: String representation of a percentage
# 
#     Returns:
#         float: Percentage in decimal form
# 
#     """
#     return float(x.strip('%')) / 100


import glob
import logging
import os
import numpy as np
import pandas as pd
import ssbio.protein.sequence.utils.alignment
from ssbio.utils import percentage_to_float

    Args:
        quality_directory: path to directory with PROCHECK output (.sum files)

    Returns:
        Pandas DataFrame: Summary of PROCHECK results

    """

    # TODO: save as dict instead, offer df as option
    # TODO: parse for one file instead

    procheck_summaries = glob.glob(os.path.join(quality_directory, '*.sum'))

    if len(procheck_summaries) == 0:
        return pd.DataFrame()

    all_procheck = {}
    for summ in procheck_summaries:
        structure_id = os.path.basename(summ).split('.sum')[0]
        procheck_dict = {}
        with open(summ) as f_in:
            lines = (line.rstrip() for line in f_in) # All lines including the blank ones
            lines = (line for line in lines if line) # Non-blank lines
            for line in lines:
                if len(line.split()) > 1:
                    if line.split()[1] == 'Ramachandran':
                        procheck_dict['procheck_rama_favored'] = percentage_to_float(line.split()[3])
                        procheck_dict['procheck_rama_allowed'] = percentage_to_float(line.split()[5])
                        procheck_dict['procheck_rama_allowed_plus'] = percentage_to_float(line.split()[7])

# Path: ssbio/utils.py
# def percentage_to_float(x):
#     """Convert a string representation of a percentage to float.
# 
#     >>> percentage_to_float('55%')
#     0.55
# 
#     Args:
#         x: String representation of a percentage
# 
#     Returns:
#         float: Percentage in decimal form
# 
#     """
#     return float(x.strip('%')) / 100


# Path: ssbio/protein/structure/properties/quality.py
import glob
import logging
import os
import numpy as np
import pandas as pd
import ssbio.protein.sequence.utils.alignment
from ssbio.utils import percentage_to_float


    Args:
        quality_directory: path to directory with PROCHECK output (.sum files)

    Returns:
        Pandas DataFrame: Summary of PROCHECK results

    """

    # TODO: save as dict instead, offer df as option
    # TODO: parse for one file instead

    procheck_summaries = glob.glob(os.path.join(quality_directory, '*.sum'))

    if len(procheck_summaries) == 0:
        return pd.DataFrame()

    all_procheck = {}
    for summ in procheck_summaries:
        structure_id = os.path.basename(summ).split('.sum')[0]
        procheck_dict = {}
        with open(summ) as f_in:
            lines = (line.rstrip() for line in f_in) # All lines including the blank ones
            lines = (line for line in lines if line) # Non-blank lines
            for line in lines:
                if len(line.split()) > 1:
                    if line.split()[1] == 'Ramachandran':
                        procheck_dict['procheck_rama_favored'] = percentage_to_float(line.split()[3])
                        procheck_dict['procheck_rama_allowed'] = percentage_to_float(line.split()[5])
                        procheck_dict['procheck_rama_allowed_plus'] = percentage_to_float(line.split()[7])

                        procheck_dict['procheck_rama_disallowed'] = percentage_to_float(line.split()[9])