class ImportReadsUtil:
    def __init__(self, config):
        self.uploader_utils = UploaderUtil(config)
        self.sra_importer = ImportSRAUtil(config)

    def import_reads_from_staging(self, params):
        self._validate_import_reads_from_staging_params(params)

        if params.get('import_type') == 'FASTQ/FASTA':
            fastq_importer_params = params
            fastq_importer_params['fwd_staging_file_name'] = params.get(
                'fastq_fwd_staging_file_name')
            fastq_importer_params['rev_staging_file_name'] = params.get(
                'fastq_rev_staging_file_name')

            returnVal = self.uploader_utils.upload_fastq_file(
                fastq_importer_params)

            uploaded_file = params.get('fastq_fwd_staging_file_name')
            if params.get('fastq_rev_staging_file_name') is not None:
                uploaded_file += '\n' + params.get(
                    'fastq_rev_staging_file_name')
            fastq_importer_params['uploaded_files'] = [uploaded_file]
            """
            Update the workspace object related meta-data for staged file
            """
            self.uploader_utils.update_staging_service(
                params.get('fastq_fwd_staging_file_name'),
                returnVal['obj_ref'])

            if params.get('fastq_rev_staging_file_name') is not None:
                self.uploader_utils.update_staging_service(
                    params.get('fastq_rev_staging_file_name'),
                    returnVal['obj_ref'])

            reportVal = self.sra_importer.generate_report(
                [returnVal['obj_ref']], fastq_importer_params)
            returnVal.update(reportVal)

        elif params.get('import_type') == 'SRA':
            sra_importer_params = params
            sra_importer_params['staging_file_subdir_path'] = params.get(
                'sra_staging_file_name')

            returnVal = self.sra_importer.import_sra_from_staging(
                sra_importer_params)

            sra_importer_params['uploaded_files'] = [
                params.get('sra_staging_file_name')
            ]
            """
            Update the workspace object related meta-data for staged file
            """
            self.uploader_utils.update_staging_service(
                params.get('sra_staging_file_name'), returnVal['obj_ref'])

            reportVal = self.sra_importer.generate_report(
                [returnVal['obj_ref']], sra_importer_params)
            returnVal.update(reportVal)

        return returnVal

    def _validate_import_reads_from_staging_params(self, params):
        """
        _validate_import_reads_from_staging_params:
                    validates params passed to import_reads_from_staging method

        """

        # check for required parameters
        for p in ['import_type', 'sequencing_tech', 'name', 'workspace_name']:
            if p not in params:
                raise ValueError(
                    '"{}" parameter is required, but missing'.format(p))

        valide_import_type = ['FASTQ/FASTA', 'SRA']
        if params.get('import_type') not in valide_import_type:
            error_msg = 'Import file type [{}] is not supported. '.format(
                params.get('import_type'))
            error_msg += 'Please selet one of {}'.format(valide_import_type)
            raise ValueError(error_msg)

        if (params.get('import_type') == 'FASTQ/FASTA'
                and not params.get('fastq_fwd_staging_file_name')):
            error_msg = 'FASTQ/FASTA input file type selected. But missing FASTQ/FASTA file.'
            raise ValueError(error_msg)

        if (params.get('import_type') == 'SRA'
                and not params.get('sra_staging_file_name')):
            error_msg = 'SRA input file type selected. But missing SRA file.'
            raise ValueError(error_msg)

        if ((params.get('fastq_fwd_staging_file_name')
             and params.get('sra_staging_file_name'))
                or (params.get('fastq_rev_staging_file_name')
                    and params.get('sra_staging_file_name'))):
            error_msg = 'Both SRA and FASTQ/FASTA file given. Please provide one file type only.'
            raise ValueError(error_msg)
Exemple #2
0
    def upload_fastq_file(self, ctx, params):
        """
        :param params: instance of type "UploadMethodParams"
           (sequencing_tech: sequencing technology name: output reads file
           name workspace_name: workspace name/ID of the object For files in
           user's staging area: fwd_staging_file_name: single-end fastq file
           name or forward/left paired-end fastq file name from user's
           staging area rev_staging_file_name: reverse/right paired-end fastq
           file name user's staging area For files from web: download_type:
           download type for web source fastq file ('Direct Download', 'FTP',
           'DropBox', 'Google Drive') fwd_file_url: single-end fastq file URL
           or forward/left paired-end fastq file URL rev_file_url:
           reverse/right paired-end fastq file URL urls_to_add: used for
           parameter-groups. dict of {fwd_file_url, rev_file_url, name,
           single_genome, interleaved, insert_size_mean and
           read_orientation_outward} Optional Params: single_genome: whether
           the reads are from a single genome or a metagenome. interleaved:
           whether reads is interleaved insert_size_mean: mean (average)
           insert length insert_size_std_dev: standard deviation of insert
           lengths read_orientation_outward: whether reads in a pair point
           outward) -> structure: parameter "workspace_name" of type
           "workspace_name" (workspace name of the object), parameter
           "fwd_staging_file_name" of type "fwd_staging_file_name" (input and
           output file path/url), parameter "rev_staging_file_name" of type
           "rev_staging_file_name", parameter "download_type" of type
           "download_type", parameter "fwd_file_url" of type "fwd_file_url",
           parameter "rev_file_url" of type "rev_file_url", parameter
           "sequencing_tech" of type "sequencing_tech", parameter "name" of
           type "name", parameter "urls_to_add" of type "urls_to_add" ->
           structure: parameter "fwd_file_url" of type "fwd_file_url",
           parameter "rev_file_url" of type "rev_file_url", parameter "name"
           of type "name", parameter "single_genome" of type "single_genome",
           parameter "interleaved" of type "interleaved", parameter
           "insert_size_mean" of type "insert_size_mean", parameter
           "insert_size_std_dev" of type "insert_size_std_dev", parameter
           "read_orientation_outward" of type "read_orientation_outward",
           parameter "single_genome" of type "single_genome", parameter
           "interleaved" of type "interleaved", parameter "insert_size_mean"
           of type "insert_size_mean", parameter "insert_size_std_dev" of
           type "insert_size_std_dev", parameter "read_orientation_outward"
           of type "read_orientation_outward"
        :returns: instance of type "UploadMethodResult" -> structure:
           parameter "obj_ref" of type "obj_ref", parameter "report_name" of
           type "report_name", parameter "report_ref" of type "report_ref"
        """
        # ctx is the context object
        # return variables are: returnVal
        #BEGIN upload_fastq_file
        print '--->\nRunning uploadmethods.upload_fastq_file\nparams:'
        print json.dumps(params, indent=1)

        if params.get('urls_to_add'):
            returnVal = {'obj_ref': ''}
            for params_item in params.get('urls_to_add'):
                params_item['workspace_name'] = params.get('workspace_name')
                params_item['download_type'] = params.get('download_type')
                params_item['sequencing_tech'] = params.get('sequencing_tech')
                params_item['interleaved'] = params.get('interleaved')
                for key, value in params_item.iteritems():
                    if isinstance(value, basestring):
                        params_item[key] = value.strip()
                fastqUploader = UploaderUtil(self.config)
                itemReturnVal = fastqUploader.upload_fastq_file(params_item)
                returnVal['obj_ref'] += itemReturnVal['obj_ref'] + ','
            returnVal['obj_ref'] = returnVal['obj_ref'][:-1]
        else:
            for key, value in params.iteritems():
                if isinstance(value, basestring):
                    params[key] = value.strip()
            fastqUploader = UploaderUtil(self.config)
            returnVal = fastqUploader.upload_fastq_file(params)

        reportVal = fastqUploader.generate_report(returnVal['obj_ref'], params)
        returnVal.update(reportVal)
        #END upload_fastq_file

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