Skip to content
Snippets Groups Projects
proc_input_view.py 21.18 KiB
# 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

# 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)
        json_data_report=json_data['GPhL_pipedream']
        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']

        logger.info(json_data_statistics)
        logger.info(json_data_solutions)

        try:
            report=storeParseReport(json_data_report)
            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)

            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
    """

    try:        
        # GPhL_pipedream=data['GPhL_pipedream']

        # autoprocscaling=data['dataprocessing']['processingdata']['AutoProcscaling']
        # autoprocscalingstatistics=data['dataprocessing']['processingdata']['AutoProcScalingStatistics']
        # autoproc=data['dataprocessing']['processingdata']['AutoProc']
        # referencedata=data['dataprocessing']['referencedata']
        
        # postrefinement=data['ligandfitting']['ligands']['1']['postrefinement']['cycles']['2']
        # molprobity=data['ligandfitting']['ligands']['1']['validationstatistics']['molprobity']

        # IF TEST

        report=Report(command=data['command'], 
            jsonversion=data['jsonversion'],
            runby=data['runby'],
            runfrom=data['runfrom'],
            jobid=data['jobid'],
            gphlpipedream_output=data['output'],
            version=data['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'],
            
        #     losecontacts=solutions['closecontacts'],
        #     chain=solutions['chain'],
        #     contactscore=solutions['contactscore'],
        #     ligandstrain=solutions['ligandstrain'],
        #     poorfit=solutions['poorfit'],
        #     output=solutions['output'],
        #     correlationcoefficient=solutions['correlationcoefficient'],
        #     rhofitscore=solutions['rhofitscore'],

        #     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'],

        #     datatype=data['dataprocessing']['datatype'],
        #     processingtype=data['dataprocessing']['processingtype'],
        #     symmetry=referencedata['symmetry'],
        #     gamma=referencedata['cell']['gamma'],
        #     a=referencedata['cell']['a'],
        #     alpha=referencedata['cell']['alpha'],
        #     b=referencedata['cell']['b'],
        #     beta=referencedata['cell']['beta'],
        #     c=referencedata['cell']['c'],

        #     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'],

        # number=data['refinement']['models']['1']['number'],
        # LMRscore=data['refinement']['models']['1']['LMRscore'],
        # modelname=data['refinement']['models']['1']['modelname'],
        # selectedmodel=data['refinement']['selectedmodel'],
        # refinementprotocol=data['refinement']['refinementprotocol'])

            # postrefinement_WilsonB=['WilsonB'],
            # postrefinement_Rfree=['Rfree'],
            # postrefinement_MeanB=['MeanB'],
            # postrefinement_type=['type'],
            # postrefinement_step=['step'],
            # postrefinement_RMSbonds=['RMSbonds'],
            # postrefinement_RMSangles=['RMSangles'],
            # postrefinement_R=['R'],
            # postrefinement_WatersPresent=['WatersPresent'],

            # refinement_WilsonB=['WilsonB'],
            # refinement_Rfree=['Rfree'],
            # refinement_MeanB=['MeanB'],
            # refinement_type=['type'],
            # refinement_step=['step'],
            # refinement_RMSbonds=['RMSbonds'],
            # refinement_RMSangles=['RMSangles'],
            # refinement_R=['R'],
            # refinement_WatersPresent=['WatersPresent'],

        report.save()
        return report.serialize

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

@csrf_exempt
def storeParseRC(data, report):

    """
    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"})

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

@csrf_exempt
def storeParseLigand(data, report):

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

    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

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

@csrf_exempt
def storeParsePRC(data, ligand):

    """
    Creates nodes for each PRC (Post-Refinement Cycle) with relative properties
    """

    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"})

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

@csrf_exempt
def storeParseLigandSolutions(data, ligand):

    """
    Creates nodes for each ligand solutions with relative properties
    """

    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"})

@csrf_exempt
def storeParseLigandStatistics(data, ligand):

    """
    Creates nodes for each ligand solutions with relative properties
    """

    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"})

@csrf_exempt
def connectReportLigand(data1, data2):

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

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

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

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

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

@csrf_exempt
def connectLigandLigandSolutions(data1, data2):
    
    """
    Create a relationship between a ligand and relative solutions
    """

    try:
        ligand=Ligand.nodes.get(ligand_id=data1)
        solutions=LigandSolutions.nodes.get(uuid=data2)

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

@csrf_exempt
def connectLigandLigandStatistics(data1, data2):
    
    """
    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)

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

@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)

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