Skip to content
Snippets Groups Projects
proc_input_view.py 21.7 KiB
Newer Older
root's avatar
root committed
# Python imports
import json
import logging
import sys

# Django imports
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

# Models imports
from api.models.pipedream_model import GPhLPipedream
from api.models.report_model import Report
from api.models.dpstep_model import DPStep
from api.models.ligandsfitting_model import LigandsFitting
from api.models.reductionscaling_model import ReductionScaling
from api.models.refinement_cycles_model import RefinementCycles
from api.models.post_refinement_cycles_model import PostRefinementCycles
from api.models.ligand_model import Ligand
from api.models.ligand_statistics_model import LigandStatistics
from api.models.ligand_solutions_model import LigandSolutions
root's avatar
root committed
from api.models.reference_model import Reference
from api.models.structurefactors_model import StructureFactors
from api.models.mtzfile_model import MTZ
root's avatar
root committed

# Activate logger
logger=logging.getLogger('dataproc')

@csrf_exempt
def storeProcInput(request):

    """
    Parse JSON data and call store functions for each model
    """

    if request.method=='POST':
        json_data=json.loads(request.body)
root's avatar
root committed
        
        json_data_report=json_data
root's avatar
root committed
        # json_data_ligand=json_data['ligandfitting']['ligands']['1']
        # json_data_rc=json_data['refinement']['Cycles']
        # json_data_prc=json_data['ligandfitting']['ligands']['1']['postrefinement']['cycles']
        # json_data_solutions=json_data['ligandfitting']['ligands']['1']['solutions']
        # json_data_statistics=json_data['ligandfitting']['ligands']['1']['validationstatistics']['ligandstatistics']
root's avatar
root committed
        # json_data_reference=json_data['dataprocessing']['referencedata']['cell']
root's avatar
root committed
        # json_data_mtz=json_data['dataprocessing']
root's avatar
root committed

root's avatar
root committed
            report=storeParseReport(json_data_report)
root's avatar
root committed
            # ligand=storeParseLigand(json_data_ligand, report)
            # storeParseRC(json_data_rc, report)
            # storeParsePRC(json_data_prc, ligand)
            # storeParseLigandSolutions(json_data_solutions, ligand)
            # storeParseLigandStatistics(json_data_statistics, ligand)
root's avatar
root committed
            # structurefactors=storeParseStructureFactors(json_data_reference)
root's avatar
root committed
            # storeParseMTZ(json_data_mtz, structurefactors)
root's avatar
root committed

            return JsonResponse({"STATUS": "INPUT SUCCESSFULLY REGISTERED"})
        
        except :
            return JsonResponse({"STATUS":"ERROR OCCURRED"}, safe=False)

@csrf_exempt
def storeParseReport(data):

    """
    Creates nodes for each report with relative properties
    """

root's avatar
root committed
        GPhL_pipedream=data['GPhL_pipedream']
        autoprocscaling=data['dataprocessing']['processingdata']['AutoProcscaling']
        autoprocscalingstatistics=data['dataprocessing']['processingdata']['AutoProcScalingStatistics']
        autoproc=data['dataprocessing']['processingdata']['AutoProc']
        molprobity=data['ligandfitting']['ligands']['1']['validationstatistics']['molprobity']

        report=Report(command=GPhL_pipedream['command'], 
            jsonversion=GPhL_pipedream['jsonversion'],
            runby=GPhL_pipedream['runby'],
            runfrom=GPhL_pipedream['runfrom'],
            jobid=GPhL_pipedream['jobid'],
            gphlpipedream_output=GPhL_pipedream['output'],
            version=GPhL_pipedream['version'],

            molprobitypercentile=molprobity['molprobitypercentile'],
            ramaoutlierpercent=molprobity['ramaoutlierpercent'],
            cbetadeviations=molprobity['cbetadeviations'],
            ramafavoredpercent=molprobity['ramafavoredpercent'],
            poorrotamers=molprobity['poorrotamers'],
            rmsbonds=molprobity['rmsbonds'],
            rmsangles=molprobity['rmsangles'],
            clashpercentile=molprobity['clashpercentile'],
            poorrotamerspercent=molprobity['poorrotamerspercent'],
            clashscore=molprobity['clashscore'],
            ramafavored=molprobity['ramafavored'],
            molprobityscore=molprobity['molprobityscore'],
            ramaoutliers=molprobity['ramaoutliers'],

            recordTimeStamp=autoprocscaling['recordTimeStamp'],
            resolutionEllipsoidAxis13=autoprocscaling['resolutionEllipsoidAxis13'],
            resolutionEllipsoidAxis33=autoprocscaling['resolutionEllipsoidAxis33'],
            resolutionEllipsoidAxis23=autoprocscaling['resolutionEllipsoidAxis23'],
            resolutionEllipsoidValue2=autoprocscaling['resolutionEllipsoidValue2'],
            resolutionEllipsoidAxis21=autoprocscaling['resolutionEllipsoidAxis21'],
            resolutionEllipsoidAxis31=autoprocscaling['resolutionEllipsoidAxis31'],
            resolutionEllipsoidAxis22=autoprocscaling['resolutionEllipsoidAxis22'],
            resolutionEllipsoidAxis32=autoprocscaling['resolutionEllipsoidAxis32'],
            resolutionEllipsoidValue3=autoprocscaling['resolutionEllipsoidValue3'],
            resolutionEllipsoidAxis12=autoprocscaling['resolutionEllipsoidAxis12'],
            resolutionEllipsoidValue1=autoprocscaling['resolutionEllipsoidValue1'],
            resolutionEllipsoidAxis11=autoprocscaling['resolutionEllipsoidAxis11'],

            innerShell_multiplicity=autoprocscalingstatistics['innerShell']['multiplicity'],
            innerShell_resolutionLimitLow=autoprocscalingstatistics['innerShell']['resolutionLimitLow'],
            innerShell_rMeasWithinIPlusIMinus=autoprocscalingstatistics['innerShell']['rMeasWithinIPlusIMinus'],
            innerShell_DanoOverSigDano=autoprocscalingstatistics['innerShell']['DanoOverSigDano'],
            innerShell_completenessSpherical=autoprocscalingstatistics['innerShell']['completenessSpherical'],
            innerShell_nTotalUniqueObservations=autoprocscalingstatistics['innerShell']['nTotalUniqueObservations'],
            innerShell_rMerge=autoprocscalingstatistics['innerShell']['rMerge'],
            innerShell_rPimWithinIPlusIMinus=autoprocscalingstatistics['innerShell']['rPimWithinIPlusIMinus'],
            innerShell_anomalousCompletenessEllipsoidal=autoprocscalingstatistics['innerShell']['anomalousCompletenessEllipsoidal'],
            innerShell_nTotalObservations=autoprocscalingstatistics['innerShell']['nTotalObservations'],
            innerShell_completenessEllipsoidal=autoprocscalingstatistics['innerShell']['completenessEllipsoidal'],
            innerShell_anomalousCompleteness=autoprocscalingstatistics['innerShell']['anomalousCompleteness'],
            innerShell_anomalousMultiplicity=autoprocscalingstatistics['innerShell']['anomalousMultiplicity'],
            innerShell_resolutionLimitHigh=autoprocscalingstatistics['innerShell']['resolutionLimitHigh'],
            innerShell_completeness=autoprocscalingstatistics['innerShell']['completeness'],
            innerShell_ccHalf=autoprocscalingstatistics['innerShell']['ccHalf'],
            innerShell_rPimAllIPlusIMinus=autoprocscalingstatistics['innerShell']['rPimAllIPlusIMinus'],
            innerShell_meanIOverSigI=autoprocscalingstatistics['innerShell']['meanIOverSigI'],
            innerShell_anomalousCompletenessSpherical=autoprocscalingstatistics['innerShell']['anomalousCompletenessSpherical'],
            innerShell_ccAnomalous=autoprocscalingstatistics['innerShell']['ccAnomalous'],
            innerShell_rMeasAllIPlusIMinus=autoprocscalingstatistics['innerShell']['rMeasAllIPlusIMinus'],

            outerShell_multiplicity=autoprocscalingstatistics['outerShell']['multiplicity'],
            outerShell_resolutionLimitLow=autoprocscalingstatistics['outerShell']['resolutionLimitLow'],
            outerShell_rMeasWithinIPlusIMinus=autoprocscalingstatistics['outerShell']['rMeasWithinIPlusIMinus'],
            outerShell_DanoOverSigDano=autoprocscalingstatistics['outerShell']['DanoOverSigDano'],
            outerShell_completenessSpherical=autoprocscalingstatistics['outerShell']['completenessSpherical'],
            outerShell_nTotalUniqueObservations=autoprocscalingstatistics['outerShell']['nTotalUniqueObservations'],
            outerShell_rMerge=autoprocscalingstatistics['outerShell']['rMerge'],
            outerShell_rPimWithinIPlusIMinus=autoprocscalingstatistics['outerShell']['rPimWithinIPlusIMinus'],
            outerShell_anomalousCompletenessEllipsoidal=autoprocscalingstatistics['outerShell']['anomalousCompletenessEllipsoidal'],
            outerShell_nTotalObservations=autoprocscalingstatistics['outerShell']['nTotalObservations'],
            outerShell_completenessEllipsoidal=autoprocscalingstatistics['outerShell']['completenessEllipsoidal'],
            outerShell_anomalousCompleteness=autoprocscalingstatistics['outerShell']['anomalousCompleteness'],
            outerShell_anomalousMultiplicity=autoprocscalingstatistics['outerShell']['anomalousMultiplicity'],
            outerShell_resolutionLimitHigh=autoprocscalingstatistics['outerShell']['resolutionLimitHigh'],
            outerShell_completeness=autoprocscalingstatistics['outerShell']['completeness'],
            outerShell_ccHalf=autoprocscalingstatistics['outerShell']['ccHalf'],
            outerShell_rPimAllIPlusIMinus=autoprocscalingstatistics['outerShell']['rPimAllIPlusIMinus'],
            outerShell_meanIOverSigI=autoprocscalingstatistics['outerShell']['meanIOverSigI'],
            outerShell_anomalousCompletenessSpherical=autoprocscalingstatistics['outerShell']['anomalousCompletenessSpherical'],
            outerShell_ccAnomalous=autoprocscalingstatistics['outerShell']['ccAnomalous'],
            outerShell_rMeasAllIPlusIMinus=autoprocscalingstatistics['outerShell']['rMeasAllIPlusIMinus'],

            overall_multiplicity=autoprocscalingstatistics['overall']['multiplicity'],
            overall_resolutionLimitLow=autoprocscalingstatistics['overall']['resolutionLimitLow'],
            overall_rMeasWithinIPlusIMinus=autoprocscalingstatistics['overall']['rMeasWithinIPlusIMinus'],
            overall_DanoOverSigDano=autoprocscalingstatistics['overall']['DanoOverSigDano'],
            overall_completenessSpherical=autoprocscalingstatistics['overall']['completenessSpherical'],
            overall_nTotalUniqueObservations=autoprocscalingstatistics['overall']['nTotalUniqueObservations'],
            overall_rMerge=autoprocscalingstatistics['overall']['rMerge'],
            overall_rPimWithinIPlusIMinus=autoprocscalingstatistics['overall']['rPimWithinIPlusIMinus'],
            overall_anomalousCompletenessEllipsoidal=autoprocscalingstatistics['overall']['anomalousCompletenessEllipsoidal'],
            overall_nTotalObservations=autoprocscalingstatistics['overall']['nTotalObservations'],
            overall_completenessEllipsoidal=autoprocscalingstatistics['overall']['completenessEllipsoidal'],
            overall_anomalousCompleteness=autoprocscalingstatistics['overall']['anomalousCompleteness'],
            overall_anomalousMultiplicity=autoprocscalingstatistics['overall']['anomalousMultiplicity'],
            overall_resolutionLimitHigh=autoprocscalingstatistics['overall']['resolutionLimitHigh'],
            overall_completeness=autoprocscalingstatistics['overall']['completeness'],
            overall_ccHalf=autoprocscalingstatistics['overall']['ccHalf'],
            overall_rPimAllIPlusIMinus=autoprocscalingstatistics['overall']['rPimAllIPlusIMinus'],
            overall_meanIOverSigI=autoprocscalingstatistics['overall']['meanIOverSigI'],
            overall_anomalousCompletenessSpherical=autoprocscalingstatistics['overall']['anomalousCompletenessSpherical'],
            overall_ccAnomalous=autoprocscalingstatistics['overall']['ccAnomalous'],
            overall_rMeasAllIPlusIMinus=autoprocscalingstatistics['overall']['rMeasAllIPlusIMinus'],

            refinedCell_beta=autoproc['refinedCell_beta'],
            refinedCell_b=autoproc['refinedCell_b'],
            wavelength=autoproc['wavelength'],
            refinedCell_a=autoproc['refinedCell_a'],
            refinedCell_alpha=autoproc['refinedCell_alpha'],
            spaceGroup=autoproc['spaceGroup'],
            refinedCell_c=autoproc['refinedCell_c'],
            refinedCell_gamma=autoproc['refinedCell_gamma'])
root's avatar
root committed
        report.save()
        return report.serialize
root's avatar
root committed
    except:
        print(sys.exc_info()[0])
        return ({"STATUS": "ERROR OCCURRED WHILE REGISTERING REPORT"})

root's avatar
root committed
@csrf_exempt
def storeParseStructureFactors(data):

    """
    Creates nodes for 
    """
    try:
        structurefactors=StructureFactors(
            # uuid=data['uuid'],
            # datatype=data['datatype'],
            # processingtype=data['processingtype'],
            gamma=data['gamma'],
            a=data['a'],         
            alpha=data['alpha'],
root's avatar
root committed
            b=data['b'],
            beta=data['beta'],
root's avatar
root committed
            c=data['c'])
            # rsf_source=data['rsf_source'],
            # rsf_filesize=data['rsf_filesize'],
            # rsf_filepath=data['rsf_filepath'])

        structurefactors.save()
root's avatar
root committed
        # structurefactors2=structurefactors.serialize
root's avatar
root committed
        # structurefactors2=structurefactors.serialize
        # connectStructureFactorsRef(structurefactors2['structurefactors_node_properties']['uuid'], ref['ref_node_properties']['uuid'])

        return ({"STATUS": "STRUCTURE FACTORS REGISTERED"})

    except:
            print(sys.exc_info()[0])
            return ({"STATUS": "ERROR OCCURRED WHILE REGISTERING RC"})

@csrf_exempt
def storeParseMTZ(data, structurefactors):

    """
    Creates nodes for 
    """
    try:
        mtz=MTZ(datatype=data['datatype'])
        mtz.save()

        mtz2=mtz.serialize
        # connectStructureFactorsMTZ(structurefactors['structurefactors_node_properties']['uuid'], mtz2['mtz_node_properties']['uuid'])

        return ({"STATUS": "MTZ DATATYPE REGISTERED"})

    except:
            print(sys.exc_info()[0])
            return ({"STATUS": "ERROR OCCURRED WHILE REGISTERING RC"})

@csrf_exempt
def storeParseRC(data, report):
root's avatar
root committed

    """
    Creates nodes for each RC (Refinement Cycle) with relative properties
    """
    try:
        for input_rc in data:
            rc=RefinementCycles(cycle_number=input_rc['cycle_number'],
                WilsonB=input_rc['WilsonB'],
                Rfree=input_rc['Rfree'],
                MeanB=input_rc['MeanB'],
                rc_type=input_rc['type'],
                step=input_rc['step'],
                RMSbonds=input_rc['RMSbonds'],
                RMSangles=input_rc['RMSangles'],
                R=input_rc['R'])
                # WatersPresent=input_rc['WatersPresent'])
            rc.save()
            rc2=rc.serialize
            connectReportRC(report['report_node_properties']['jobid'], rc2['rc_node_properties']['uuid'])

        return ({"STATUS": "RC REGISTERED"})
root's avatar
root committed

    except:
            print(sys.exc_info()[0])
            return ({"STATUS": "ERROR OCCURRED WHILE REGISTERING RC"})
root's avatar
root committed

@csrf_exempt
def storeParseLigand(data, report):
root's avatar
root committed

    """
    Creates nodes for each ligand with relative properties
    """
root's avatar
root committed

    try:
            ligand=Ligand(ligand_id=data['id'])
            ligand.save()
            ligand2=ligand.serialize
            connectReportLigand(report['report_node_properties']['jobid'], ligand2['ligand_node_properties']['uuid'])
            
            return ligand2
root's avatar
root committed

    except:
            print(sys.exc_info()[0])
            return ({"STATUS": "ERROR OCCURRED WHILE REGISTERING LIGAND"})
root's avatar
root committed

@csrf_exempt
def storeParsePRC(data, ligand):
root's avatar
root committed

    """
    Creates nodes for each PRC (Post-Refinement Cycle) with relative properties
    """
root's avatar
root committed

    try:
        for input_prc in data:
            prc=PostRefinementCycles(cycle_number=input_prc['cycle_number'],
                WilsonB=input_prc['WilsonB'],
                Rfree=input_prc['Rfree'],
                MeanB=input_prc['MeanB'],
                prc_type=input_prc['type'],
                step=input_prc['step'],
                RMSbonds=input_prc['RMSbonds'],
                RMSangles=input_prc['RMSangles'],
                R=input_prc['R'])
                # WatersPresent=input_prc['WatersPresent']
            prc.save()
            prc2=prc.serialize
            connectLigandPRC(prc2['prc_node_properties']['uuid'], ligand['ligand_node_properties']['uuid'])

        return ({"STATUS": "PRC REGISTERED"})
root's avatar
root committed

    except:
            print(sys.exc_info()[0])
            return ({"STATUS": "ERROR OCCURRED WHILE REGISTERING PRC"})
root's avatar
root committed

@csrf_exempt
def storeParseLigandSolutions(data, ligand):
root's avatar
root committed

    """
    Creates nodes for each ligand solutions with relative properties
    """
root's avatar
root committed

    try:
        for input_solutions in data:

            solutions=LigandSolutions(closecontacts=input_solutions['closecontacts'],
                chain=input_solutions['chain'],
                contactscore=input_solutions['contactscore'],
                ligandstrain=input_solutions['ligandstrain'],
                poorfit=input_solutions['poorfit'],
                output=input_solutions['output'],
                correlationcoefficient=input_solutions['correlationcoefficient'],
                rhofitscore=input_solutions['rhofitscore'],
                solution_number=input_solutions['solution_number'])
                # WatersPresent=input_rc['WatersPresent'])
            solutions.save()
            solutions2=solutions.serialize
            connectLigandLigandSolutions(ligand['ligand_node_properties']['ligand_id'], solutions2['ligand_solutions_node_properties']['uuid'])

        return ({"STATUS": "LIGANDS SOLUTIONS REGISTERED"})
    except:
            print(sys.exc_info()[0])
            return ({"STATUS": "ERROR OCCURRED WHILE REGISTERING LIGANDS SOLUTIONS"})
root's avatar
root committed

@csrf_exempt
def storeParseLigandStatistics(data, ligand):
root's avatar
root committed

    """
    Creates nodes for each ligand solutions with relative properties
    """
root's avatar
root committed

    try:
        for input_statistics in data:

            statistics=LigandStatistics(ligandomin=input_statistics['ligandomin'],
                mogulzbond=input_statistics['mogulzbond'],
                ligandbmin=input_statistics['ligandbmin'],
                mogulring=input_statistics['mogulring'],
                moguldihe=input_statistics['moguldihe'],
                ligandbavg=input_statistics['ligandbavg'],
                mogulangl=input_statistics['mogulangl'],
                mogulbond=input_statistics['mogulbond'],
                ligandbmax=input_statistics['ligandbmax'],
                ligandid=input_statistics['ligandid'],
                ligandomax=input_statistics['ligandomax'],
                ligandcc=input_statistics['ligandcc'],
                mogulzangl=input_statistics['mogulzangl'])

            statistics.save()
            statistics2=statistics.serialize
            connectLigandLigandStatistics(ligand['ligand_node_properties']['ligand_id'], statistics2['ligand_statistics_node_properties']['uuid'])

        return ({"STATUS": "LIGANDS SOLUTIONS REGISTERED"})
    except:
            print(sys.exc_info()[0])
            return ({"STATUS": "ERROR OCCURRED WHILE REGISTERING LIGANDS SOLUTIONS"})
root's avatar
root committed

@csrf_exempt
def connectReportLigand(data1, data2):
root's avatar
root committed

    """
    Create a relationship between a report and a ligand
    """
root's avatar
root committed
    
    try:
        report=Report.nodes.get(jobid=data1)
        ligand=Ligand.nodes.get(uuid=data2)
        return JsonResponse({"STATUS": report.has_ligand.connect(ligand)}, safe=False)
root's avatar
root committed

    except:
        return JsonResponse({"STATUS": "ERROR OCCURRED WHILE CONNECTING REPORT TO LIGAND"}, safe=False)
root's avatar
root committed

@csrf_exempt
def connectReportRC(data1, data2):
    
    """
    Create a relationship between a report and a rc
    """
root's avatar
root committed

    try:
        report=Report.nodes.get(jobid=data1)
        rc=RefinementCycles.nodes.get(uuid=data2)
        return JsonResponse({"STATUS": report.has_refinement_cycles.connect(rc)}, safe=False)
root's avatar
root committed

    except:
        return JsonResponse({"STATUS": "ERROR OCCURRED WHILE CONNECTING REPORT TO RCs"}, safe=False)
root's avatar
root committed

@csrf_exempt
def connectLigandLigandSolutions(data1, data2):
    
    """
    Create a relationship between a ligand and relative solutions
    """
root's avatar
root committed

    try:
        ligand=Ligand.nodes.get(ligand_id=data1)
        solutions=LigandSolutions.nodes.get(uuid=data2)
root's avatar
root committed

        return JsonResponse({"STATUS": ligand.has_solutions.connect(solutions)}, safe=False)
    except:
        return JsonResponse({"STATUS": "ERROR OCCURRED WHILE CONNECTING LIGAND TO SOLUTIONS"}, safe=False)
root's avatar
root committed

@csrf_exempt
def connectLigandLigandStatistics(data1, data2):
root's avatar
root committed
    
    """
    Create a relationship between a ligand and relative statistics
    """

    try:
        ligand=Ligand.nodes.get(ligand_id=data1)
        statistics=LigandStatistics.nodes.get(uuid=data2)
        return JsonResponse({"STATUS": ligand.has_statistics.connect(statistics)}, safe=False)
root's avatar
root committed

    except:
        return JsonResponse({"STATUS": "ERROR OCCURRED WHILE CONNECTING LIGAND TO PRCs"}, safe=False)
root's avatar
root committed

@csrf_exempt
def connectLigandPRC(data1, data2):
    
    """
    Create a relationship between a report and a prc
    """

    try:
        ligand=Ligand.nodes.get(uuid=data2)
        prc=PostRefinementCycles.nodes.get(uuid=data1)
        return JsonResponse({"STATUS": ligand.has_postrefinement_cycles.connect(prc)}, safe=False)
root's avatar
root committed

    except:
        return JsonResponse({"STATUS": "ERROR OCCURRED WHILE CONNECTING LIGAND TO PRCs"}, safe=False)
root's avatar
root committed

root's avatar
root committed
@csrf_exempt
def connectStructureFactorsRef(data1, data2):
    
    """
    Create a relationship between a SF (Structure factors) and Reference node.
    """

    try:
        structurefactors=StructureFactors.nodes.get(uuid=data1)
        ref=Reference.nodes.get(uuid=data2)
        return JsonResponse({"STATUS": structurefactors.labelled.connect(ref)}, safe=False)

    except:
        return JsonResponse({"STATUS": "ERROR OCCURRED WHILE CONNECTING STRUCTURE FACTORS TO REFERENCE"}, safe=False)

@csrf_exempt
def connectStructureFactorsMTZ(data1, data2):
    
    """
    Create a relationship between a SF (Structure factors) and MTZ node.
    """

    try:
        structurefactors=StructureFactors.nodes.get(uuid=data1)
        mtz=MTZ.nodes.get(uuid=data2)
        return JsonResponse({"STATUS": structurefactors.is_mtz.connect(mtz)}, safe=False)

    except:
        return JsonResponse({"STATUS": "ERROR OCCURRED WHILE CONNECTING STRUCTURE FACTORS TO MTZ"}, safe=False)