def test_write_tsv(self):
     expected_file = 'test.tsv'
     try:
         compound_set = pickle.load(open('compound_set.pkl', 'rb'))
         file_path = compound_parsing.write_tsv(compound_set, expected_file)
         filecmp.cmp(file_path, 'test_out.tsv')
     finally:
         if os.path.exists(expected_file):
             os.remove(expected_file)
Exemple #2
0
    def compound_set_to_file(self, ctx, params):
        """
        CompoundSetToFile
        string compound_set_name
        string output_format
        :param params: instance of type "compoundset_download_params" ->
           structure: parameter "workspace_name" of String, parameter
           "compound_set_name" of String, parameter "output_format" of String
        :returns: instance of type "compoundset_download_results" ->
           structure: parameter "report_name" of String, parameter
           "report_ref" of String
        """
        # ctx is the context object
        # return variables are: output
        #BEGIN compound_set_to_file
        self._check_required_param(params, ['workspace_name', 'compound_set_name',
                                            'output_format'])
        compoundset = self.ws_client.get_objects2({'objects': [
            {'workspace': params['workspace_name'],
             'name': params['compound_set_name']}]})['data'][0]['data']
        ext = params['output_format']
        out = "%s/%s.%s" % (self.scratch, compoundset['name'], ext)
        if ext == 'sdf':
            outfile_path = parse.write_sdf(compoundset, out)
        elif ext == 'tsv':
            outfile_path = parse.write_tsv(compoundset, out)
        else:
            raise ValueError('Invalid output file type. Expects tsv or sdf')

        report_files = [{'path': outfile_path,
                         'name': os.path.basename(outfile_path),
                         'label': os.path.basename(outfile_path),
                         'description': 'A compound set in %s format' % ext}]

        report_params = {
            'objects_created': [],
            'message': 'Converted %s compound set to %s format.' % (
                params['compound_set_name'], params['output_format']),
            'file_links': report_files,
            'workspace_name': params['workspace_name'],
            'report_object_name': 'compound_set_download_report'
        }

        # Construct the output to send back
        report_client = KBaseReport(self.callback_url)
        report_info = report_client.create_extended_report(report_params)
        output = {'report_name': report_info['name'],
                  'report_ref': report_info['ref'],
                  }
        #END compound_set_to_file

        # At some point might do deeper type checking...
        if not isinstance(output, dict):
            raise ValueError('Method compound_set_to_file return value ' +
                             'output is not type dict as required.')
        # return the results
        return [output]
Exemple #3
0
 def _export_compound_set(self, ref, file_type):
     logging.info("Exporting {} as {}".format(ref, file_type))
     compoundset = self.dfu.get_objects({'object_refs':
                                         [ref]})['data'][0]['data']
     temp_dir = "{}/{}".format(self.scratch, uuid.uuid4())
     os.mkdir(temp_dir)
     out_dir = "{}/{}".format(temp_dir, compoundset['name'])
     os.mkdir(out_dir)
     target = "{}/{}.{}".format(out_dir, compoundset['name'], file_type)
     if file_type == 'tsv':
         parse.write_tsv(compoundset, target)
     elif file_type == 'sdf':
         parse.write_sdf(compoundset, target)
     else:
         raise ValueError("Bad file_type: {}".format(file_type))
     handle = self.dfu.package_for_download({
         'file_path': out_dir,
         'ws_refs': [ref]
     })
     output = {'shock_id': handle['shock_id']}
     return output
Exemple #4
0
    def compound_set_to_file(self, ctx, params):
        """
        CompoundSetToFile
        string compound_set_name
        string output_format
        :param params: instance of type "compoundset_download_params" ->
           structure: parameter "compound_set_ref" of String, parameter
           "output_format" of String
        :returns: instance of type "compoundset_download_results" ->
           structure: parameter "file_path" of String, parameter
           "packed_mol2_files_path" of String, parameter
           "comp_id_mol2_file_name_map" of mapping from String to String
        """
        # ctx is the context object
        # return variables are: output
        #BEGIN compound_set_to_file
        self._check_param(params, ['compound_set_ref', 'output_format'])
        ret = self.dfu.get_objects(
            {'object_refs': [params['compound_set_ref']]})['data'][0]
        compoundset = ret['data']
        ext = params['output_format']
        out = f"{self.scratch}/{uuid.uuid4()}"
        os.mkdir(out)
        out += f"/{compoundset['name']}"
        if ext == 'sdf':
            outfile_path = parse.write_sdf(compoundset, out)
        elif ext == 'tsv':
            outfile_path = parse.write_tsv(compoundset, out)
        else:
            outfile_path = parse.write_mol_dir(compoundset, out, ext)

        packed_mol2_files_path, comp_id_mol2_file_name_map = self._fetch_mol2_files(
            params['compound_set_ref'])

        output = {
            'file_path': outfile_path,
            'packed_mol2_files_path': packed_mol2_files_path,
            'comp_id_mol2_file_name_map': comp_id_mol2_file_name_map
        }

        #END compound_set_to_file

        # At some point might do deeper type checking...
        if not isinstance(output, dict):
            raise ValueError('Method compound_set_to_file return value ' +
                             'output is not type dict as required.')
        # return the results
        return [output]