def Dump(options,vData, traits, vcfIDs): print "number of rows = ", len(vData) for marker in vData: print "Chr" + str(marker.get_chrom()) + ":" + str(marker.get_pos()) #print marker.get_info() #print marker.info_keys() #print options.colorFlags.split(',') if options.colorFlags: print "color flags:", str(marker.info_is_present(options.colorFlags.split(','))) # ['txDN','tdRC','tdP0']) print "genotypes:", marker.genotypeTally() # organize the traits into a list, returning a list of case/control/None corresponding to the vcfIDs traitIDs = [ str(i) for i in traits[options.id] ] print options.groups, "by genotype" organizedList=CrossTable.cullList(vcfIDs, traitIDs, traits[options.groups]) xTable = CrossTable.xTable(organizedList, marker.get_genotypes()) xTable.printTable() print ""
def dotHistogram(options, vstuff, exonDict, bedRow, traits): """Creates a histogram based upon a set of options, vcf information, a list of exon tuples, a bed of UCSC genomes, and a list of traits.""" ax1, ax2, fig = SetupPlot(options.start, options.stop, options.ymin * -1, options.ymax, options) # for each element of vstuff (the data of chromosomes) create the cross table, add the proper dotGraph to the total plot for v in vstuff: # check to see if the gene is in the exon. If it is, create a cross table, draw the dots and add them to the graph if exonDict.has_key(int(v.get_pos())): xTable = CrossTable.xTable(traits["T2D"], v.get_genotypes()) dots = dotPlot(xTable.getTable(), exonDict[int(v.get_pos())]) ax1.add_collection(dots) exonRect = drawExon(bedRow.get_exons()) # draw the exons ax2.add_collection(exonRect) ax2.add_line(Line2D([-1, 100000000000], [0, 0], linewidth=1, color="black")) if options.png: fig.savefig(options.prefix + ".png") if options.pdf: fig.savefig(options.prefix + ".pdf") if options.graph: plt.show()
def pictograph(options, vData, exonDict, bedRow, traits, region, vcfIDs): '''Creates a plot based upon a set of options, vcf information, a list of exon tuples, a bed of UCSC genomes, and a list of traits.''' start, stop = tuplesDomain((region[1], region[2]), options.introns, exonDict, bedRow)# change the region specified to basepair values dimensions = (start, stop, options.ymin * -1, options.ymax) exoncolors=(options.exoncolor1, options.exoncolor2) plotSize = (options.width, options.height) ax1, ax2, fig = SetupPlot(plotSize, dimensions, options.plotTitle, region[0], options.codons) # initialize the graph, with proper range and choices vDataFiltered = [ vmarker for vmarker in vData if vmarker.checkFilter(options.filterList) ] if options.MAF: #a = max( [ vmarker.get_maf() for vmarker in vDataFiltered ] ) vDataFiltered = [ vmarker for vmarker in vDataFiltered if vmarker.get_maf() < options.MAF ] #b = max( [ vmarker.get_maf() for vmarker in vDataFiltered ] ) #print a, '->', b tableKeys = [] traitIDs = [ str(i) for i in traits[options.id] ] # infoDict ={} colorMap = {} colorKeys = [] i = 0 if options.colorFlags: colorKeys = options.colorFlags.split(',') colorKeys.append(None) for flag in colorKeys: colorMap[flag] = options.palette[i%len(options.palette)] i = i + 1 elif options.colorInfo: colorKeys = set( [ gzutils.itemOrDefault( m.get_info(options.colorInfo), 0, "N/A") for m in vDataFiltered ] ) colorKeys = colorKeys - set(['N/A']) colorKeys = sorted( list(colorKeys) ) colorKeys.append('N/A') for key in colorKeys: colorMap[key] = options.palette[i%len(options.palette)] i = i + 1 if len(colorMap) > len(options.palette): print "More levels than colors in palette. Recycling colors." #print [(k, colorMap[k]) for k in colorKeys] for marker in vDataFiltered: # check to see if the gene is in the exon. If it is, create a cross table, draw the dots and add them to the graph if (exonDict.has_key(int(marker.get_pos()))): organizedList=CrossTable.cullList(vcfIDs, traitIDs, traits[options.groups])# organize the traits into a list, returning a list of case/control/None corresponding to the vcfIDs xTable = CrossTable.xTable(organizedList, marker.get_genotypes()) if len( [ t for t in tableKeys if t != None ] ) < 2: # check for case/control elements in our data tableKeys = [ k for k in xTable.getTable().keys() if k != None ] if options.colorFlags: markerInfo = marker.info_is_present(colorKeys) elif options.colorInfo: try: markerInfo = marker.get_info(options.colorInfo)[0] ### better name later? except IndexError: markerInfo = 'N/A' else: markerInfo = 'N/A' ## marker.get_info()[0] ### better default later? # if not infoDict.has_key(markerInfo): # if the info is not in our dict # if len(infoDict)==len(options.palette): # check to see if we've used our whole palette # print "No more colors in palette. Reusing colors." # infoDict[markerInfo] = options.palette[len(infoDict)%len(options.palette)] # tempColors = (infoDict[markerInfo], infoDict[markerInfo]) tempColors = (colorMap[markerInfo], colorMap[markerInfo]) if len(tableKeys) > 1: if marker.is_indel(): # if this gene is an indel, draw triangles instead drawings = patchPlot(xTable.getTable(), exonDict[int(marker.get_pos())], tempColors, "triangle", tableKeys) else: drawings = patchPlot(xTable.getTable(), exonDict[int(marker.get_pos())], tempColors, options.shape, tableKeys) ax1.add_collection(drawings) print "%s markers plotted after filtering."%len(vDataFiltered) if tableKeys != None and len(tableKeys) > 1: ax1.set_ylabel("%s %s"%(tableKeys[0], tableKeys[-1]))# make y-axis label as needed if not options.nolegend and (options.colorInfo or options.colorFlags): # makeLegend(ax1, infoDict) makeLegend(ax1, colorMap, colorKeys) if bedRow!=[]:# as long as we're actually drawing exons (so a gene, not just a region) exonRect = drawExon(bedRow.get_exons(), exonDict, exoncolors, options.introns) # draw the exons, adding them to the plot ax2.add_collection(exonRect) ax2.add_line(Line2D([-10000000000, 10000000000], [0, 0], linewidth=1, color='black')) if options.png: saveFile = graphName(options.prefix, "results", ".png")# if user has chosen to save graph as a png, save it fig.savefig(saveFile) print "Saved as %s\n"%saveFile if options.pdf: # if user has chosen to save graph as a pdf, save it saveFile = graphName(options.prefix, "results", ".pdf") fig.savefig(saveFile) print "Saved as %s\n"%saveFile if options.graph: # if user has chosen to show the graph, then show it plt.show()