Пример #1
0
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()
Пример #2
0
	def get_maf(self):
		tally = self.genotypeTally()
		af1 = 2 * itemOrDefault(tally,'0/0',0) + itemOrDefault(tally,'0/1',0)
		af2 = 2 * itemOrDefault(tally,'1/1',0) + itemOrDefault(tally,'0/1',0)
		return ( min([af1,af2]) / float(af1 + af2) )