def outputClusterPlotsForAllSNPs(ordered_snpid_ls, snpid2name, snpid2peak1_peak2_color_ls, fig_output_dir, output_fname, need_draw_figure=0): #from pymodule.latex import outputFigureInLatex import os,sys outf = open(output_fname, 'w') for snpid in ordered_snpid_ls: snp_name = snpid2name[snpid] fig_fname = 'cluster_plot_%s'%(snp_name.replace(' ', '_')) #replace space with _ if need_draw_figure: output_fname_prefix=os.path.join(fig_output_dir, fig_fname) drawClusterPlotForOneSNP(snpid2peak1_peak2_color_ls[snpid], snp_name, output_fname_prefix) #output the figure in latex fig_fname = os.path.join('figures/', '%s.png'%fig_fname) #relative path of the figure to the latex file fig_label = 'fl%s'%(snp_name.replace(' ', '')) caption = 'cluster plot for %s.'%(snp_name) outf.write(outputFigureInLatex(fig_fname, caption, fig_label, need_floatpackage=1, fig_width=0.5)) del outf
def OutputCliquesGroupedByRegion(curs, region2clique_id_ls, clique_id2size, clique2ecotype_table, component2clique_table, output_fname, fig_output_dir, stock_db='stock20071008', min_clique_size=3, need_to_draw_figures=0): """ 2007-10-18 """ from variation.src.common import draw_graph_on_map, get_ecotypeid2pos from variation.src.common import get_pic_area from pymodule.latex import outputFigureInLatex import sys,os ecotypeid2pos = get_ecotypeid2pos(curs, '%s.ecotype'%stock_db) outf = open(output_fname, 'w') no_of_regions = 0 clique_id2region = {} #for the cc2clique latex table for region, clique_id_ls in region2clique_id_ls.iteritems(): region = ' and '.join(region) #region is tuple, convert it to string sys.stderr.write("%s:\n"%region) outf.write("\\subsection{%s}\n"%region) size_clique_id_ls = [] for clique_id in clique_id_ls: if clique_id2size[clique_id]>=min_clique_size: size_clique_id_ls.append([clique_id2size[clique_id], clique_id]) outf.write('region %s has %s cliques in total but %s cliques above min size %s.\n'%(region, len(clique_id_ls), len(size_clique_id_ls), min_clique_size)) if len(size_clique_id_ls)==0: continue size_clique_id_ls.sort() caption = 'region %s has %s cliques in total but %s cliques above min size %s.'%(region, len(clique_id_ls), len(size_clique_id_ls), min_clique_size) no_of_regions += 1 table_label = 't_region_%s'%no_of_regions clique_id_size_ls = [[row[1],row[0]] for row in size_clique_id_ls] header_ls = ['clique_id', 'size'] outf.write(outputMatrixInLatexTable(clique_id_size_ls, caption, table_label, header_ls)) for size, clique_id in size_clique_id_ls: sys.stderr.write("clique %s, size %s\n"%(clique_id, size)) #output the table and get ecotype_id_ls fig_label = 'fclique%s'%clique_id table_label = 'tclique%s'%clique_id ecotype_id_ls = outputCliqueInLatexTable(curs, region, clique_id, table_label, fig_label, stock_db, clique2ecotype_table, outf) #draw the clique site_g, site2weight, site2pos = construct_site_graph_out_of_ecotype_id_ls(ecotype_id_ls, ecotypeid2pos) fig_fname = 'haplotype_%s_size%s_%ssites_map'%(clique_id,len(ecotype_id_ls), len(site2pos)) #just the name without filetype extension, like png, eps etc. if need_to_draw_figures: draw_graph_on_map(site_g, site2weight, site2pos, 'clique %s, size %s, %s sites of %s'%(clique_id, len(ecotype_id_ls), len(site2pos), region), pic_area=get_pic_area(site2pos.values(), 30), output_fname_prefix=os.path.join(fig_output_dir, fig_fname)) #output the figure in latex fig_fname = os.path.join('figures/', '%s.png'%fig_fname) #relative path of the figure to the latex file caption = 'map of haplotype clique %s has %s ecotypes from %s sites of %s. check Table~\\ref{%s}.'%(clique_id, len(ecotype_id_ls), len(site2pos), region, table_label) outf.write(outputFigureInLatex(fig_fname, caption, fig_label)) clique_id2region[clique_id] = region del site_g, site2pos, site2weight, ecotype_id_ls outf.write("\\subsection{which component the clique is from}\n") cc_id_clique_id_region_ls = [] cc_id2clique_id_ls = {} for clique_id, region in clique_id2region.iteritems(): curs.execute("select cc_id from %s where clique_id=%s"%(component2clique_table, clique_id)) rows = curs.fetchall() cc_id = rows[0][0] cc_id_clique_id_region_ls.append([cc_id, clique_id, region]) if cc_id not in cc_id2clique_id_ls: cc_id2clique_id_ls[cc_id] = [] cc_id2clique_id_ls[cc_id].append(clique_id) cc_id_clique_id_region_ls.sort() caption = '%s components versus %s cliques'%(len(cc_id2clique_id_ls), len(cc_id_clique_id_region_ls)) table_label = 't_cc2clique_1' header_ls = ['component id', 'clique id', 'region'] outf.write(outputMatrixInLatexTable(cc_id_clique_id_region_ls, caption, table_label, header_ls)) del outf
def OutputCliquesGroupedByRegion(curs, region2clique_id_ls, clique_id2size, clique2ecotype_table, component2clique_table, output_fname, fig_output_dir, stock_db='stock20071008', min_clique_size=3, need_to_draw_figures=0): """ 2007-10-18 """ from variation.src.common import draw_graph_on_map, get_ecotypeid2pos from variation.src.common import get_pic_area from pymodule.latex import outputFigureInLatex import sys, os ecotypeid2pos = get_ecotypeid2pos(curs, '%s.ecotype' % stock_db) outf = open(output_fname, 'w') no_of_regions = 0 clique_id2region = {} #for the cc2clique latex table for region, clique_id_ls in region2clique_id_ls.iteritems(): region = ' and '.join(region) #region is tuple, convert it to string sys.stderr.write("%s:\n" % region) outf.write("\\subsection{%s}\n" % region) size_clique_id_ls = [] for clique_id in clique_id_ls: if clique_id2size[clique_id] >= min_clique_size: size_clique_id_ls.append( [clique_id2size[clique_id], clique_id]) outf.write( 'region %s has %s cliques in total but %s cliques above min size %s.\n' % (region, len(clique_id_ls), len(size_clique_id_ls), min_clique_size)) if len(size_clique_id_ls) == 0: continue size_clique_id_ls.sort() caption = 'region %s has %s cliques in total but %s cliques above min size %s.' % ( region, len(clique_id_ls), len(size_clique_id_ls), min_clique_size) no_of_regions += 1 table_label = 't_region_%s' % no_of_regions clique_id_size_ls = [[row[1], row[0]] for row in size_clique_id_ls] header_ls = ['clique_id', 'size'] outf.write( outputMatrixInLatexTable(clique_id_size_ls, caption, table_label, header_ls)) for size, clique_id in size_clique_id_ls: sys.stderr.write("clique %s, size %s\n" % (clique_id, size)) #output the table and get ecotype_id_ls fig_label = 'fclique%s' % clique_id table_label = 'tclique%s' % clique_id ecotype_id_ls = outputCliqueInLatexTable(curs, region, clique_id, table_label, fig_label, stock_db, clique2ecotype_table, outf) #draw the clique site_g, site2weight, site2pos = construct_site_graph_out_of_ecotype_id_ls( ecotype_id_ls, ecotypeid2pos) fig_fname = 'haplotype_%s_size%s_%ssites_map' % ( clique_id, len(ecotype_id_ls), len(site2pos) ) #just the name without filetype extension, like png, eps etc. if need_to_draw_figures: draw_graph_on_map( site_g, site2weight, site2pos, 'clique %s, size %s, %s sites of %s' % (clique_id, len(ecotype_id_ls), len(site2pos), region), pic_area=get_pic_area(site2pos.values(), 30), output_fname_prefix=os.path.join(fig_output_dir, fig_fname)) #output the figure in latex fig_fname = os.path.join( 'figures/', '%s.png' % fig_fname) #relative path of the figure to the latex file caption = 'map of haplotype clique %s has %s ecotypes from %s sites of %s. check Table~\\ref{%s}.' % ( clique_id, len(ecotype_id_ls), len(site2pos), region, table_label) outf.write(outputFigureInLatex(fig_fname, caption, fig_label)) clique_id2region[clique_id] = region del site_g, site2pos, site2weight, ecotype_id_ls outf.write("\\subsection{which component the clique is from}\n") cc_id_clique_id_region_ls = [] cc_id2clique_id_ls = {} for clique_id, region in clique_id2region.iteritems(): curs.execute("select cc_id from %s where clique_id=%s" % (component2clique_table, clique_id)) rows = curs.fetchall() cc_id = rows[0][0] cc_id_clique_id_region_ls.append([cc_id, clique_id, region]) if cc_id not in cc_id2clique_id_ls: cc_id2clique_id_ls[cc_id] = [] cc_id2clique_id_ls[cc_id].append(clique_id) cc_id_clique_id_region_ls.sort() caption = '%s components versus %s cliques' % ( len(cc_id2clique_id_ls), len(cc_id_clique_id_region_ls)) table_label = 't_cc2clique_1' header_ls = ['component id', 'clique id', 'region'] outf.write( outputMatrixInLatexTable(cc_id_clique_id_region_ls, caption, table_label, header_ls)) del outf