from pubchem import experimental_properties
from collect import collect, APIFunction
import re


class MeltingPoint(APIFunction):

    # Takes JSON data and finds melting points and averages them
    def run(self, data):
        melting_data = str(experimental_properties(data, 'Melting Point'))

        mp = []

        for point in melting_data['Information']:
            line = str(point['Value']['StringWithMarkup'][0]['String']).lower()
            if 'decomp' not in line and \
                    'greater' not in line and \
                    'less' not in line and \
                    'approx' not in line and \
                    '<' not in line and \
                    '>' not in line:

                # Get either a range of temperature or the measured temperature
                if '°c' in line:
                    if re.match(r"\d*-\d+", line):
                        ranges = re.findall(r"\d*-\d+", line)[0].split['-']
                        mp.append((float(ranges[0]) + float(ranges[1])) / 2)
                    else:
                        mp.append(float(re.findall(r"\d*", line)[0]))
                # Same as getting celsius just convert
                elif '°f' in line:
                    if re.match(r"\d*-\d+", line):
                        ranges = re.findall(r"\d*-\d+", line)[0].split['-']
                        mp.append((((float(ranges[0]) + float(ranges[1])) / 2) - 32) * (5 / 9))
                    else:
                        mp.append((float(re.findall(r"\d*", line)[0]) - 32) * (5 / 9))

        if len(mp) == 0:
            return None
        else:
            return sum(mp) / len(mp)

# Takes list of molecules and outputs the smiles and corresponding melting point from melting_point function
collect('data/melting_point/list.txt', 'mp.txt', MeltingPoint)
ce634ee0eec18a6414d31bf36e2eef803625b95a
<mask token>


class MeltingPoint(APIFunction):
    <mask token>


<mask token>

<mask token>


class MeltingPoint(APIFunction):

    def run(self, data):
        melting_data = str(experimental_properties(data, 'Melting Point'))
        mp = []
        for point in melting_data['Information']:
            line = str(point['Value']['StringWithMarkup'][0]['String']).lower()
            if ('decomp' not in line and 'greater' not in line and 'less'
                 not in line and 'approx' not in line and '<' not in line and
                '>' not in line):
                if '°c' in line:
                    if re.match('\\d*-\\d+', line):
                        ranges = re.findall('\\d*-\\d+', line)[0].split['-']
                        mp.append((float(ranges[0]) + float(ranges[1])) / 2)
                    else:
                        mp.append(float(re.findall('\\d*', line)[0]))
                elif '°f' in line:
                    if re.match('\\d*-\\d+', line):
                        ranges = re.findall('\\d*-\\d+', line)[0].split['-']
                        mp.append(((float(ranges[0]) + float(ranges[1])) / 
                            2 - 32) * (5 / 9))
                    else:
                        mp.append((float(re.findall('\\d*', line)[0]) - 32) *
                            (5 / 9))
        if len(mp) == 0:
            return None
        else:
            return sum(mp) / len(mp)


<mask token>

<mask token>


class MeltingPoint(APIFunction):

    def run(self, data):
        melting_data = str(experimental_properties(data, 'Melting Point'))
        mp = []
        for point in melting_data['Information']:
            line = str(point['Value']['StringWithMarkup'][0]['String']).lower()
            if ('decomp' not in line and 'greater' not in line and 'less'
                 not in line and 'approx' not in line and '<' not in line and
                '>' not in line):
                if '°c' in line:
                    if re.match('\\d*-\\d+', line):
                        ranges = re.findall('\\d*-\\d+', line)[0].split['-']
                        mp.append((float(ranges[0]) + float(ranges[1])) / 2)
                    else:
                        mp.append(float(re.findall('\\d*', line)[0]))
                elif '°f' in line:
                    if re.match('\\d*-\\d+', line):
                        ranges = re.findall('\\d*-\\d+', line)[0].split['-']
                        mp.append(((float(ranges[0]) + float(ranges[1])) / 
                            2 - 32) * (5 / 9))
                    else:
                        mp.append((float(re.findall('\\d*', line)[0]) - 32) *
                            (5 / 9))
        if len(mp) == 0:
            return None
        else:
            return sum(mp) / len(mp)


collect('data/melting_point/list.txt', 'mp.txt', MeltingPoint)

from pubchem import experimental_properties
from collect import collect, APIFunction
import re


class MeltingPoint(APIFunction):

    def run(self, data):
        melting_data = str(experimental_properties(data, 'Melting Point'))
        mp = []
        for point in melting_data['Information']:
            line = str(point['Value']['StringWithMarkup'][0]['String']).lower()
            if ('decomp' not in line and 'greater' not in line and 'less'
                 not in line and 'approx' not in line and '<' not in line and
                '>' not in line):
                if '°c' in line:
                    if re.match('\\d*-\\d+', line):
                        ranges = re.findall('\\d*-\\d+', line)[0].split['-']
                        mp.append((float(ranges[0]) + float(ranges[1])) / 2)
                    else:
                        mp.append(float(re.findall('\\d*', line)[0]))
                elif '°f' in line:
                    if re.match('\\d*-\\d+', line):
                        ranges = re.findall('\\d*-\\d+', line)[0].split['-']
                        mp.append(((float(ranges[0]) + float(ranges[1])) / 
                            2 - 32) * (5 / 9))
                    else:
                        mp.append((float(re.findall('\\d*', line)[0]) - 32) *
                            (5 / 9))
        if len(mp) == 0:
            return None
        else:
            return sum(mp) / len(mp)


collect('data/melting_point/list.txt', 'mp.txt', MeltingPoint)

from pubchem import experimental_properties
from collect import collect, APIFunction
import re


class MeltingPoint(APIFunction):

    # Takes JSON data and finds melting points and averages them
    def run(self, data):
        melting_data = str(experimental_properties(data, 'Melting Point'))

        mp = []

        for point in melting_data['Information']:
            line = str(point['Value']['StringWithMarkup'][0]['String']).lower()
            if 'decomp' not in line and \
                    'greater' not in line and \
                    'less' not in line and \
                    'approx' not in line and \
                    '<' not in line and \
                    '>' not in line:

                # Get either a range of temperature or the measured temperature
                if '°c' in line:
                    if re.match(r"\d*-\d+", line):
                        ranges = re.findall(r"\d*-\d+", line)[0].split['-']
                        mp.append((float(ranges[0]) + float(ranges[1])) / 2)
                    else:
                        mp.append(float(re.findall(r"\d*", line)[0]))
                # Same as getting celsius just convert
                elif '°f' in line:
                    if re.match(r"\d*-\d+", line):
                        ranges = re.findall(r"\d*-\d+", line)[0].split['-']
                        mp.append((((float(ranges[0]) + float(ranges[1])) / 2) - 32) * (5 / 9))
                    else:
                        mp.append((float(re.findall(r"\d*", line)[0]) - 32) * (5 / 9))

        if len(mp) == 0:
            return None
        else:
            return sum(mp) / len(mp)

# Takes list of molecules and outputs the smiles and corresponding melting point from melting_point function
collect('data/melting_point/list.txt', 'mp.txt', MeltingPoint)