Example #1
0
    def _input_as_parameter(self, data):
        """ Set the input path and log path based on data (a fasta filepath)
        """
        ## temporary hack: this converts a biom file to classic otu table
        ##  format for use within R
        if self.Parameters["-v"].Value:
            print "Converting BIOM format to tab-delimited..."
        temp_predictor_fp = join(self.Parameters["-o"].Value, splitext(split(data)[1])[0] + ".txt")
        temp_predictor_f = open(temp_predictor_fp, "w")
        temp_predictor_f.write(convert_biom_to_table(open(data, "U")))
        temp_predictor_f.close()
        predictor_fp = temp_predictor_fp

        self.Parameters["-i"].on(predictor_fp)
        # access data through self.Parameters so we know it's been cast
        # to a FilePath
        return ""
    def _input_as_parameter(self, data):
        """ Set the input path and log path based on data (a fasta filepath)
        """
        # temporary hack: this converts a biom file to classic otu table
        # format for use within R
        if self.Parameters['-v'].Value:
            print 'Converting BIOM format to tab-delimited...'
        temp_predictor_fp = join(self.Parameters['-o'].Value,
                                 splitext(split(data)[1])[0] + '.txt')
        temp_predictor_f = open(temp_predictor_fp, 'w')
        temp_predictor_f.write(convert_biom_to_table(data))
        temp_predictor_f.close()
        predictor_fp = temp_predictor_fp

        self.Parameters['-i'].on(predictor_fp)
        # access data through self.Parameters so we know it's been cast
        # to a FilePath
        return ''
Example #3
0
    def run(self, **kwargs):
        table_file = kwargs['table_file']
        matrix_type = kwargs['matrix_type']
        biom_to_classic_table = kwargs['biom_to_classic_table']
        sparse_biom_to_dense_biom = kwargs['sparse_biom_to_dense_biom']
        dense_biom_to_sparse_biom = kwargs['dense_biom_to_sparse_biom']
        sample_metadata = kwargs['sample_metadata']
        observation_metadata = kwargs['observation_metadata']
        header_key = kwargs['header_key']
        output_metadata_id = kwargs['output_metadata_id']
        process_obs_metadata = kwargs['process_obs_metadata']
        table_type = kwargs['table_type']

        if sum([biom_to_classic_table, sparse_biom_to_dense_biom,
                dense_biom_to_sparse_biom]) > 1:
            raise CommandError("Converting between classic/BIOM formats and "
                               "sparse/dense representations are mutually "
                               "exclusive. You may only specify a single "
                               "operation at a time.")

        # if the user does not specify a name for the output metadata column,
        # set it to the same as the header key
        output_metadata_id = output_metadata_id or header_key

        convert_error_msg = ("Input does not look like a BIOM-formatted file. "
                             "Did you accidentally specify that a classic "
                             "table file should be created from a BIOM table "
                             "file?")
        if biom_to_classic_table:
            try:
                result = convert_biom_to_table(table_file, header_key,
                                               output_metadata_id)
            except ValueError:
                raise CommandError(convert_error_msg)
        elif sparse_biom_to_dense_biom:
            try:
                table = parse_biom_table(table_file)
            except ValueError:
                raise CommandError(convert_error_msg)

            conv_constructor = self.TableTypes[table._biom_type.lower()][1]
            conv_table = table_factory(table._data, table.SampleIds,
                            table.ObservationIds, table.SampleMetadata,
                            table.ObservationMetadata, table.TableId,
                            constructor=conv_constructor)
            result = conv_table.getBiomFormatJsonString(generatedby())
        elif dense_biom_to_sparse_biom:
            try:
                table = parse_biom_table(table_file)
            except ValueError:
                raise CommandError(convert_error_msg)

            conv_constructor = self.TableTypes[table._biom_type.lower()][0]
            conv_table = table_factory(table._data, table.SampleIds, 
                            table.ObservationIds, table.SampleMetadata, 
                            table.ObservationMetadata, table.TableId, 
                            constructor=conv_constructor)
            result = conv_table.getBiomFormatJsonString(generatedby())
        else:
            if table_type is None:
                raise CommandError("Must specify the BIOM table type: %s" %
                                   ', '.join(self.TableTypes.keys()))
            else:
                table_type = table_type.lower()

            if table_type not in self.TableTypes:
                raise CommandError("Unknown BIOM table type, must be one of: "
                                   "%s" % ', '.join(self.TableTypes.keys()))

            if matrix_type not in self.MatrixTypes:
                raise CommandError("Unknown BIOM matrix type, must be one of: "
                                   "%s" % ', '.join(self.MatrixTypes))

            if process_obs_metadata not in \
                    self.ObservationMetadataTypes.keys():
                raise CommandError("Unknown observation metadata processing "
                        "method, must be one of: %s" %
                        ', '.join(self.ObservationMetadataTypes.keys()))

            idx = 0 if matrix_type == 'sparse' else 1
            constructor = self.TableTypes[table_type][idx]


            convert_error_msg = ("Input does not look like a classic table. "
                                 "Did you forget to specify that a classic "
                                 "table file should be created from a BIOM "
                                 "table file?")
            try:
                result = convert_table_to_biom(table_file, sample_metadata,
                        observation_metadata,
                        self.ObservationMetadataTypes[process_obs_metadata],
                        constructor)
            except ValueError:
                raise CommandError(convert_error_msg)
            except IndexError:
                raise CommandError(convert_error_msg)

        return {'table_str': result}
Example #4
0
    def run(self, **kwargs):
        table_file = kwargs["table_file"]
        biom_to_classic_table = kwargs["biom_to_classic_table"]
        sparse_biom_to_dense_biom = kwargs["sparse_biom_to_dense_biom"]
        dense_biom_to_sparse_biom = kwargs["dense_biom_to_sparse_biom"]
        sample_metadata = kwargs["sample_metadata"]
        observation_metadata = kwargs["observation_metadata"]
        header_key = kwargs["header_key"]
        output_metadata_id = kwargs["output_metadata_id"]
        process_obs_metadata = kwargs["process_obs_metadata"]

        if sum([biom_to_classic_table, sparse_biom_to_dense_biom, dense_biom_to_sparse_biom]) > 1:
            raise CommandError(
                "Converting between classic/BIOM formats and "
                "sparse/dense representations are mutually "
                "exclusive. You may only specify a single "
                "operation at a time."
            )

        # if the user does not specify a name for the output metadata column,
        # set it to the same as the header key
        output_metadata_id = output_metadata_id or header_key

        convert_error_msg = (
            "Input does not look like a BIOM-formatted file. "
            "Did you accidentally specify that a classic "
            "table file should be created from a BIOM table "
            "file?"
        )
        if biom_to_classic_table:
            try:
                result = convert_biom_to_table(table_file, header_key, output_metadata_id)
            except (ValueError, TypeError):
                raise CommandError(convert_error_msg)
        elif sparse_biom_to_dense_biom:
            try:
                table = parse_biom_table(table_file)
            except (ValueError, TypeError):
                raise CommandError(convert_error_msg)

            conv_table = table_factory(
                table._data,
                table.sample_ids,
                table.observation_ids,
                table.sample_metadata,
                table.observation_metadata,
                table.TableId,
            )
            result = conv_table.get_biom_format_json_string(generatedby())
        elif dense_biom_to_sparse_biom:
            try:
                table = parse_biom_table(table_file)
            except (ValueError, TypeError):
                raise CommandError(convert_error_msg)

            conv_table = table_factory(
                table._data,
                table.sample_ids,
                table.observation_ids,
                table.sample_metadata,
                table.observation_metadata,
                table.table_id,
            )
            result = conv_table.get_biom_format_json_string(generatedby())
        else:
            if process_obs_metadata not in self.ObservationMetadataTypes.keys():
                raise CommandError(
                    "Unknown observation metadata processing method, must be "
                    "one of: %s" % ", ".join(self.ObservationMetadataTypes.keys())
                )

            convert_error_msg = (
                "Input does not look like a classic table. "
                "Did you forget to specify that a classic "
                "table file should be created from a BIOM "
                "table file?"
            )
            try:
                result = convert_table_to_biom(
                    table_file,
                    sample_metadata,
                    observation_metadata,
                    self.ObservationMetadataTypes[process_obs_metadata],
                )
            except (ValueError, TypeError, IndexError):
                raise CommandError(convert_error_msg)

        return {"table_str": result}
Example #5
0
def main():
    opts,args = parser.parse_args()

    if opts.input_fp is None:
        parser.print_help()
        parser.error('Must specify an input file!')
    if opts.output_fp is None:
        parser.print_help()
        parser.error('Must specify an output file!')

    biom_to_classic_table = opts.biom_to_classic_table
    sparse_biom_to_dense_biom = opts.sparse_biom_to_dense_biom
    dense_biom_to_sparse_biom = opts.dense_biom_to_sparse_biom
    process_obs_metadata = opts.process_obs_metadata
    
    if sum([biom_to_classic_table,
            sparse_biom_to_dense_biom,
            dense_biom_to_sparse_biom]) > 1:
        parser.print_help()
        option_parser.error("The --biom_to_classic_table, --sparse_biom_to_dense_biom, "
         "and --dense_biom_to_sparse_biom options are mutually exclusive. Pass only one at a time.")
    
    input_f = open(opts.input_fp,'U')
    output_f = open(opts.output_fp,'w')
    
    #dense = opts.biom_type == 'dense'
    count_map_f = int
    sample_mapping_fp = opts.sample_mapping_fp
    obs_mapping_fp = opts.observation_mapping_fp
    
    if sample_mapping_fp != None:
        sample_mapping = parse_mapping(open(sample_mapping_fp,'U'))
    else:
        sample_mapping = None
    
    if obs_mapping_fp != None:
        obs_mapping = parse_mapping(open(obs_mapping_fp, 'U'))
    else:
        obs_mapping = None
    
    # if the user does not specify a name for the output metadata column, set it to the
    # same as the header key
    header_key = opts.header_key
    output_metadata_id = opts.output_metadata_id or header_key
    if biom_to_classic_table:
        try:
            output_f.write(convert_biom_to_table(\
                           input_f, header_key, output_metadata_id))
        except ValueError:
            raise ValueError, "Input does not look like a .biom file. Did you accidentally specify -b?"
    elif sparse_biom_to_dense_biom:
        try:
            table = parse_biom_table(input_f)
        except ValueError:
            raise ValueError, "Input does not look like a .biom file. Did you accidentally specify -b?"        

        conv_constructor = BIOM_TYPES[table._biom_type.lower()][1]
        conv_table = table_factory(table._data, table.SampleIds, 
                        table.ObservationIds, table.SampleMetadata, 
                        table.ObservationMetadata, table.TableId, 
                        constructor=conv_constructor)
        output_f.write(conv_table.getBiomFormatJsonString(generatedby()))
    elif dense_biom_to_sparse_biom:
        try:
            table = parse_biom_table(input_f)
        except ValueError:
            raise ValueError, "Input does not look like a .biom file. Did you accidentally specify -b?"

        conv_constructor = BIOM_TYPES[table._biom_type.lower()][0]
        conv_table = table_factory(table._data, table.SampleIds, 
                        table.ObservationIds, table.SampleMetadata, 
                        table.ObservationMetadata, table.TableId, 
                        constructor=conv_constructor)

        output_f.write(conv_table.getBiomFormatJsonString(generatedby()))
    else:
        if opts.biom_table_type is None:
            parser.error('Must specify the BIOM table type: %s' % \
                    ', '.join(BIOM_TYPES.keys()))
        else:
            biom_table_type = opts.biom_table_type.lower()
        if biom_table_type not in BIOM_TYPES:
            parser.error('Unknown BIOM table type, must be one of: %s' % \
                    ', '.join(BIOM_TYPES.keys()))
        if opts.biom_type is None or opts.biom_type not in ['dense', 'sparse']:
            parser.error('Must specify the BIOM matrix type, ' + \
                    'either "dense" or "sparse"')

        idx = 0 if opts.biom_type == 'sparse' else 1
        constructor = BIOM_TYPES[biom_table_type][idx]
        
        try:
            output_f.write(convert_table_to_biom(input_f,sample_mapping, 
                                obs_mapping, OBS_META_TYPES[process_obs_metadata], constructor))
        except ValueError:
            raise ValueError, "Input does not look like a classic table. Do you need to pass -b?"
    input_f.close()
    output_f.close()
    def __call__(self, predictor_fp, confidence, support, sort = "lift", taxonomy = "no", output_dir=None, verbose=False):
        """Run the application with the specified kwargs on data

            remove_tmp: if True, removes tmp files
            
            returns a CommandLineAppResult object
        """
        suppress_stdout = self.SuppressStdout
        suppress_stderr = self.SuppressStderr
        if suppress_stdout:
            outfile = devnull
        else:
            outfilepath = FilePath(self.getTmpFilename(self.TmpDir))
            outfile = open(outfilepath,'w')
        if suppress_stderr:
            errfile = devnull
        else:
            errfilepath = FilePath(self.getTmpFilename(self.TmpDir))
            errfile = open(errfilepath, 'w')
        if output_dir is None:
            output_dir = mkdtemp(prefix='R_output_')
        
        ## temporary hack: this converts a biom file to classic otu table
        ##  format for use within R
        if verbose:
            print 'Converting BIOM format to tab-delimited...'

        temp_predictor_fp = join(output_dir,
                                 splitext(split(predictor_fp)[1])[0]+'.txt')
        temp_predictor_f = open(temp_predictor_fp,'w')
        if taxonomy == "no":
	    temp_predictor_f.write(convert_biom_to_table(open(predictor_fp,'U')))
        else:
            temp_predictor_f.write(taxonomy_biom_to_table(open(predictor_fp, 'U'), 'taxonomy', 'taxonomy'))
        temp_predictor_f.close()
        predictor_fp = temp_predictor_fp

        rflags = self.RParameters['flags']
        rscript = self._get_R_script_path()
        base_command = self._get_base_command()
        cd_command, base_command = base_command.split(';')
        cd_command += ';'
        source_dir = self._get_R_script_dir()
        
        # Build up the command, consisting of a BaseCommand followed byp

        # input and output (file) specifications
        args = ['-i', predictor_fp, 
		'-c', confidence,
		'-s', support,
		'-o', sort,
		'--source_dir', source_dir]
        if verbose:
                args += ['-v']

        command = self._commandline_join(
            [   cd_command, base_command,
                '--args'
            ] + args + [' < %s ' %(rscript)]
            ) 
	print "Command: ", command
        if self.HaltExec: 
            raise AssertionError, "Halted exec with command:\n" + command

        # run command, wait for output, get exit status
        proc = subprocess.Popen(command, shell=True, stdout=outfile, stderr=errfile)

        if verbose:
            print '\nR output\n'
            tmpoutfile = open(outfilepath,'U')
            while proc.poll() is None:
                stdout.write(tmpoutfile.readline())
                sleep(0.00001)
            tmpoutfile.close()
        proc.wait()
        exit_status = proc.returncode

        # Determine if error should be raised due to exit status of 
        # appliciation
        if not self._accept_exit_status(exit_status):
            if exit_status == 2:
                raise ApplicationError, \
                    'R library not installed: \n' + \
                    ''.join(open(errfilepath,'r').readlines()) + '\n'
            else:
                raise ApplicationError, \
                    'Unacceptable application exit status: %s, command: %s'\
                    % (str(exit_status),command) +\
                    ' Program output: \n\n%s\n'\
                     %(''.join(open(errfilepath,'r').readlines()))