Exemple #1
0
def process_kmer_job():
    try:
        if skr_config.LOGIN_ENABLED and session.get('logged_in') != True:
            return redirect('/login')

        parameters = build_seekr_parameters(request)

        application.logger.debug(parameters)

        t1 = time.perf_counter()
        counts, names, comparison_counts, comparison_names, counter = _run_seekr_algorithm(
            parameters=parameters)
        t2 = time.perf_counter()
        application.logger.debug('Running the algorithm took %.3f seconds' %
                                 (t2 - t1))

        fixup_counts_warnings = fixup_counts(counts, counter)
        if comparison_counts is None:
            comparison_counts = counts
            comparison_names = names
        else:
            fixup_comparision_warnings = fixup_counts(comparison_counts,
                                                      counter)

        kmers = [
            ''.join(i)
            for i in product('AGTC', repeat=parameters['kmer_length'])
        ]

        csv_string = get_kmers_csv(counts=counts, names=names, kmers=kmers)

        last_modified = email.utils.formatdate(time.time(), usegmt=True)
        headers = {
            'Content-Type': 'application/csv',
            'Content-Disposition': 'attachment;filename = seekr.csv',
            'Content-Length': str(len(csv_string)),
            'Last-Modified': last_modified
        }
        return (csv_string, headers)

    except Exception as e:
        application.logger.exception('Error in /files/kmers')
        #TODO change error from json
        return jsonify({'error': "Server_Error: 500"})
Exemple #2
0
def process_pearsons_job():
    try:
        if skr_config.LOGIN_ENABLED and session.get('logged_in') != True:
            return redirect('/login')

        parameters = build_seekr_parameters(request)

        application.logger.debug('CURRENT METHOD: process_pearsons_job')

        t1 = time.perf_counter()
        counts, names, comparison_counts, comparison_names, counter = _run_seekr_algorithm(
            parameters=parameters)
        t2 = time.perf_counter()
        application.logger.debug('Running the algorithm took %.3f seconds' %
                                 (t2 - t1))

        fixup_counts_warnings = fixup_counts(counts, counter)
        if comparison_counts is None:
            comparison_counts = counts
            comparison_names = names
        else:
            fixup_comparision_warnings = fixup_counts(comparison_counts,
                                                      counter)

        pearsons = pearson(counts, comparison_counts)
        application.logger.debug("Finished Pearson's. Converting to .csv.")
        csv_string = get_pearsons_csv(names, pearsons, comparison_names)

        last_modified = email.utils.formatdate(time.time(), usegmt=True)
        headers = {
            'Content-Type': 'application/csv',
            'Content-Disposition': 'attachment;filename = pearsons.csv',
            'Content-Length': str(len(csv_string)),
            'Last-Modified': last_modified
        }
        return (csv_string, headers)

    except Exception as e:
        application.logger.exception('Error in /files/pearsons')
        #TODO change error from json
        return jsonify({'error': "Server_Error: 500"})
Exemple #3
0
def process_jobs():
    try:
        if skr_config.LOGIN_ENABLED and session.get('logged_in') != True:
            return redirect('/login')

        parameters = build_seekr_parameters(request)

        application.logger.debug(parameters)

        t1 = time.perf_counter()
        counts, names, comparison_counts, comparison_names, counter = _run_seekr_algorithm(
            parameters=parameters)
        t2 = time.perf_counter()
        application.logger.debug('Running the algorithm took %.3f seconds' %
                                 (t2 - t1))

        if len(names) <= skr_config.MAX_VISUAL_SEQ_LENGTH and len(
                comparison_names
        ) <= skr_config.MAX_VISUAL_SEQ_LENGTH and parameters[
                'kmer_length'] <= skr_config.MAX_VISUAL_KMER_LENGTH:

            fixup_counts_warnings = fixup_counts(counts, counter)
            if comparison_counts is None:
                comparison_counts = counts
                comparison_names = names
            else:
                fixup_comparision_warnings = fixup_counts(
                    comparison_counts, counter)

            #reorder according to hierarchical cluster example
            if len(counts) > 1:
                Z = cluster_vis.cluster_kmers(counts)
                ordering = cluster_vis.get_ordering(Z)
                ordered_counts = counts[ordering, :]
                ordering_int_list = ordering.astype(int).tolist()
                ordered_names = [names[i] for i in ordering_int_list]
            else:
                ordered_counts = counts
                ordering_int_list = [0]
                ordered_names = names

            if len(comparison_counts) > 1:
                comparison_Z = cluster_vis.cluster_kmers(comparison_counts)
                comparison_ordering = cluster_vis.get_ordering(comparison_Z)
                comparison_ordered_counts = comparison_counts[
                    comparison_ordering, :]
                comparison_ordering_int_list = comparison_ordering.astype(
                    int).tolist()
                comparison_ordered_names = [
                    comparison_names[i] for i in comparison_ordering_int_list
                ]
            else:
                comparison_ordered_counts = comparison_counts
                comparison_ordering_int_list = [0]
                comparison_ordered_names = comparison_names

            pearsons = pearson(counts, comparison_counts)

            # shorten length of names returned down to 20 characters
            new_names = []
            for s in names:
                if len(s) > skr_config.SEQUENCE_NAME_DISPLAY_LENGTH:
                    new_names.append(
                        s[:skr_config.SEQUENCE_NAME_DISPLAY_LENGTH])
                else:
                    new_names.append(s)

            names = new_names

            new_names = []
            for s in comparison_names:
                if len(s) > skr_config.SEQUENCE_NAME_DISPLAY_LENGTH:
                    new_names.append(
                        s[:skr_config.SEQUENCE_NAME_DISPLAY_LENGTH])
                else:
                    new_names.append(s)

            comparison_names = new_names

            kmers = [
                ''.join(i)
                for i in product('AGTC', repeat=parameters['kmer_length'])
            ]

            norm_npm = counts
            scale_npm = norm_npm.flatten()
            mean = np.mean(scale_npm)
            z_npm = stats.zscore(scale_npm)
            count = 0
            for i in z_npm:
                if i >= 2:
                    scale_npm[count] = 2
                elif i < -1:
                    scale_npm[count] = -1
                count = count + 1
            clean_counts = np.reshape(scale_npm, np.shape(norm_npm))

            pearsons = pearsons.round(3)
            counts = counts.round(3)
            clean_counts = clean_counts.round(3)

            pearsons = str(pearsons.tolist())
            counts = str(counts.tolist())
            clean_counts = str(clean_counts.tolist())

            return jsonify({
                'user_names': names,
                'comparison_names': comparison_names,
                'kmer_bins': kmers,
                'pearson_matrix': pearsons,
                'kmer_matrix': counts,
                'kmer_matrix_clean': clean_counts,
                'user_cluster': ordering_int_list,
                'comparison_cluster': comparison_ordering_int_list,
                'user_warnings': fixup_counts_warnings,
                'comparison_warnings': fixup_comparision_warnings
            })

        else:
            return jsonify({'visual_flag': True})

    except SeekrServerError as e:
        application.logger.exception(e)
        return jsonify({'error': str(e)})

    except Exception as e:
        application.logger.exception(e)
        return jsonify({'error': '500'})