Exemple #1
0
 def response_error(self, error):
     reply_content = KQMLList()
     for e in error:
         error_msg = '"%s"' %\
             str(e).encode('string-escape').replace('"', '\\"')
         reply_content.add(error_msg)
     return self.format_error(reply_content.to_string())
Exemple #2
0
 def reply(self, ):
     reply_msg = KQMLPerformative('reply')
     reply_content = KQMLList()
     for t in self.tests:
         print 'reply {0}'.format(t)
         t_content = t.get_content()
         if t_content:
             reply_content.add(t_content.to_string())
     reply_msg.setParameter(':content', reply_content)
     return reply_msg
Exemple #3
0
    def respond_simulate_model(self, content_list):
        '''
        Response content to simulate-model request
        '''
        model_str = content_list.get_keyword_arg(':model')
        try:
            #model_str = model_str.to_string()
            model = self.decode_model(model_str)
        except InvalidModelDescriptionError as e:
            logger.error(e)
            reply_content =\
                KQMLList.from_string('(FAILURE :reason INVALID_MODEL)')
            return reply_content
        except Exception as e:
            logger.error(e)
            reply_content =\
                KQMLList.from_string('(FAILURE :reason INVALID_MODEL)')
            return reply_content

        target_entity = content_list.get_keyword_arg(':target_entity')
        if target_entity is not None:
            target_entity = target_entity.to_string()[1:-1]
        else:
            reply_content =\
                KQMLList.from_string('(FAILURE :reason MISSING_PARAMETER)')
            return reply_content
        target_pattern = content_list.get_keyword_arg(':target_pattern')
        if target_pattern is not None:
            target_pattern = target_pattern.to_string().lower()
        else:
            reply_content =\
                KQMLList.from_string('(FAILURE :reason MISSING_PARAMETER)')
            return reply_content
        condition_entity = content_list.get_keyword_arg(':condition_entity')
        if condition_entity is not None:
            condition_entity = condition_entity.to_string()[1:-1]
        condition_type = content_list.get_keyword_arg(':condition_type')
        if condition_type is not None:
            condition_type = condition_type.to_string().lower()

        self.get_context(model)
        if condition_entity is None:
            target_match = self.mea.check_pattern(model, target_entity,
                                                  target_pattern)
        else:
            target_match = self.mea.compare_conditions(model, target_entity,
                                                       target_pattern,
                                                       condition_entity,
                                                       condition_type)
        target_match_str = 'TRUE' if target_match else 'FALSE'
        reply_content = KQMLList()
        reply_content.add('SUCCESS :content (:target_match %s)' %
                          target_match_str)
        return reply_content
Exemple #4
0
    def respond_test(self):
        '''
        Response content to version message
        '''
        reply_content = KQMLList()
        version_response = KQMLList.from_string( '' +\
            '(ONT::TELL :content ' +\
            ')')

        reply_content.add(KQMLList(version_response))
        return reply_content
Exemple #5
0
    def respond_find_treatment(self, content_list):
        '''
        Response content to find-treatment request
        '''
        reply_content = KQMLList()
        #TODO: eliminate code duplication here
        disease_arg = content_list.get_keyword_arg(':disease')
        try:
            disease = self.get_disease(disease_arg)
        except Exception as e:
            logger.error(e)
            msg_str = '(FAILURE :reason INVALID_DISEASE)'
            reply_content = KQMLList.from_string(msg_str)
            return reply_content

        if disease.disease_type != 'cancer':
            reply_content.add('FAILURE :reason DISEASE_NOT_FOUND')
            return reply_content

        logger.debug('Disease: %s' % disease.name)

        try:
            mut_protein, mut_percent = \
                self.dtda.get_top_mutation(disease.name)
        except DiseaseNotFoundException:
            reply_content.add('FAILURE :reason DISEASE_NOT_FOUND')
            return reply_content

        # TODO: get functional effect from actual mutations
        # TODO: add list of actual mutations to response
        # TODO: get fraction not percentage from DTDA
        reply_content.add(
            KQMLList.from_string('(SUCCESS ' +
                                 ':protein (:name %s :hgnc %s) ' %
                                 (mut_protein, mut_protein) +
                                 ':prevalence %.2f ' % (mut_percent / 100.0) +
                                 ':functional-effect ACTIVE)'))

        # Try to find a drug
        drug_names, chebi_ids = self.dtda.find_target_drugs(mut_protein)
        drug_list_str = ''
        for dn, ci in zip(drug_names, chebi_ids):
            if ci is None:
                drug_list_str += '(:name %s) ' % dn.encode('ascii', 'ignore')
            else:
                drug_list_str += '(:name %s :chebi_id %s) ' % (dn, ci)
        reply_content.add(
            KQMLList.from_string('(SUCCESS :drugs (' + drug_list_str + '))'))

        return reply_content
Exemple #6
0
 def respond_is_drug_target(self, content_list):
     '''
     Response content to is-drug-target request
     '''
     drug_arg = content_list.get_keyword_arg(':drug')
     drug = drug_arg[0].to_string()
     target_arg = content_list.get_keyword_arg(':target')
     target = target_arg[0].to_string()
     reply_content = KQMLList()
     try:
         is_target = self.dtda.is_nominal_drug_target(drug, target)
     except DrugNotFoundException:
         reply_content.add('FAILURE :reason DRUG_NOT_FOUND')
         return reply_content
     status = 'SUCCESS'
     if is_target:
         is_target_str = 'TRUE'
     else:
         is_target_str = 'FALSE'
     msg_str = '%s :is-target %s' %\
               (status, is_target_str)
     reply_content.add(msg_str)
     return reply_content
Exemple #7
0
def render_status(status):
    reply_content = KQMLList()
    reply_content.add(":status")
    if 'plot' in status:
        plot_data = status['plot']
        plot = KQMLList()
        plot.add(':plot')
        if 'legend' in plot_data:
            legend = KQMLList()
            legend.add(':legend')
            for label in plot_data['legend']:
                legend.add('"' + label + '"')
            plot.add(legend)
        if 'observables' in plot_data:
            observables = KQMLList()
            observables.add(':observables')
            for o in plot_data['observables']:
                observation = KQMLList()
                observation.add(':observation')
                if 'time' in o:
                    time = KQMLList()
                    time.add(':time')
                    time.add(render_value(o['time']))
                    observation.add(time)
                if 'values' in o:
                    values = KQMLList()
                    values.add(':values')
                    for f in o['values']:
                        values.add(render_value(f))
                    observation.add(values)
                observables.add(observation)
            plot.add(observables)
        reply_content.add(plot)
    if 'tracked_events' in status:
        tracked_events = KQMLList()
        tracked_events.add(':tracked_events')
        tracked_events.add(render_value(status['tracked_events']))
        plot.add(tracked_events)
    if 'is_running' in status:
        is_running = KQMLList()
        is_running.add(':is_running')
        is_running.add(render_value(status['is_running']))
        plot.add(is_running)
    if 'event_percentage' in status:
        event_percentage = KQMLList()
        event_percentage.add(':event_percentage')
        event_percentage.add(render_value(status['event_percentage']))
        plot.add(event_percentage)
    if 'time_percentage' in status:
        time_percentage = KQMLList()
        time_percentage.add(':time_percentage')
        time_percentage.add(render_value(status['time_percentage']))
        plot.add(time_percentage)
    if 'time' in status:
        time = KQMLList()
        time.add(':time')
        time.add(render_value(status['time']))
        plot.add(time)
    if 'event' in status:
        event = KQMLList()
        event.add(':event')
        event.add(render_value(status['event']))
        plot.add(event)
    # trips is not happy with this not sure why
    # if 'log_messages':
    #     log_messages = KQMLList()
    #     log_messages.add(':log_messages')
    #     for message in status['log_messages']:
    #         log_messages.add("'"+render_value(message)+"'")
    #     plot.add(log_messages)
    return reply_content
Exemple #8
0
 def get_content(self):
     reply_content = KQMLList()
     if self.get_actual():
         actual_string = self.get_actual().to_string()
         expected_string = self.get_expected().to_string()
         print 'get_content:actual_string {0}'.format(actual_string)
         print 'get_content:expected_string {0}'.format(expected_string)
         if expected_string in actual_string:
             return None
         else:
             reply_content.add(":request")
             reply_content.add(self.get_request())
             reply_content.add(":expected")
             reply_content.add(self.get_expected())
             reply_content.add(":actual")
             reply_content.add(self.get_actual())
     else:
         reply_content.add(":request")
         reply_content.add(self.get_request())
         reply_content.add(":noresponse")
     return reply_content