def post(self):
        """
        Render JSON that has been POST'ed to this resource
        :return: A Flask Response with the rendered image
        :rtype: Response
        """
        # Parse the query options
        args = interaction_arg_parser.parse_args()
        try:
            # We exepct a JSON object in request.data with the protein and ligand data structures
            payload = json.loads(request.data.decode("utf-8"))
            self.__validate_schema(payload)

            # Try to read the ligand from the payload
            try:
                ligand = read_molecule_from_string(
                    payload['ligand']['value'],
                    payload['ligand']['format'],
                    payload['ligand']['gz'] if 'gz' in payload['ligand'] else False,
                    args['reparse']
                )
            except Exception as ex:
                message = "Error reading ligand"
                if args['debug']:
                    message += ": {0}".format(str(ex))
                raise Exception(message)

            # Try to read the receptor from the payload
            try:
                receptor = read_molecule_from_string(
                    payload['receptor']['value'],
                    payload['receptor']['format'],
                    payload['receptor']['gz'] if 'gz' in payload['receptor'] else False,
                    args['reparse']
                )
            except Exception as ex:
                message = "Error reading receptor"
                if args['debug']:
                    message += ": {0}".format(str(ex))
                raise Exception(message)

            # Render the image
            return _render_image(receptor, ligand, args)

        # On error render a PNG with an error message
        except Exception as ex:
            if args['debug']:
                return Response(json.dumps({"error": str(ex)}), status=400, mimetype='application/json')
            else:
                return render_error_image(args['width'], args['height'], str(ex))
Beispiel #2
0
    def post(self):
        """
        Render JSON that has been POST'ed to this resource
        :return: A Flask Response with the rendered image
        :rtype: Response
        """
        # Parse the query options
        args = interaction_arg_parser.parse_args()
        try:
            # We exepct a JSON object in request.data with the protein and ligand data structures
            payload = json.loads(request.data.decode("utf-8"))
            self.__validate_schema(payload)

            # Try to read the ligand from the payload
            try:
                ligand = read_molecule_from_string(
                    payload['ligand']['value'], payload['ligand']['format'],
                    payload['ligand']['gz']
                    if 'gz' in payload['ligand'] else False, args['reparse'])
            except Exception as ex:
                message = "Error reading ligand"
                if args['debug']:
                    message += ": {0}".format(str(ex))
                raise Exception(message)

            # Try to read the receptor from the payload
            try:
                receptor = read_molecule_from_string(
                    payload['receptor']['value'],
                    payload['receptor']['format'], payload['receptor']['gz']
                    if 'gz' in payload['receptor'] else False, args['reparse'])
            except Exception as ex:
                message = "Error reading receptor"
                if args['debug']:
                    message += ": {0}".format(str(ex))
                raise Exception(message)

            # Render the image
            return _render_image(receptor, ligand, args)

        # On error render a PNG with an error message
        except Exception as ex:
            if args['debug']:
                return Response(json.dumps({"error": str(ex)}),
                                status=400,
                                mimetype='application/json')
            else:
                return render_error_image(args['width'], args['height'],
                                          str(ex))
Beispiel #3
0
    def post(self):
        """
        Convert a molecule to another file format
        :return: A Flask Response with the rendered image
        :rtype: Response
        """
        # Parse the query options
        try:
            # We exepct a JSON object in request.data with the protein and ligand data structures
            payload = json.loads(request.data.decode("utf-8"))
            # Checks to make sure we have everything we need in payload
            self.__validate_schema(payload)
            # Read the molecule
            mol = read_molecule_from_string(
                payload['molecule']['value'],
                payload['molecule']['input']['format'],
                payload['molecule']['input']['gz']
                if 'gz' in payload['molecule']['input'] else False,
                payload['molecule']['input']['reparse']
                if 'reparse' in payload['molecule']['input'] else False)

            # Prepare the molecule for writing
            ofs = oemolostream()
            if payload['molecule']['output']['format'] == "smiles":
                ofs_format = OEFormat_SMI
            else:
                ofs_format = OEGetFileType(
                    to_utf8(payload['molecule']['output']['format']))
            if ofs_format == OEFormat_UNDEFINED:
                raise Exception("Unknown output file type: " +
                                payload['molecule']['output']['format'])
            ofs.SetFormat(ofs_format)
            ofs.openstring()
            OEWriteMolecule(ofs, mol)

            # Get molecule output stream
            if 'gz' in payload['molecule']['output'] and payload['molecule'][
                    'output']['gz']:
                output = compress_string(ofs.GetString().decode('utf-8'))
            else:
                output = ofs.GetString().decode('utf-8')

            return Response(json.dumps({
                'molecule': {
                    'value':
                    output,
                    'format':
                    payload['molecule']['output']['format'],
                    'gz':
                    payload['molecule']['output']['gz']
                    if 'gz' in payload['molecule']['output'] else False
                }
            }),
                            status=200,
                            mimetype='application/json')

        except Exception as ex:
            return Response(json.dumps({"error": str(ex)}),
                            status=400,
                            mimetype='application/json')
Beispiel #4
0
 def get(self, fmt):
     """
     Render an image with the molecule passed through the URL
     :param fmt: The image format
     :type fmt: str
     :return: The rendered image
     :rtype: Response
     """
     # Parse the query options
     args = depictor_arg_parser.parse_args()
     try:
         # Read the molecule
         mol = read_molecule_from_string(args['val'], fmt, bool(args['gz']),
                                         bool(args['reparse']))
         # Render the image
         return self.__render_image(mol, args)
     # On error render a PNG with an error message
     except Exception as ex:
         if args['debug']:
             return Response(json.dumps({"error": str(ex)}),
                             status=400,
                             mimetype='application/json')
         else:
             return render_error_image(args['width'], args['height'],
                                       str(ex))
Beispiel #5
0
    def post(self, fmt):
        """
        Render a raw receptor-ligand that has been POST'ed to this resource by first searching for the ligand
        :return: A Flask Response with the rendered image
        :rtype: Response
        """
        # Parse the query options
        args = interaction_arg_parser.parse_args()
        try:
            # Try to read the receptor-ligand complex
            try:
                mol = read_molecule_from_string(request.data.decode("utf-8"),
                                                fmt, args['gz'],
                                                args['reparse'])
            except Exception as ex:
                message = "Error reading molecule file"
                if args['debug']:
                    message += ": {0}".format(str(ex))
                raise Exception(message)

            # Generate the ligand selection functor
            if args['chain'] or args['resi'] or args['resn']:
                functor = generate_ligand_functor(args['chain'], args['resi'],
                                                  args['resn'])
            else:
                raise Exception("No ligand selection options given")

            # Split the ligand from the complex
            ligand = OEGraphMol()
            OESubsetMol(ligand, mol, functor, False, False)

            # Check the ligand
            if not ligand or ligand.NumAtoms() == 0:
                raise Exception("No atoms matched ligand selection")

            # Delete the ligand from the complex
            for atom in mol.GetAtoms(functor):
                mol.DeleteAtom(atom)

            # Error check receptor
            if not mol or mol.NumAtoms() == 0:
                raise Exception("No atoms in receptor")

            return _render_image(mol, ligand, args)

        except Exception as ex:
            if args['debug']:
                return Response(json.dumps({"error": str(ex)}),
                                status=400,
                                mimetype='application/json')
            else:
                return render_error_image(args['width'], args['height'],
                                          str(ex))
    def post(self, fmt):
        """
        Render a raw receptor-ligand that has been POST'ed to this resource by first searching for the ligand
        :return: A Flask Response with the rendered image
        :rtype: Response
        """
        # Parse the query options
        args = interaction_arg_parser.parse_args()
        try:
            # Try to read the receptor-ligand complex
            try:
                mol = read_molecule_from_string(
                    request.data.decode("utf-8"),
                    fmt,
                    args['gz'],
                    args['reparse']
                )
            except Exception as ex:
                message = "Error reading molecule file"
                if args['debug']:
                    message += ": {0}".format(str(ex))
                raise Exception(message)

            # Generate the ligand selection functor
            if args['chain'] or args['resi'] or args['resn']:
                functor = generate_ligand_functor(args['chain'], args['resi'], args['resn'])
            else:
                raise Exception("No ligand selection options given")

            # Split the ligand from the complex
            ligand = OEGraphMol()
            OESubsetMol(ligand, mol, functor, False, False)

            # Check the ligand
            if not ligand or ligand.NumAtoms() == 0:
                raise Exception("No atoms matched ligand selection")

            # Delete the ligand from the complex
            for atom in mol.GetAtoms(functor):
                mol.DeleteAtom(atom)

            # Error check receptor
            if not mol or mol.NumAtoms() == 0:
                raise Exception("No atoms in receptor")

            return _render_image(mol, ligand, args)

        except Exception as ex:
            if args['debug']:
                return Response(json.dumps({"error": str(ex)}), status=400, mimetype='application/json')
            else:
                return render_error_image(args['width'], args['height'], str(ex))
    def post(self):
        """
        Convert a molecule to another file format
        :return: A Flask Response with the rendered image
        :rtype: Response
        """
        # Parse the query options
        try:
            # We exepct a JSON object in request.data with the protein and ligand data structures
            payload = json.loads(request.data.decode("utf-8"))
            # Checks to make sure we have everything we need in payload
            self.__validate_schema(payload)
            # Read the molecule
            mol = read_molecule_from_string(
                payload['molecule']['value'],
                payload['molecule']['input']['format'],
                payload['molecule']['input']['gz'] if 'gz' in payload['molecule']['input'] else False,
                payload['molecule']['input']['reparse'] if 'reparse' in payload['molecule']['input'] else False
            )

            # Prepare the molecule for writing
            ofs = oemolostream()
            if payload['molecule']['output']['format'] == "smiles":
                ofs_format = OEFormat_SMI
            else:
                ofs_format = OEGetFileType(to_utf8(payload['molecule']['output']['format']))
            if ofs_format == OEFormat_UNDEFINED:
                raise Exception("Unknown output file type: " + payload['molecule']['output']['format'])
            ofs.SetFormat(ofs_format)
            ofs.openstring()
            OEWriteMolecule(ofs, mol)

            # Get molecule output stream
            if 'gz' in payload['molecule']['output'] and payload['molecule']['output']['gz']:
                output = compress_string(ofs.GetString().decode('utf-8'))
            else:
                output = ofs.GetString().decode('utf-8')

            return Response(json.dumps(
                {
                    'molecule': {
                        'value': output,
                        'format': payload['molecule']['output']['format'],
                        'gz': payload['molecule']['output']['gz'] if 'gz' in payload['molecule']['output'] else False
                    }
                }
            ), status=200, mimetype='application/json')

        except Exception as ex:
            return Response(json.dumps({"error": str(ex)}), status=400, mimetype='application/json')
 def post(self, fmt):
     """
     Render an image with the molecule POST'ed to this resource
     :param fmt: The molecule format
     :type fmt: str
     :return: The rendered image
     :rtype: Response
     """
     # Parse the query options
     args = depictor_arg_parser.parse_args()
     try:
         # Read the molecule
         mol = read_molecule_from_string(request.data.decode("utf-8"), fmt, bool(args['gz']), bool(args['reparse']))
         # Render the image
         return self.__render_image(mol, args)
     # On error render a PNG with an error message
     except Exception as ex:
         if args['debug']:
             return Response(json.dumps({"error": str(ex)}), status=400, mimetype='application/json')
         else:
             return render_error_image(args['width'], args['height'], str(ex))