Пример #1
0
def peak_data(samples):
    print(request.args)
    samples = samples.split("/")

    if "data" in request.args:
        print(request.args.get("data", type=str))
        peak = ConsolidatedPeak.from_quoted_string(
            request.args.get("data", type=str))
        print(peak)

        CAS = peak.hits[0].cas
        Name = peak.hits[0].name
        rt = peak.rt

        if CAS.replace("-", '').replace("0", '') == '':
            # CAS Number is all zeros
            pickle_name = hashlib.md5(Name.encode("utf-8")).hexdigest()
            # html_file_name = os.path.join(html_file_directory, f"{pickle_name}_{rt}.html")

            if (cache_dir / pickle_name).exists():
                with open(cache_dir / pickle_name, "rb") as f:
                    comp = pickle.load(f)
            else:
                # if True:
                comp = get_compounds(Name, 'name')[0]
                # Save to cache
                with open(cache_dir / pickle_name, "wb") as f:
                    pickle.dump(comp, f)

        else:
            if (cache_dir / CAS).exists():
                with open(cache_dir / CAS, "rb") as f:
                    comp = pickle.load(f)
            else:
                # if True:
                comp = get_compounds(CAS, 'name')[0]
                # Save to cache
                with open(cache_dir / CAS, "wb") as f:
                    pickle.dump(comp, f)

        return render_template(
            "properties_template_2.html",
            comp=comp,
            peak=peak,
            samples=samples,
        )

    else:
        # Legacy mode

        index = request.args.get("index", 0, type=int)
        filename = request.args.get("filename", '', type=str)

        peak_data = []

        if filename == '':
            return "Please provide a filename with ?filename=", 400

        with open(os.path.join(filename), "r") as jsonfile:
            for i, peak in enumerate(jsonfile):
                if i == index:
                    peak_data = json.loads(peak)

        if not peak_data:
            # Index was out of range
            return "Peak index out of range", 400

        CAS = peak_data["hits"][0]["CAS"]
        Name = peak_data["hits"][0]["Name"]
        rt = peak_data["average_rt"]

        if CAS.replace("-", '').replace("0", '') == '':
            # CAS Number is all zeros
            pickle_name = hashlib.md5(Name.encode("utf-8")).hexdigest()
            # html_file_name = os.path.join(html_file_directory, f"{pickle_name}_{rt}.html")

            if (cache_dir / pickle_name).exists():
                with open(cache_dir / pickle_name, "rb") as f:
                    comp = pickle.load(f)
            else:
                # if True:
                comp = get_compounds(Name, 'name')[0]
                # Save to cache
                with open(cache_dir / pickle_name, "wb") as f:
                    pickle.dump(comp, f)

        else:
            if (cache_dir / CAS).exists():
                with open(cache_dir / CAS, "rb") as f:
                    comp = pickle.load(f)
            else:
                # if True:
                comp = get_compounds(CAS, 'name')[0]
                # Save to cache
                with open(cache_dir / CAS, "wb") as f:
                    pickle.dump(comp, f)

        return render_template(
            "properties_template.html",
            comp=comp,
            data=peak_data,
            samples=samples,
        )
Пример #2
0
        "621-65-8", "623-26-7", "623-87-0", "626-17-5", "628-96-6", "638-36-8",
        "64-18-6", "64-19-7", "644-08-6", "66-25-1", "693-21-0", "71-43-2",
        "7440-44-0", "75-05-8", "75-07-0", "76-22-2", "7782-42-5", "78-11-5",
        "78-84-2", "78411-60-6", "8006-44-8", "8012-95-1", "83-32-9",
        "836-30-6", "84-62-8", "84-66-2", "84-74-2", "84002-64-2", "85-01-8",
        "85-98-3", "85209-46-7", "86-30-6", "86-53-3", "86-74-8", "88-72-2",
        "88-73-7", "88-74-4", "88-75-5", "88-89-1", "90-02-8", "90-12-0",
        "9004-67-5", "9004-70-0", "9005-38-3", "91-15-6", "91-19-0", "91-20-3",
        "91-22-5", "91-57-6", "92-24-0", "92-82-0", "939-27-5", "95-13-6",
        "95-15-8", "95-16-9", "95-47-6", "95-48-7", "95-63-6", "961-68-2",
        "98-01-1", "98-86-2", "98-95-3", "99-08-1", "99-35-4", "99-65-0",
        "99-99-0"
]:

    try:
        comp = get_compounds(cas, 'name')[0]
    except:
        input(">")
        continue

    import pprint

    print(comp.IUPAC)
    print(comp.CAS)
    print(comp.cid)
    print(comp.inchi)
    print(comp.inchikey)
    print(comp.canonical_smiles)
    print(comp.melting_point)
    print(comp.boiling_point)
    print(comp.color)
Пример #3
0
def hit_data(samples):
    # print(request.args)
    samples = samples.split("/")

    maybe_make("cache")  # Internal Cache Directory

    if "data" in request.args:
        # print(request.args.get("data", type=str))
        hit = ConsolidatedSearchResult.from_quoted_string(
            request.args.get("data", type=str))
        # print(hit)

        CAS = hit.cas
        Name = hit.name

        if CAS.replace("-", '').replace("0", '') == '':
            # CAS Number is all zeros
            pickle_name = hashlib.md5(Name.encode("utf-8")).hexdigest()
            # html_file_name = os.path.join(html_file_directory, f"{pickle_name}_{rt}.html")

            if (cache_dir / pickle_name).exists():
                with open(cache_dir / pickle_name, "rb") as f:
                    comp = pickle.load(f)
            else:
                # Check that a connection an be established to PubChem server
                try:
                    urllib.request.urlopen(API_BASE, timeout=2)

                except urllib.error.HTTPError as e:
                    if e.code == 400:
                        pass
                    else:
                        raise e

                except urllib.error.URLError:
                    warnings.warn(
                        "Unable to connect to PubChem server. Check your internet connection and try again."
                    )
                    return render_template(
                        "properties_template_offline.html",
                        hit=hit,
                        samples=samples,
                    )

                try:
                    comp = get_compounds(CAS, 'name')[0]
                except IndexError:
                    comp = None

                # Save to cache
                with open(cache_dir / pickle_name, "wb") as f:
                    pickle.dump(comp, f)

        else:
            if (cache_dir / CAS).exists():
                with open(cache_dir / CAS, "rb") as f:
                    comp = pickle.load(f)
            else:
                # if True:
                try:
                    comp = get_compounds(CAS, 'name')[0]
                except IndexError:
                    comp = None

                # Save to cache
                with open(cache_dir / CAS, "wb") as f:
                    pickle.dump(comp, f)

        # TODO: Combine information from hit.reference_data and comp, e.g. synonyms

        return render_template(
            "properties_template_2.html",
            comp=comp,
            hit=hit,
            samples=samples,
        )

    else:
        # Legacy mode

        index = request.args.get("index", 0, type=int)
        filename = request.args.get("filename", '', type=str)

        peak_data = []

        if filename == '':
            return "Please provide a filename with ?filename=", 400

        with open(os.path.join(filename), "r") as jsonfile:
            for i, peak in enumerate(jsonfile):
                if i == index:
                    peak_data = json.loads(peak)

        if not peak_data:
            # Index was out of range
            return "Peak index out of range", 400

        CAS = peak_data["hits"][0]["CAS"]
        Name = peak_data["hits"][0]["Name"]
        rt = peak_data["average_rt"]

        if CAS.replace("-", '').replace("0", '') == '':
            # CAS Number is all zeros
            pickle_name = hashlib.md5(Name.encode("utf-8")).hexdigest()
            # html_file_name = os.path.join(html_file_directory, f"{pickle_name}_{rt}.html")

            if (cache_dir / pickle_name).exists():
                with open(cache_dir / pickle_name, "rb") as f:
                    comp = pickle.load(f)
            else:
                # if True:
                comp = get_compounds(Name, 'name')[0]
                # Save to cache
                with open(cache_dir / pickle_name, "wb") as f:
                    pickle.dump(comp, f)

        else:
            if (cache_dir / CAS).exists():
                with open(cache_dir / CAS, "rb") as f:
                    comp = pickle.load(f)
            else:
                # if True:
                comp = get_compounds(CAS, 'name')[0]
                # Save to cache
                with open(cache_dir / CAS, "wb") as f:
                    pickle.dump(comp, f)

        return render_template(
            "properties_template.html",
            comp=comp,
            data=peak_data,
            samples=samples,
        )
Пример #4
0
"""Test for PubChemPy"""

from chemistry_tools.lookup import get_compounds
from decimal import Decimal

comp = get_compounds("diphenylamine", 'name')[0]

import pprint
pprint.pprint(comp._physical_properties)

print(f"CID:							{'Passed!' if comp.cid == 11487 else 'Failed!'}")
print(f"CAS Number:						{'Passed!' if comp.CAS == '122-39-4' else 'Failed!'}")
print(
    f"Formula:						{'Passed!' if comp.molecular_formula == 'C12H11N' else 'Failed!'}"
)
print(
    f"IUPAC:							{'Passed!' if comp.IUPAC['String'] == 'N-phenylaniline' else 'Failed!'}"
)
print(
    f"InChI:							{'Passed!' if comp.inchi == 'InChI=1S/C12H11N/c1-3-7-11(8-4-1)13-12-9-5-2-6-10-12/h1-10,13H' else 'Failed!'}"
)
print(
    f"InChIKey:						{'Passed!' if comp.inchikey == 'DMBHHRLKUKUOEG-UHFFFAOYSA-N' else 'Failed!'}"
)
print(
    f"Canonical SMILES:				{'Passed!' if comp.canonical_smiles == 'C1=CC=C(C=C1)NC2=CC=CC=C2' else 'Failed!'}"
)
print(
    f"Isomeric SMILES:				{'Passed!' if True == False else 'Failed!'}")  # TODO
print(
    f"SMILES:							{'Passed!' if comp.smiles == 'C1=CC=C(C=C1)NC2=CC=CC=C2' else 'Failed!'}"