예제 #1
0
파일: allele.py 프로젝트: trmznt/genaf
def func_callback( query, user ):

    from fatools.lib.analytics.summary import summarize_alleles, plot_alleles

    analytical_sets = query.get_filtered_analytical_sets()
    report = summarize_alleles( analytical_sets )

    options={}
    fso_dir = None

    if True:
        # create plot file
        if fso_dir is None:
            fso_dir = get_fso_temp_dir(user.login)
        plotfile = fso_dir.abspath + '/' + PLOTFILE
        plot_alleles(report, plotfile, dbh=get_dbhandler())
        options['plotfile'] = fso.get_urlpath(plotfile)

    if False:
        # create tab-delimited text file
        if fso_dir is None:
            fso_dir = get_fso_temp_dir(user.login)

        tabfile = fso_dir.abspath + '/' + TABFILE
        options['tabfile'] = fso.get_urlpath(tabfile)

    html, code = format_output(report, options)

    return {    'custom': None,
                'options': options,
                'title': 'Allele Summary Result',
                'html': html,
                'jscode': code,
    }
예제 #2
0
파일: nj.py 프로젝트: trmznt/genaf-snp
def do_nj(query, userinstance, ns, *args, **kwargs):  #q, user, ns=None):

    dbh = get_dbhandler()
    analytical_sets = query.get_filtered_analytical_sets()

    dm = dist.get_distance_matrix(analytical_sets)

    dbh = query.dbh
    fso_dir = get_fso_temp_dir(userinstance.login)

    tip_label = query.specs['options'].get('tip_label', 'S')
    label_callback = {
        'S': lambda x: dbh.get_sample_by_id(x).code,
        'I': None,
        'C': lambda x: dbh.get_sample_by_id(x).location.country,
        '1': lambda x: dbh.get_sample_by_id(x).location.level1,
        '2': lambda x: dbh.get_sample_by_id(x).location.level2,
        '3': lambda x: dbh.get_sample_by_id(x).location.level3,
        '4': lambda x: dbh.get_sample_by_id(x).location.level4,
        '-': lambda x: '-',
    }

    tree_type = {
        'F': 'fan',
        'R': 'radial',
        'U': 'unrooted',
        'P': 'phylogram'
    }[query.specs['options'].get('tree_type', 'F')]
    branch_coloring = True  #query.options.get('branch_coloring', 'Y') == 'Y'

    njplot_png = plot_nj(dm,
                         fso_dir.abspath,
                         'png',
                         label_callback=label_callback[tip_label],
                         tree_type=tree_type,
                         branch_coloring=branch_coloring)
    njplot_pdf = plot_nj(dm,
                         fso_dir.abspath,
                         'pdf',
                         label_callback=label_callback[tip_label],
                         tree_type=tree_type,
                         branch_coloring=branch_coloring)

    ns.result = {
        'png_plot': fso.get_urlpath(njplot_png),
        'pdf_plot': fso.get_urlpath(njplot_pdf)
    }

    return True
예제 #3
0
파일: nj.py 프로젝트: trmznt/genaf
def func_callback( query, user ):

    from fatools.lib.analytics.dist import get_distance_matrix
    from fatools.lib.analytics.nj import plot_nj

    haplotype_sets = query.get_filtered_haplotype_sets()

    dm = get_distance_matrix(haplotype_sets)

    dbh = query.dbh
    fso_dir = get_fso_temp_dir(user.login)

    tip_label = query.options.get('tip_label', 'S')
    label_callback = {
        'S': lambda x: dbh.get_sample_by_id(x).code,
        'I': None,
        'C': lambda x: dbh.get_sample_by_id(x).location.country,
        '1': lambda x: dbh.get_sample_by_id(x).location.level1,
        '2': lambda x: dbh.get_sample_by_id(x).location.level2,
        '3': lambda x: dbh.get_sample_by_id(x).location.level3,
        '4': lambda x: dbh.get_sample_by_id(x).location.level4,
    }

    tree_type = { 'F': 'fan', 'R': 'radial', 'U': 'unrooted'}[query.options.get('tree_type', 'F')]

    njplot_png = plot_nj(dm, fso_dir.abspath, 'png',
            label_callback = label_callback[tip_label], tree_type=tree_type)
    njplot_pdf = plot_nj(dm, fso_dir.abspath, 'pdf',
            label_callback = label_callback[tip_label], tree_type=tree_type)

    options = { 'png_plot': fso.get_urlpath(njplot_png),
                'pdf_plot': fso.get_urlpath(njplot_pdf) }

    html, code = format_output( options )

    return {    'custom': None,
                'options': None,
                'title': 'Neighbor-Joining (NJ) Tree Result',
                'html': html,
                'jscode': code,
                'refs': [
                    'Paradis E., Claude J. & Strimmer K. (2004) '
                    'APE: analyses of phylogenetics and evolution in R language. '
                    '<em>Bioinformatics</em>, <strong>20</strong>: 289-290',
                    '<a href="http://ape-package.ird.fr/">APE website</a>'
                ],
    }
예제 #4
0
파일: allele.py 프로젝트: trmznt/msaf
def index(request):

    if not request.GET.get('_method', None) in ['_exec', '_yamlexec']:
        queries = get_queries()

        return render_to_response('msaf:templates/tools/allele/index.mako',
                { 'queries': queries, 'markers': get_marker_list() },
                request = request)

    if request.GET.get('_method') == '_exec':

    #parse form

        baseparams = parse_base_params( request.GET )
        spatial_differentiation = int(request.GET.get('spatial_differentiation', -1))
        temporal_differentiation = int(request.GET.get('temporal_differentiation', 0))
        sample_sets = parse_advquerycmd( baseparams.queryset )



    else:
        selector, filter, differentiation = parse_yaml_params( request.GET )
        baseparams = set_base_params(selector, filter)
        spatial_differentiation = differentiation.spatial
        temporal_differentiation = differentiation.temporal
        sample_sets = selector.get_sample_sets()


    #sample_ids = parse_querycmd( baseparams.queryset )
    #diff_analytical_sets, sample_report, marker_report = get_filtered_analytical_sets(
    #            sample_ids = sample_ids, marker_ids = baseparams.marker_ids,
    #            allele_absolute_threshold = baseparams.allele_absolute_threshold,
    #            allele_relative_threshold = baseparams.allele_relative_threshold,
    #            sample_quality_threshold = baseparams.sample_quality_threshold,
    #            marker_quality_threshold = baseparams.marker_quality_threshold,
    #            spatial_differentiation = spatial_differentiation,
    #            temporal_differentiation = temporal_differentiation)

    #analytical_set = diff_analytical_sets[0]

    diff_analytical_sets, sample_report, marker_report = get_filtered_analytical_sets2(
                sample_sets = sample_sets,
                baseparams = baseparams,
                spatial_differentiation = spatial_differentiation,
                temporal_differentiation = temporal_differentiation )


    # need to get all alleles from all peaks, including size & height

    temp_dir = fso.mkranddir( '/temps' )

    results, plot_file = summarize_alleles2( diff_analytical_sets, temp_dir.rpath )
    ploturl = fso.get_urlpath( plot_file )

    return render_to_response('msaf:templates/tools/allele/report.mako',
            {   'results': results,
                'ploturl': ploturl
            },
            request = request )
예제 #5
0
파일: pca.py 프로젝트: trmznt/genaf-snp
def do_pca(query, userinstance, ns, *args, **kwargs):  #q, user, ns=None):

    dbh = get_dbhandler()
    #df = dbh.get_allele_dataframe(None, None, None)
    #initial_sample_sets = query.get_sample_sets()
    #analytical_sets = query.get_analytical_sets()
    analytical_sets = query.get_filtered_analytical_sets()
    #variant_df = pd.pivot_table( df, index = 'sample_id', columns = 'locus_id', values='call',
    #            aggfunc = lambda x: x )
    #haplotype_sets = query.get_filtered_haplotype_sets()

    dimension = 3
    dm = dist.get_distance_matrix(analytical_sets)
    pca_res = ca.pcoa(dm, dim=dimension)

    fso_dir = get_fso_temp_dir(userinstance.login)
    plotfile_urls = []

    for (ax, ay) in combinations(range(dimension), 2):
        plotfile = fso_dir.abspath + '/' + 'pcoa-%d-%d' % (ax, ay)
        plot_png = ca.plot_pca(pca_res, dm, ax, ay, plotfile + '.png',
                               query.specs['options']['symbol'],
                               query.specs['options']['symbol_size'],
                               query.specs['options']['symbol_alpha'])
        plot_pdf = ca.plot_pca(pca_res, dm, ax, ay, plotfile + '.pdf',
                               query.specs['options']['symbol'],
                               query.specs['options']['symbol_size'],
                               query.specs['options']['symbol_alpha'])
        plotfile_urls.append(
            (fso.get_urlpath(plot_png), fso.get_urlpath(plot_pdf)))

    pca_data = ca.format_data(pca_res, dm)
    data_file = fso_dir.abspath + '/' + 'pcoa-data.txt'
    with open(data_file, 'w') as outfile:
        for r in pca_data:
            outfile.write('\t'.join(r))
            outfile.write('\n')

    ns.result = {
        'plotfile_urls': plotfile_urls,
        'data_file': fso.get_urlpath(data_file)
    }

    return True
예제 #6
0
파일: mca.py 프로젝트: trmznt/genaf
def func_callback( query, user ):

    from fatools.lib.analytics.dist import get_distance_matrix, null_distance
    from fatools.lib.analytics.ca import mca, plot_pca, format_data

    dimension = 2

    haplotype_sets = query.get_filtered_haplotype_sets()
    dm = get_distance_matrix(haplotype_sets, null_distance)
    mca_res = mca(dm)

    fso_dir = get_fso_temp_dir(user.login)
    plotfile_urls = []

    for (ax, ay) in combinations(range( dimension ), 2):
        plotfile = fso_dir.abspath + '/' + 'pcoa-%d-%d' % (ax, ay)
        plot_png = plot_pca(mca_res, dm, ax, ay, plotfile + '.png')
        plot_pdf = plot_pca(mca_res, dm, ax, ay, plotfile + '.pdf')
        plotfile_urls.append( (fso.get_urlpath(plot_png), fso.get_urlpath(plot_pdf)) )

    mca_data = format_data(mca_res, dm)
    data_file = fso_dir.abspath + '/' + 'mca-data.txt'
    with open(data_file, 'w') as outfile:
        for r in mca_data:
            outfile.write( '\t'.join( r ) )
            outfile.write( '\n' )

    options = { 'plotfile_urls': plotfile_urls, 'data_file': fso.get_urlpath(data_file) }

    html, code = format_output( (mca_res, dm), options )

    return {    'custom': None,
                'options': None,
                'title': 'Multiple Correspondence Analysis (MCA) Result',
                'html': html,
                'jscode': code,
                'refs': [
                    'L&ecirc;, S., Josse, J. &amp; Husson, F. (2008). '
                    'FactoMineR: An R Package for Multivariate Analysis. '
                    '<em>Journal of Statistical Software</em>. <strong>25(1)</strong>. pp. 1-18.',
                    '<a href="http://factominer.free.fr/">FactoMineR website</a>'
                ],
    }
예제 #7
0
파일: pcoa.py 프로젝트: trmznt/genaf
def func_callback( query, user ):

    from fatools.lib.analytics.dist import get_distance_matrix
    from fatools.lib.analytics.ca import pcoa, plot_pca, format_data

    dimension = 2

    haplotype_sets = query.get_filtered_haplotype_sets()

    dm = get_distance_matrix(haplotype_sets)
    pca_res = pcoa(dm, dim = dimension)

    fso_dir = get_fso_temp_dir(user.login)
    plotfile_urls = []

    for (ax, ay) in combinations(range( dimension ), 2):
        plotfile = fso_dir.abspath + '/' + 'pcoa-%d-%d' % (ax, ay)
        plot_png = plot_pca(pca_res, dm, ax, ay, plotfile + '.png')
        plot_pdf = plot_pca(pca_res, dm, ax, ay, plotfile + '.pdf')
        plotfile_urls.append( (fso.get_urlpath(plot_png), fso.get_urlpath(plot_pdf)) )

    pca_data = format_data(pca_res, dm)
    data_file = fso_dir.abspath + '/' + 'pcoa-data.txt'
    with open(data_file, 'w') as outfile:
        for r in pca_data:
            outfile.write( '\t'.join( r ) )
            outfile.write( '\n' )

    options = { 'plotfile_urls': plotfile_urls, 'data_file': fso.get_urlpath(data_file) }

    html, code = format_output( (pca_res, dm), options )

    return {    'custom': None,
                'options': None,
                'title': 'Principal Coordinate Analysis (PCoA) Result',
                'html': html,
                'jscode': code,
    }

    return ('Principal Coordinate Analysis (PCoA) Result', html, code)
예제 #8
0
파일: djost.py 프로젝트: trmznt/genaf
def format_output( djost ):

    body = div()

    body.add( h4('D-Jost') )
    body.add( create_table( djost['M'] ) )

    if djost['msg']:
        body.add(br(), b('Warning: '), djost['msg'])

    body.add(
        br(),
        a('Data download link', href=fso.get_urlpath( djost['data_file'] )),
    )

    return (body, '')
예제 #9
0
파일: haplotype.py 프로젝트: trmznt/msaf
def index(request):

    if not request.GET.get('_method', None) == '_exec':
        queries = get_queries()

        return render_to_response('msaf:templates/tools/haplotype/index.mako',
                { 'queries': queries, 'markers': get_marker_list() },
                request = request)


    #parse form

    baseparams = parse_base_params( request.GET )
    spatial_differentiation = int(request.GET.get('spatial_differentiation', 4))
    temporal_differentiation = int(request.GET.get('temporal_differentiation', 0))


    sample_ids = parse_querycmd( baseparams.queryset )
    diff_analytical_sets, sample_report, marker_report = get_filtered_analytical_sets(
                sample_ids = sample_ids, marker_ids = baseparams.marker_ids,
                allele_absolute_threshold = baseparams.allele_absolute_threshold,
                allele_relative_threshold = baseparams.allele_relative_threshold,
                allele_relative_cutoff = baseparams.allele_relative_cutoff,
                sample_quality_threshold = baseparams.sample_quality_threshold,
                marker_quality_threshold = baseparams.marker_quality_threshold,
                spatial_differentiation = spatial_differentiation,
                temporal_differentiation = temporal_differentiation )

    (unique_haplotype, haplotype_freqs, total_freqs, haplotype_df) = summarize_haplotypes(
                                diff_analytical_sets )

    # create the histogram

    temp_dir = fso.mkranddir('/temps')
    plot_file = plot_haplotype( haplotype_df, temp_dir.rpath, 'haplotypes_stacked.png' )
    vpath_plot = fso.get_urlpath( plot_file )

    return render_to_response('msaf:templates/tools/haplotype/report.mako',
            {   'unique_haplotype': unique_haplotype,
                'haplotype_freqs': haplotype_freqs,
                'vpath_plot': vpath_plot },
            request = request )
예제 #10
0
파일: structure.py 프로젝트: trmznt/msaf
def index(request):

    if not request.GET.get('_method', None) == '_exec':
        queries = get_queries()

        return render_to_response('msaf:templates/tools/structure/index.mako',
                { 'queries': queries, 'markers': get_marker_list() },
                request = request)


    #parse form

    baseparams = parse_base_params( request.GET )
    spatial_differentiation = int(request.GET.get('spatial_differentiation', 4))
    temporal_differentiation = int(request.GET.get('temporal_differentiation', 0))


    # filter samples

    sample_ids = parse_querycmd( baseparams.queryset )
    sample_sets, sample_df = group_samples( sample_ids,
                                        spatial_differentiation = spatial_differentiation,
                                        temporal_differentiation = temporal_differentiation )

    base_analytical_sets = create_analytical_sets( sample_sets,
                            marker_ids = baseparams.marker_ids,
                            allele_absolute_threshold = baseparams.allele_absolute_threshold,
                            allele_relative_threshold = baseparams.allele_relative_threshold )

    sample_report, filtered_analytical_sets = assess_sample_quality( base_analytical_sets,
                            sample_quality_threshold = baseparams.sample_quality_threshold )

    marker_report, filtered_marker_ids = assess_marker_quality( filtered_analytical_sets,
                            marker_quality_threshold = baseparams.marker_quality_threshold )

    diff_sample_sets, diff_sample_df = group_samples( get_sample_ids(filtered_analytical_sets),
                            spatial_differentiation = spatial_differentiation,
                            temporal_differentiation = temporal_differentiation )

    diff_analytical_sets = create_analytical_sets( diff_sample_sets,
                            marker_ids = filtered_marker_ids,
                            allele_absolute_threshold = baseparams.allele_absolute_threshold,
                            allele_relative_threshold = baseparams.allele_relative_threshold )


    # start analysis here

    (databuf, parambuf) = export_structure( diff_analytical_sets )

    temp_dir = fso.mkranddir( '/temps' )
    infile = temp_dir.rpath + '/infile.txt'
    paramfile = temp_dir.rpath + '/baseparams.txt'
    with open( infile, 'w' ) as f:
        f.write( databuf )
    with open( paramfile, 'w') as f:
        f.write( parambuf )
    #pca_file = plot_pca( base_pca, base_pwdist, temp_dir.rpath, "base-pca.png" )
    #vpath_basepca = fso.get_urlpath( pca_file )


    return render_to_response('msaf:templates/tools/structure/report.mako',
            { 'infile': fso.get_urlpath( infile ), 'paramfile': fso.get_urlpath( paramfile ) },
            request = request )
예제 #11
0
파일: nj.py 프로젝트: trmznt/msaf
def index(request):

    if not request.params.get('_method', None) == '_exec':
        queries = get_queries()

        return render_to_response('msaf:templates/tools/nj/index.mako',
                { 'queries': queries, 'markers': get_marker_list() },
                request = request)


    #parse form

    #baseparams = parse_base_params( request.params )
    #spatial_differentiation = int(request.params.get('spatial_differentiation', 4))
    #temporal_differentiation = int(request.params.get('temporal_differentiation', 0))

    label_modifier_file = request.params.get('labelfile', None)
    label_modifier = {}
    if label_modifier_file:
        buf = label_modifier_file.file.read().decode('UTF-8')
        #lines = label_modifier_file.read().split('\n')
        for line in buf.split('\n'):
            if not line: continue
            old_label, new_label = line.split('\t')
            label_modifier[old_label] = new_label

    baseparams = parse_base_params( request.params )
    spatial_differentiation = int(request.params.get('spatial_differentiation', -1))
    temporal_differentiation = int(request.params.get('temporal_differentiation', 0))

    sample_sets = parse_advquerycmd( baseparams.queryset )
    diff_analytical_sets, sample_report, marker_report = get_filtered_analytical_sets2(
                sample_sets = sample_sets,
                baseparams = baseparams,
                spatial_differentiation = spatial_differentiation,
                temporal_differentiation = temporal_differentiation )


    #sample_ids = parse_querycmd( baseparams.queryset )
    #diff_analytical_sets, sample_report, marker_report = get_filtered_analytical_sets(
    #            sample_ids = sample_ids, marker_ids = baseparams.marker_ids,
    #            allele_absolute_threshold = baseparams.allele_absolute_threshold,
    #            allele_relative_threshold = baseparams.allele_relative_threshold,
    #            sample_quality_threshold = baseparams.sample_quality_threshold,
    #            marker_quality_threshold = baseparams.marker_quality_threshold,
    #            spatial_differentiation = spatial_differentiation,
    #            temporal_differentiation = temporal_differentiation )

    base_pwdist = pw_distance( diff_analytical_sets )
    if label_modifier:
        base_pwdist.modify_labels( label_modifier )

    temp_dir = fso.mkranddir('/temps')
    nj_file = nj_tree( base_pwdist, temp_dir.rpath, 'png')
    nj_pdf = nj_tree( base_pwdist, temp_dir.rpath, 'pdf')

    vpath_nj = fso.get_urlpath( nj_file )
    vpath_nj_pdf = fso.get_urlpath( nj_pdf )

    return render_to_response('msaf:templates/tools/nj/report.mako',
            { 'vpath_nj': vpath_nj, 'vpath_nj_pdf': vpath_nj_pdf },
            request = request )
예제 #12
0
파일: genotype.py 프로젝트: trmznt/msaf
def index(request):

    if not request.GET.get('_method', None) == '_exec':
        queries = get_queries()

        return render_to_response('msaf:templates/tools/genotype/index.mako',
                { 'queries': queries, 'markers': get_marker_list() },
                request = request)


    #parse form

    baseparams = parse_base_params( request.GET )
    spatial_differentiation = int(request.GET.get('spatial_differentiation', -1))
    temporal_differentiation = int(request.GET.get('temporal_differentiation', 0))


    # filter samples

    #sample_ids = parse_querycmd( baseparams.queryset )
    sample_sets = parse_advquerycmd( baseparams.queryset )
    diff_analytical_sets, sample_report, marker_report = get_filtered_analytical_sets2(
                sample_sets = sample_sets,
                baseparams = baseparams,
                spatial_differentiation = spatial_differentiation,
                temporal_differentiation = temporal_differentiation )
    #raise RuntimeError
    #diff_analytical_sets, sample_report, marker_report = get_filtered_analytical_sets2(
    #            sample_ids = sample_ids, marker_ids = baseparams.marker_ids,
    #            allele_absolute_threshold = baseparams.allele_absolute_threshold,
    #            allele_relative_threshold = baseparams.allele_relative_threshold,
    #            allele_relative_cutoff = baseparams.allele_relative_cutoff,
    #            sample_quality_threshold = baseparams.sample_quality_threshold,
    #            marker_quality_threshold = baseparams.marker_quality_threshold,
    #            spatial_differentiation = spatial_differentiation,
    #            temporal_differentiation = temporal_differentiation )


    #sample_sets, sample_df = group_samples( sample_ids,
    #                                    spatial_differentiation = spatial_differentiation,
    #                                    temporal_differentiation = temporal_differentiation )

    #base_analytical_sets = create_analytical_sets( sample_sets,
    #                        marker_ids = baseparams.marker_ids,
    #                        allele_absolute_threshold = baseparams.allele_absolute_threshold,
    #                        allele_relative_threshold = baseparams.allele_relative_threshold,
    #                        allele_relative_cutoff = baseparams.allele_relative_cutoff)

    #sample_report, filtered_analytical_sets = assess_sample_quality( base_analytical_sets,
    #                        sample_quality_threshold = baseparams.sample_quality_threshold )

    #marker_report, filtered_marker_ids = assess_marker_quality( filtered_analytical_sets,
    #                        marker_quality_threshold = baseparams.marker_quality_threshold )

    #diff_sample_sets, diff_sample_df = group_samples( get_sample_ids(filtered_analytical_sets),
    #                        spatial_differentiation = spatial_differentiation,
    #                        temporal_differentiation = temporal_differentiation )

    #diff_analytical_sets = create_analytical_sets( diff_sample_sets,
    #                        marker_ids = filtered_marker_ids,
    #                        allele_absolute_threshold = baseparams.allele_absolute_threshold,
    #                        allele_relative_threshold = baseparams.allele_relative_threshold )

    # start analysis here

    #genotype_summary = summarize_genotypes( diff_analytical_sets )

    reports = []

    for analytical_set in diff_analytical_sets:

        data, aux_data, assay_data = prepare_data( analytical_set.get_allele_df() )
        reports.append( ( analytical_set.get_label(), data, aux_data, assay_data ))

    temp_dir = fso.mkranddir( '/temps' )
    tabfile = temp_dir.rpath + '/genotypes.tab'
    with open(tabfile, 'w') as f:
        writer = csv.writer(f, delimiter='\t')
        for (label, rows, aux_rows, assay_rows) in reports:
            writer.writerow(('Label: %s' % label,))
            writer.writerows( rows )
    
    return render_to_response('msaf:templates/tools/genotype/report.mako',
            { 'reports': reports, 'tabfile': fso.get_urlpath(tabfile) },
            request = request )
예제 #13
0
파일: moi.py 프로젝트: trmznt/genaf
def format_output(result_data, user, options):

    results, stats = result_data

    dbh = get_dbhandler()

    html = div()

    # construct table header
    table_header = tr( th('') )
    labels = results.keys()
    for label in labels:
        table_header.add( th(label) )


    # construct table body
    table_body = tbody()

    for row_label, row_key in ( ('Mean', 'mean'), ('Median', 'med'),
                                ('Max', 'max'), ('Std Dev', 'std')):
        cerr('formating table')
        table_row = tr( td(row_label) )
        table_row.add( * tuple( td('%4.3f' % getattr(results[l], row_key))
                        for l in labels ))
        table_body.add( table_row )

    table_row = tr( td('# of polyclonal samples)') )
    for l in labels:
        res = results[l]
        table_row.add( td('%d (%3.2f)' % (res.M, res.M/res.N)) )
    table_body.add( table_row )

    # construct histonum

    table_row = tr( td('# of MoI') )
    for l in labels:
        res = results[l]
        txt = "<table><tr><td>MoI &nbsp;|&nbsp;</td><td># samples</td></tr>"
        for r in res.histogram.items():
            txt += '<tr><td> %d </td><td> %d </td></tr>' % r
        txt += '</table>'
        table_row.add( td( literal(txt) ))
    table_body.add( table_row )

    if stats:
        table_row = tr( td('Statistical test') )
        table_row.add( td( '%s -- p-value: %4.3f' % (stats['test'], stats['stats'].pvalue),
            colspan = len(results) - 1 ))
        table_body.add( table_row )

    table_row = tr( td('# of polyclonal markers') )
    for l in labels:
        res = results[l]
        txt = "<table><tr><td># markers &nbsp;|&nbsp;</td><td># samples</td></tr>"
        for r in res.alleles.items():
            txt += '<tr><td> %d </td><td> %d </td></tr>' % r
        txt += '</table>'
        table_row.add( td( literal(txt) ))
    table_body.add( table_row )


    table_row = tr( td('Polyclonality by markers') )
    for l in labels:
        res = results[l]
        txt = "<table><tr><td>Marker &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;</td><td>Proportion</td></tr>"
        for r in res.markers.items():
            txt += '<tr><td>%s</td><td>%4.3f</td></tr>' % (
                dbh.get_marker_by_id(r[0]).code, r[1]/res.N)
        txt += '</table>'
        table_row.add( td( literal(txt) ))
    table_body.add( table_row )


    table_row = tr( td('Polyclonality rank') )
    for l in labels:
        res = results[l]
        txt = "<table><tr><td>Marker &nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;</td><td>Proportion</td></tr>"
        for r in res.markers_rank:
            txt += '<tr><td>+ %s</td><td>%4.3f</td></tr>' % (
                dbh.get_marker_by_id(r[0]).code, r[1]/res.M)
        txt += '</table>'
        table_row.add( td( literal(txt) ))
    table_body.add( table_row )

    fso_dir = get_fso_temp_dir(user.login)
    table_row = tr( td('Data file') )
    for l in labels:
        res = results[l]
        data_file = 'moi-%s.txt' % l.replace('|','_').replace('/','_').replace(' ', '_')
        data_path = fso_dir.abspath + '/' + data_file
        outstream = open(data_path, 'w')
        outstream.write('SAMPLE_ID\tSAMPLE\tMOI\tMLOCI\n')
        for t in res.sample_dist.itertuples():
            (sample_id, moi_number, mloci_number) = t
            sample_code = dbh.get_sample_by_id(sample_id).code
            outstream.write('%d\t%s\t%d\t%d\n' %
                (sample_id, sample_code, moi_number, mloci_number))
        outstream.close()
        #res.sample_dist.to_csv(data_path)
        table_row.add( td(
            literal('<a href="%s">%s</a>' % (fso.get_urlpath(data_path), data_file)) )
        )
    table_body.add( table_row )

    # construct full table
    cerr('constructing table')
    moi_table = table(class_='table table-condensed table-striped')[
        table_header, table_body
    ]

    html.add( moi_table )

    return (html, '')
예제 #14
0
파일: pca.py 프로젝트: trmznt/msaf
def index(request):

    if not request.GET.get('_method', None) == '_exec':
        queries = get_queries()

        return render_to_response('msaf:templates/tools/pca/index.mako',
                { 'queries': queries, 'markers': get_marker_list() },
                request = request)


    #parse form

    #baseparams = parse_base_params( request.GET )
    #spatial_differentiation = int(request.GET.get('spatial_differentiation', 4))
    #temporal_differentiation = int(request.GET.get('temporal_differentiation', 0))
    dimension = int(request.GET.get('dimension', 2))

    baseparams = parse_base_params( request.GET )
    spatial_differentiation = int(request.GET.get('spatial_differentiation', -1))
    temporal_differentiation = int(request.GET.get('temporal_differentiation', 0))

    sample_sets = parse_advquerycmd( baseparams.queryset )
    diff_analytical_sets, sample_report, marker_report = get_filtered_analytical_sets2(
                sample_sets = sample_sets,
                baseparams = baseparams,
                spatial_differentiation = spatial_differentiation,
                temporal_differentiation = temporal_differentiation )


    #sample_ids = parse_querycmd( baseparams.queryset )
    #sample_sets, sample_df = group_samples( sample_ids,
    #                                    spatial_differentiation = spatial_differentiation,
    #                                    temporal_differentiation = temporal_differentiation )

    #base_analytical_sets = create_analytical_sets( sample_sets,
    #                        marker_ids = baseparams.marker_ids,
    #                        allele_absolute_threshold = baseparams.allele_absolute_threshold,
    #                        allele_relative_threshold = baseparams.allele_relative_threshold )

    #sample_report, filtered_analytical_sets = assess_sample_quality( base_analytical_sets,
    #                        sample_quality_threshold = baseparams.sample_quality_threshold )

    #marker_report, filtered_marker_ids = assess_marker_quality( filtered_analytical_sets,
    #                        marker_quality_threshold = baseparams.marker_quality_threshold )

    #diff_sample_sets, diff_sample_df = group_samples( get_sample_ids(filtered_analytical_sets),
    #                        spatial_differentiation = spatial_differentiation,
    #                        temporal_differentiation = temporal_differentiation )

    #diff_analytical_sets = create_analytical_sets( diff_sample_sets,
    #                        marker_ids = filtered_marker_ids,
    #                        allele_absolute_threshold = baseparams.allele_absolute_threshold,
    #                        allele_relative_threshold = baseparams.allele_relative_threshold )


    base_pwdist = pw_distance( diff_analytical_sets )
    base_pca = pca_distance2( base_pwdist, dimension )


    temp_dir = fso.mkranddir( '/temps' )

    vpaths_basepca = []
    for (ax, ay) in combinations( range( dimension ), 2 ):

        filename = 'base-pca-%d-%d' % (ax, ay)
        pca_file = plot_pca2( base_pca, base_pwdist, ax, ay, temp_dir.rpath, filename + '.png' )
        pca_pdf = plot_pca2( base_pca, base_pwdist, ax, ay, temp_dir.rpath, filename + '.pdf' )
        vpaths_basepca.append( fso.get_urlpath( pca_file ) )

    return render_to_response('msaf:templates/tools/pca/report.mako',
            { 'vpaths_basepca': vpaths_basepca },
            request = request )