panelCircle.set_xlim([min_radius, max_radius]) panelCircle.set_ylim([min_radius, max_radius]) for patch in myPatches: panelCircle.add_patch(patch) end = time.time() print(end - start) # Print image(s) ifargs.BASENAME is None: file_base = "redwood" else: file_base = args.BASENAME print_images( base_output_name=file_base, image_formats=args.fileform, no_timestamp = args.no_timestamp, dpi=args.dpi, transparent=args.transparent) def feature_set_direction(feature_set_df): """This function determines if the features in the dataframe passed here are all forward, all reverse, or mixed""" all_pos = all(feature_set_df['strand'] == '+') all_neg = all(feature_set_df['strand'] == '-') if all_pos: return '+' elif all_neg: return '-' else: return 'mixed'
def synplot(args): rc.update_rcParams() print(args) GFFs = [] for i in range(len(args.gff_paths)): gffpath = args.gff_paths[i] species = "" if args.gff_labels: species = args.gff_labels[i] GFFs.append(GFFParse(gffpath, args.stop_codons, species)) # find the optimum shuffling pattern # and add a list of samplenames to the args optGFFs = shuffle_optimize_gffs(args, GFFs) # Make a sandwich for a circular comparison setattr(args, 'samplenames', {gff.samplename:gff for gff in optGFFs}) # now get the cms and normalize #cms, normalize = gen_colormaps() cm = black_colormap() ## and we get the protein alignment objects # {seqname: {"featType": featType, # "seqs": {samplename: seq}, # "indices": {samplename: indices}} print("getting alignments") seqs_dict = get_alignments(args) print("done getting alignments") print("seqs_dict is:") print(seqs_dict) # set the figure dimensions if args.ratio: figWidth = args.ratio[0] + 1 figHeight = args.ratio[1] + 1 #set the panel dimensions panelWidth = args.ratio[0] panelHeight = args.ratio[1] else: figWidth = 2.5*4 figHeight = 5 #set the panel dimensions panelWidth = 2.5 * 3 panelHeight = 1.75 figure = plt.figure(figsize=(figWidth,figHeight)) #find the margins to center the panel in figure leftMargin = (figWidth - panelWidth)/2 bottomMargin = ((figHeight - panelHeight)/2) + 0.25 panel0=plt.axes([leftMargin/figWidth, #left bottomMargin/figHeight, #bottom panelWidth/figWidth, #width panelHeight/figHeight]) #height panel0.tick_params(axis='both',which='both',\ bottom='on', labelbottom='off',\ left='off', labelleft='off', \ right='off', labelright='off',\ top='off', labeltop='off') #turn off some of the axes panel0.spines['top'].set_visible(False) panel0.spines['right'].set_visible(False) panel0.spines['left'].set_visible(False) # {seqname: {"featType": featType, # "seqs": {samplename: seq}, # "indices": {samplename: indices}} allPatches = [] for seqname in seqs_dict: #go through in order print(seqname) for i in range(0, len(optGFFs) - 1): samplei = optGFFs[i].samplename samplej = optGFFs[i+1].samplename if samplei in seqs_dict[seqname]["seqs"].keys() and\ samplej in seqs_dict[seqname]["seqs"].keys(): featType = seqs_dict[seqname]["featType"] seq1 = seqs_dict[seqname]["seqs"][samplei] ind1 = seqs_dict[seqname]["indices"][samplei] seq2 = seqs_dict[seqname]["seqs"][samplej] ind2 = seqs_dict[seqname]["indices"][samplej] # this is the top one, just leave it at the actual value since # the base of the annotations start on the integer y1 = len(optGFFs) - 1 - i # this needs to be increased by the bar_thickness (0.9 * track_width in this case, or 0.09) y2 = len(optGFFs) - 2 - i myPatches = plot_synteny(seq1, ind1, seq2, ind2, y1, y2, featType, MI.blosum62, cm, seqname) for patch in myPatches: allPatches.append(patch) #print("len allPatches", len(allPatches)) # this bit plots the simplified lines in the centers ## first we plot all the lines from the centers of matching genes. ## This is temporary. Or maybe it should be a feature #verts = [] #for i in range(len(optGFFs) - 1): # j = i + 1 # coords = optGFFs[i].couple(optGFFs[j], this_y = len(optGFFs) - i, other_y = len(optGFFs) - i - 1) # for coord in coords: # verts.append(coord) #for vert in verts: # xxyy = list(zip(*vert)) # panel0.plot(xxyy[0], xxyy[1]) # now we plot horizontal lines showing the length of the mitochondrial sequence maxseqlen = 0 # this is a heuristic for trackwidth of what looks good in my experience track_multiplier = 0.08 if args.ratio: track_width = track_multiplier * panelWidth else: #0.032 if only 3 #0.062 if 6 track_width = track_multiplier * panelWidth for i in range(len(optGFFs)): gff = optGFFs[i] #print(" - Plotting panels of {}".format(gff), file = sys.stderr) x_offset = 0 #print(" - Detecting if centering is on.".format(gff), file = sys.stderr) if args.center_on: x_offset = -1 * int(gff.features.loc[gff.features['name'] == args.center_on, 'center']) gff = gfftools.x_offset_gff(gff, x_offset) #print(" - Centering is on.".format(gff), file = sys.stderr) #print(" - Plotting horizontal portions with gffplot_horizontal.".format(gff), file = sys.stderr) panel0, patches = gfftools.gffplot_horizontal( figure, panel0, args, gff, track_width = track_width, start_y = len(optGFFs) - i - 1 - ((0.9 * track_width)/2), x_offset = x_offset) #print("{} patches came out of gffplot_horizontal()".format(len(patches))) seq_name = gff.features['sequence'].unique()[0] if args.gff_labels: seq_name = "$\it{{{0}}}$".format(gff.species) panel0.text(0 + x_offset, len(optGFFs) - i - 1 + (0.18/2), seq_name, fontsize = 12, ha='left', va='bottom', color = 'black', zorder = 100) if gff.seqlen > maxseqlen: maxseqlen = gff.seqlen xs = (1 + x_offset, gff.seqlen + x_offset) #ys = [len(optGFFs) - i - 1 + (0.09/2)]*2 ys = [len(optGFFs) - i - 1]*2 #print(" - Plotting lines.".format(gff), file = sys.stderr) panel0.plot(xs, ys, color='black', zorder = -9) #print(" - Adding patches.".format(gff), file = sys.stderr) #print("Right before adding patches there are {} patches.".format(len(patches))) for i in range(len(patches)): patch = patches[i] allPatches.append(patch) for patch in allPatches: panel0.add_patch(patch) panel0.set_xlabel("position (bp)") #panel0.set_xlim([-15000, int(np.ceil(maxseqlen/1000)*1000)]) panel0.set_ylim([ 0 - ( (track_width/2) * 1.1 ), len(optGFFs) - 1 + ( (track_width/2) * 1.1 )]) # This removes the text labels from the plot labels = [item.get_text() for item in panel0.get_xticklabels()] empty_string_labels = ['']*len(labels) print(" - Setting tick labels.".format(gff), file = sys.stderr) panel0.set_xticklabels(empty_string_labels) # Print image(s) print(" - Running print_images.".format(gff), file = sys.stderr) if args.BASENAME is None: file_base = "synteny" else: file_base = args.BASENAME print_images( base=file_base, image_formats=args.fileform, no_timestamp = args.no_timestamp, dpi=args.dpi, transparent=args.transparent)
def browser(args): rc.update_rcParams() print(args) # if the user forgot to add a reference, they must add one if args.REF is None: raise IOError("You must specify the reference fasta file") # if the user forgot to add the start and stop, # Print the id and the start/stop if args.CHR is None or args.START is None or args.STOP is None: print("""\n You have forgotten to specify the chromosome, the start coordinate, or the stop coordinate to plot. Try something like '-c chr1 --start 20 --stop 2000'. Here is a list of chromosome ids and their lengths from the provided reference. The minimum start coordinate is one and the maximum stop coordinate is the length of the chromosome.\n\nID\tLength""") for record in SeqIO.parse(args.REF, "fasta"): print("{}\t{}".format(record.id, len(record.seq))) sys.exit(0) if args.CMD is None: raise IOError("You must specify a plotting command.") # now we parse each set of commands commands = [ PlotCommand(thiscmd, args.REF) for thiscmd in reversed(args.CMD) ] # set the figure dimensions if args.ratio: figWidth = args.ratio[0] + 1 figHeight = args.ratio[1] + 1 #set the panel dimensions panelWidth = args.ratio[0] panelHeight = args.ratio[1] else: figWidth = 7 figHeight = len(commands) + 2 #set the panel dimensions panelWidth = 5 # panel margin x 2 + panel height = total vertical height panelHeight = 0.8 panelMargin = 0.1 figure = plt.figure(figsize=(figWidth, figHeight)) #find the margins to center the panel in figure leftMargin = (figWidth - panelWidth) / 2 bottomMargin = ((figHeight - panelHeight) / 2) + panelMargin plot_dict = { "ref": plot_ref, "bam": plot_bam, "gff3": plot_gff3 #"peptides": plot_peptides } panels = [] for i in range(len(commands)): thiscmd = commands[i] if thiscmd.cmdtype in ["gff3", "ref", "peptides"] \ or thiscmd.style == "depth" \ or "narrow" in thiscmd.options: temp_panelHeight = 0.5 else: temp_panelHeight = panelHeight panels.append( plt.axes([ leftMargin / figWidth, #left bottomMargin / figHeight, #bottom panelWidth / figWidth, #width temp_panelHeight / figHeight ]) #height ) panels[i].tick_params(axis='both',which='both',\ bottom='off', labelbottom='off',\ left='on', labelleft='on', \ right='off', labelright='off',\ top='off', labeltop='off') if thiscmd.cmdtype == "ref": panels[i].tick_params(bottom='on', labelbottom='on') #turn off some of the axes panels[i].spines["top"].set_visible(False) panels[i].spines["bottom"].set_visible(False) panels[i].spines["right"].set_visible(False) panels[i].spines["left"].set_visible(False) panels[i] = plot_dict[thiscmd.cmdtype](panels[i], args.CHR, args.START, args.STOP, thiscmd) bottomMargin = bottomMargin + temp_panelHeight + (2 * panelMargin) # Print image(s) if args.BASENAME is None: file_base = 'browser_{}.png'.format(timestamp()) else: file_base = args.BASENAME path = None if args.path: path = args.path transparent = args.transparent print_images(base_output_name=file_base, image_formats=args.fileform, dpi=args.dpi, no_timestamp=kwargs["no_timestamp"], path=path, transparent=transparent)
def custommargin(df, **kwargs): rc.update_rcParams() # 250, 231, 34 light yellow # 67, 1, 85 # R=np.linspace(65/255,1,101) # G=np.linspace(0/255, 231/255, 101) # B=np.linspace(85/255, 34/255, 101) # R=65/255, G=0/255, B=85/255 Rf = 65 / 255 Bf = 85 / 255 pdict = {'red': ((0.0, Rf, Rf), (1.0, Rf, Rf)), 'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, Bf, Bf), (1.0, Bf, Bf)), 'alpha': ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)) } # Now we will use this example to illustrate 3 ways of # handling custom colormaps. # First, the most direct and explicit: purple1 = LinearSegmentedColormap('Purple1', pdict) # set the figure dimensions fig_width = 1.61 * 3 fig_height = 1 * 3 fig = plt.figure(figsize=(fig_width, fig_height)) # set the panel dimensions heat_map_panel_width = fig_width * 0.5 heat_map_panel_height = heat_map_panel_width * 0.62 # find the margins to center the panel in figure fig_left_margin = fig_bottom_margin = (1 / 6) # lengthPanel y_panel_width = (1 / 8) # the color Bar parameters legend_panel_width = (1 / 24) # define padding h_padding = 0.02 v_padding = 0.05 # Set whether to include y-axes in histograms print(" - Setting panel options.", file = sys.stderr) if kwargs["Y_AXES"]: y_bottom_spine = True y_bottom_tick = 'on' y_bottom_label = 'on' x_left_spine = True x_left_tick = 'on' x_left_label = 'on' x_y_label = 'Count' else: y_bottom_spine = False y_bottom_tick = 'off' y_bottom_label = 'off' x_left_spine = False x_left_tick = 'off' x_left_label = 'off' x_y_label = None panels = [] # Quality histogram panel print(" - Generating the x-axis panel.", file = sys.stderr) x_panel_left = fig_left_margin + y_panel_width + h_padding x_panel_width = heat_map_panel_width / fig_width x_panel_height = y_panel_width * fig_width / fig_height x_panel = generate_panel(x_panel_left, fig_bottom_margin, x_panel_width, x_panel_height, left_tick_param=x_left_tick, label_left_tick_param=x_left_label) panels.append(x_panel) # y histogram panel print(" - Generating the y-axis panel.", file = sys.stderr) y_panel_bottom = fig_bottom_margin + x_panel_height + v_padding y_panel_height = heat_map_panel_height / fig_height y_panel = generate_panel(fig_left_margin, y_panel_bottom, y_panel_width, y_panel_height, bottom_tick_param=y_bottom_tick, label_bottom_tick_param=y_bottom_label) panels.append(y_panel) # Heat map panel heat_map_panel_left = fig_left_margin + y_panel_width + h_padding heat_map_panel_bottom = fig_bottom_margin + x_panel_height + v_padding print(" - Generating the heat map panel.", file = sys.stderr) heat_map_panel = generate_panel(heat_map_panel_left, heat_map_panel_bottom, heat_map_panel_width / fig_width, heat_map_panel_height / fig_height, bottom_tick_param='off', label_bottom_tick_param='off', left_tick_param='off', label_left_tick_param='off') panels.append(heat_map_panel) heat_map_panel.set_title(kwargs["title"]) # Legend panel print(" - Generating the legend panel.", file = sys.stderr) legend_panel_left = fig_left_margin + y_panel_width + \ heat_map_panel_width / fig_width + h_padding legend_panel_bottom = fig_bottom_margin + x_panel_height + v_padding legend_panel_height = heat_map_panel_height / fig_height legend_panel = generate_panel(legend_panel_left, legend_panel_bottom, legend_panel_width, legend_panel_height, bottom_tick_param='off', label_bottom_tick_param='off', left_tick_param='off', label_left_tick_param='off', right_tick_param='on', label_right_tick_param='on') panels.append(legend_panel) # # Everything above this is just to set up the panels # ################################################################## # Set max and min viewing window for the xaxis if kwargs["plot_max_x"]: plot_max_x = kwargs["plot_max_x"] else: if kwargs["square"]: plot_max_x = df[kwargs["xcol"]].max() plot_max_x = max(np.ceil(df[kwargs["xcol"]])) plot_min_x = kwargs["plot_min_x"] # Set x bin sizes if kwargs["xbin"]: x_bin_interval = kwargs["xbin"] else: # again, this is just based on what looks good from experience x_bin_interval = 1 # Generate x histogram print(" - Generating the x-axis histogram.", file = sys.stderr) generate_histogram(panel = x_panel, data_list = df[kwargs['xcol']], min_plot_val = plot_min_x, max_plot_val = plot_max_x, bin_interval = x_bin_interval, hist_horizontal = True, x_label=kwargs['xcol'], y_label=x_y_label, left_spine=x_left_spine) # Set max and min viewing window for the y axis if kwargs["plot_max_y"]: plot_max_y = kwargs["plot_max_y"] else: if kwargs["square"]: plot_max_y = df[kwargs["ycol"]].max() else: plot_max_y = max(np.ceil(df[kwargs["ycol"]])) plot_min_y = kwargs["plot_min_y"] # Set y bin sizes if kwargs["ybin"]: y_bin_interval = kwargs["ybin"] else: y_bin_interval = 1 # Generate y histogram print(" - Generating the y-axis histogram.", file = sys.stderr) generate_histogram(panel = y_panel, data_list = df[kwargs['ycol']], min_plot_val = plot_min_y, max_plot_val = plot_max_y, bin_interval = y_bin_interval, hist_horizontal = False, y_label = kwargs['ycol'], bottom_spine = y_bottom_spine) # Generate heat map if kwargs["square"]: print(" - Generating the square heatmap.", file = sys.stderr) counts = generate_square_map(panel = heat_map_panel, data_frame = df, plot_min_y = plot_min_y, plot_min_x = plot_min_x, plot_max_y = plot_max_y, plot_max_x = plot_max_x, color = purple1, xcol = kwargs["xcol"], ycol = kwargs["ycol"]) else: print(" - Generating the heatmap.", file = sys.stderr) counts = generate_heat_map(panel = heat_map_panel, data_frame = df, plot_min_y = plot_min_y, plot_min_x = plot_min_x, plot_max_y = plot_max_y, plot_max_x = plot_max_x, color = purple1, xcol = kwargs["xcol"], ycol = kwargs["ycol"]) # Generate legend print(" - Generating the legend.", file = sys.stderr) generate_legend(legend_panel, counts, purple1) # inform the user of the plotting window if not quiet mode #if not kwargs["QUIET"]: # print("""plotting in the following window: # {0} <= Q-score (x-axis) <= {1} # {2} <= length (y-axis) <= {3}""".format( # plot_min_x, plot_max_x, min_plot_val, max_plot_val), # file=stderr) # Print image(s) if kwargs["output_base_name"] is None: file_base = "custommargin" else: file_base = kwargs["output_base_name"] print(" - Saving your images", file = sys.stderr) print_images( base =file_base, image_formats=kwargs["fileform"], dpi=kwargs["dpi"], no_timestamp = kwargs["no_timestamp"], transparent= kwargs["no_transparent"])
def margin_plot(df, **kwargs): rc.update_rcParams() # 250, 231, 34 light yellow # 67, 1, 85 # R=np.linspace(65/255,1,101) # G=np.linspace(0/255, 231/255, 101) # B=np.linspace(85/255, 34/255, 101) # R=65/255, G=0/255, B=85/255 Rf = 65 / 255 Bf = 85 / 255 pdict = { 'red': ((0.0, Rf, Rf), (1.0, Rf, Rf)), 'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, Bf, Bf), (1.0, Bf, Bf)), 'alpha': ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)) } # Now we will use this example to illustrate 3 ways of # handling custom colormaps. # First, the most direct and explicit: purple1 = LinearSegmentedColormap('Purple1', pdict) # set the figure dimensions fig_width = 1.61 * 3 fig_height = 1 * 3 fig = plt.figure(figsize=(fig_width, fig_height)) # set the panel dimensions heat_map_panel_width = fig_width * 0.5 heat_map_panel_height = heat_map_panel_width * 0.62 # find the margins to center the panel in figure fig_left_margin = fig_bottom_margin = (1 / 6) # lengthPanel length_panel_width = (1 / 8) # the color Bar parameters legend_panel_width = (1 / 24) # define padding h_padding = 0.02 v_padding = 0.05 # Set whether to include y-axes in histograms if kwargs["Y_AXES"]: length_bottom_spine = True length_bottom_tick = 'on' length_bottom_label = 'on' qual_left_spine = True qual_left_tick = 'on' qual_left_label = 'on' qual_y_label = 'Count' else: length_bottom_spine = False length_bottom_tick = 'off' length_bottom_label = 'off' qual_left_spine = False qual_left_tick = 'off' qual_left_label = 'off' qual_y_label = None panels = [] # Quality histogram panel qual_panel_left = fig_left_margin + length_panel_width + h_padding qual_panel_width = heat_map_panel_width / fig_width qual_panel_height = length_panel_width * fig_width / fig_height qual_panel = generate_panel(qual_panel_left, fig_bottom_margin, qual_panel_width, qual_panel_height, left_tick_param=qual_left_tick, label_left_tick_param=qual_left_label) panels.append(qual_panel) # Length histogram panel length_panel_bottom = fig_bottom_margin + qual_panel_height + v_padding length_panel_height = heat_map_panel_height / fig_height length_panel = generate_panel(fig_left_margin, length_panel_bottom, length_panel_width, length_panel_height, bottom_tick_param=length_bottom_tick, label_bottom_tick_param=length_bottom_label) panels.append(length_panel) # Heat map panel heat_map_panel_left = fig_left_margin + length_panel_width + h_padding heat_map_panel_bottom = fig_bottom_margin + qual_panel_height + v_padding heat_map_panel = generate_panel(heat_map_panel_left, heat_map_panel_bottom, heat_map_panel_width / fig_width, heat_map_panel_height / fig_height, bottom_tick_param='off', label_bottom_tick_param='off', left_tick_param='off', label_left_tick_param='off') panels.append(heat_map_panel) heat_map_panel.set_title(kwargs["title"]) # Legend panel legend_panel_left = fig_left_margin + length_panel_width + \ heat_map_panel_width / fig_width + h_padding legend_panel_bottom = fig_bottom_margin + qual_panel_height + v_padding legend_panel_height = heat_map_panel_height / fig_height legend_panel = generate_panel(legend_panel_left, legend_panel_bottom, legend_panel_width, legend_panel_height, bottom_tick_param='off', label_bottom_tick_param='off', left_tick_param='off', label_left_tick_param='off', right_tick_param='on', label_right_tick_param='on') panels.append(legend_panel) # Set min and max viewing window for length if kwargs["plot_maxlen"]: max_plot_length = kwargs["plot_maxlen"] else: max_plot_length = int(np.percentile(df['length'], 99)) min_plot_length = kwargs["plot_minlen"] # Set length bin sizes if kwargs["lengthbin"]: length_bin_interval = kwargs["lengthbin"] else: # Dividing by 80 is based on what looks good from experience length_bin_interval = int(max_plot_length / 80) # length_bins = np.arange(0, max_plot_length, length_bin_interval) # Set max and min viewing window for quality if kwargs["plot_maxqual"]: max_plot_qual = kwargs["plot_maxqual"] else: max_plot_qual = max(np.ceil(df['meanQual'])) min_plot_qual = kwargs["plot_minqual"] # Set qual bin sizes if kwargs["qualbin"]: qual_bin_interval = kwargs["qualbin"] else: # again, this is just based on what looks good from experience qual_bin_interval = max_plot_qual / 85 qual_bins = np.arange(0, max_plot_qual, qual_bin_interval) # Generate length histogram generate_histogram(length_panel, df['length'], max_plot_length, min_plot_length, length_bin_interval, hist_horizontal=False, y_label='Read Length', bottom_spine=length_bottom_spine) # Generate quality histogram generate_histogram(qual_panel, df['meanQual'], max_plot_qual, min_plot_qual, qual_bin_interval, x_label='Phred Quality', y_label=qual_y_label, left_spine=qual_left_spine) # Generate heat map counts = generate_heat_map(heat_map_panel, df, min_plot_length, min_plot_qual, max_plot_length, max_plot_qual, purple1) # Generate legend generate_legend(legend_panel, counts, purple1) # inform the user of the plotting window if not quiet mode if not kwargs["QUIET"]: print("""plotting in the following window: {0} <= Q-score (x-axis) <= {1} {2} <= length (y-axis) <= {3}""".format(min_plot_qual, max_plot_qual, min_plot_length, max_plot_length), file=stderr) # Print image(s) if kwargs["BASENAME"] is None: file_base = opath.splitext(opath.basename(kwargs["fastq"]))[0] else: file_base = kwargs["BASENAME"] if "path" in kwargs.keys(): path = kwargs["path"] else: path = None print_images(base_output_name=file_base, image_formats=kwargs["fileform"], dpi=kwargs["dpi"], path=path, transparent=kwargs["TRANSPARENT"])