Mercurial > repos > bgruening > graphclust_motif_finder_plot
annotate MotifFinderPlot.py @ 0:047c451a41fb draft default tip
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
author | bgruening |
---|---|
date | Wed, 22 Feb 2017 17:01:38 -0500 |
parents | |
children |
rev | line source |
---|---|
0
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
1 #!/usr/bin/env python |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
2 |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
3 import matplotlib |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
4 matplotlib.use('Agg') |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
5 from matplotlib import pyplot as plt |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
6 import matplotlib.patches as mpatches |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
7 from collections import defaultdict |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
8 import glob |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
9 import pandas as pd |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
10 import itertools |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
11 import seaborn as sns |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
12 |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
13 |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
14 def plot_bar(ranges, colors, orig_names, cluster_nums): |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
15 fig, ax = plt.subplots() |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
16 for i, k in enumerate(sorted(ranges.keys())): |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
17 ax.broken_barh(ranges[k], (i-0.25, 0.5), facecolors=colors[k]) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
18 |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
19 ax.set_xlim(0) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
20 ax.set_xlabel('position in sequence') |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
21 ax.set_yticklabels(['']+[k+'-'+orig_names[k] for k in sorted(ranges.keys())]) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
22 ax.grid(True) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
23 fig.suptitle('Structure motif prediction\nRegions with same color are prediticted to have similar structures') |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
24 # Add the legend |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
25 patches = [mpatches.Patch(color=cluster_nums[lab], label=lab) for lab in sorted(cluster_nums)] |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
26 ax.legend(handles=patches, loc='best') # , bbox_to_anchor=(1, 0.5), loc='center left') |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
27 plt.savefig("motif_plot.png", bbox_inches='tight') |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
28 |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
29 |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
30 def parse_clusters(): |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
31 currentdir_files = sorted(list(glob.glob('*'))) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
32 print ("currentdir_files are: ", currentdir_files) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
33 print ("RESULTS_files are: ", sorted(list(glob.glob('RESULTS/*')))) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
34 |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
35 cluster_files = sorted(list(glob.glob('RESULTS/*.cluster.all'))) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
36 if len(cluster_files) == 0: |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
37 raise RuntimeError('Expected cluster.all search path is empty:{}'.format(cluster_files)) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
38 palette = itertools.cycle(sns.color_palette("Set2", len(cluster_files))) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
39 |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
40 |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
41 ranges = defaultdict(list) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
42 colors = defaultdict(list) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
43 orig_names = defaultdict(list) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
44 cluster_nums = defaultdict(list) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
45 for cluster_file in cluster_files: |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
46 cluster_color = next(palette) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
47 df_cluster = pd.read_csv(cluster_file, sep='\s+', header=None) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
48 for irow, row in df_cluster.iterrows(): |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
49 seq, start, end, strand = row[0].split("#") |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
50 ranges[seq].append((int(start), int(end)-int(start)+1)) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
51 colors[seq].append(cluster_color) |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
52 assert row[1] == 'RESULT' |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
53 cluster_nums['cluster-{}'.format(row[2])] = cluster_color |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
54 assert row[9] == 'ORIGHEAD' |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
55 orig_names[seq] = row[10] |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
56 return ranges, colors, orig_names, cluster_nums |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
57 |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
58 |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
59 my_ranges, my_colors, my_orig_names, my_cluster_nums = parse_clusters() |
047c451a41fb
planemo upload for repository https://github.com/eteriSokhoyan/galaxytools/tree/master/tools/GraphClust/Plotting commit f22de75204914cfbc4df7437ae586e5651bd474b
bgruening
parents:
diff
changeset
|
60 plot_bar(my_ranges, my_colors, my_orig_names, my_cluster_nums) |