def test_recombinations_have_these_required_fields(self): # deletions should be optional here data = { 'nucleotide': 'TGTGCTCCCGAAGCGATGGGCGGATCTGAAAAGCTGGTCTTT', 'v_gene_name': 'TRBV25-1', 'v_ties': 'TRBV25-1,TRBV28', 'd_gene_name': 'TRBD2', 'd_ties': None, 'j_gene_name': 'TRBJ1-1', 'j_ties': None, 'v_deletion': None, 'd5_deletion': None, 'd3_deletion': None, 'j_deletion': None, 'vd_insertion': 0, 'dj_insertion': 27, 'v_end': 4, 'd_start': 5, 'd_end': 9, 'j_start': 37, 'sequence_status': 'Productive', 'cdr3_length': 42, } r = Recombination() for key, value in data.items(): setattr(r, key, value) r.save() r_in_db = Recombination.objects.get() for key, value in data.items(): self.assertEqual( value, getattr(r_in_db, key), 'key value %s not equal' % key)
def vj_freq(self): ''' Returns a list of lists containing frequences of each v_family-j_gene pair and the clonofilter id ''' clonofilters = sorted(self.clonofilters_all()) vj_counts_dict_dict= dict([(clonofilter.id, clonofilter.vj_counts_dict()) for clonofilter in clonofilters]) v_list = sorted(Recombination.v_gene_names()) j_list = sorted(Recombination.j_gene_names()) data = [] # Counts for the scatter plot for clonofilter_id, vj_counts_dict in vj_counts_dict_dict.iteritems(): for v_index, v_family in enumerate(v_list): for j_index, j_gene in enumerate(j_list): data_point = [] data_point.append(v_list[v_index]) data_point.append(j_list[j_index]) if vj_counts_dict[v_family][j_gene]: data_point.append(vj_counts_dict[v_family][j_gene]) else: data_point.append(0) # Append sample id data_point.append(clonofilter_id); if data_point[2] > 0: data.append(data_point) return data
def d3_test(request, comparison_id): ''' This is a test of the d3 library. ''' from clonotypes.models import Recombination from django.utils import simplejson as json from matplotlib.colors import rgb2hex comparison = Comparison.objects.get(id=comparison_id) clonofilters = sorted(comparison.clonofilters_all()) sample_names = dict([(clonofilter.id,str(clonofilter.sample)) for clonofilter in clonofilters]) vj_counts_dict_dict= dict([(clonofilter.id, clonofilter.vj_counts_dict()) for clonofilter in clonofilters]) clonofilter_colors = comparison.colors() v_list = sorted(Recombination.v_family_names()) j_list = sorted(Recombination.j_gene_names()) data = [] # Counts for the scatter plot for clonofilter_id, vj_counts_dict in vj_counts_dict_dict.iteritems(): for v_index, v_family in enumerate(v_list): for j_index, j_gene in enumerate(j_list): data_point = [] data_point.append(v_list[v_index]) data_point.append(j_list[j_index]) if vj_counts_dict[v_family][j_gene]: data_point.append(vj_counts_dict[v_family][j_gene]) else: data_point.append(0) # Append sample id data_point.append(clonofilter_id); if data_point[2] > 0: data.append(data_point) # Functionality stats comp_functionality = [] for cf in clonofilters: cf_functionality = {} values = {} for key, value in cf.functionality_dict().iteritems(): values[key] = value cf_functionality['key'] = cf.id cf_functionality['values'] = values comp_functionality.append(cf_functionality) context = {'data': json.dumps(data), 'v_list': json.dumps(v_list), 'j_list': json.dumps(j_list), 'sample_names': json.dumps(sample_names), 'sample_colors': json.dumps(clonofilter_colors), 'comparison_id': comparison_id, 'functionality': json.dumps(comp_functionality), } return render(request, 'd3_test.html', context)
def vj_freq_ajax(request, comparison_id): comparison = Comparison.objects.get(id=comparison_id) sample_names = comparison.sample_names() data = {'vjFreq': comparison.vj_freq(), 'vList': sorted(Recombination.v_gene_names()), 'jList': sorted(Recombination.j_gene_names()), 'sampleNames': sample_names, } return HttpResponse(json.dumps(data), mimetype='application/json')
def scatter_nav_tag(comparison): from clonotypes.models import Recombination import json clonofilters = sorted(comparison.clonofilters_all()) vj_counts_dict_dict= dict([(clonofilter.id, clonofilter.vj_counts_dict()) for clonofilter in clonofilters]) v_list = sorted(Recombination.v_gene_names()) j_list = sorted(Recombination.j_gene_names()) data = [] # Counts for the scatter plot for clonofilter_id, vj_counts_dict in vj_counts_dict_dict.iteritems(): for v_index, v_family in enumerate(v_list): for j_index, j_gene in enumerate(j_list): data_point = [] data_point.append(v_list[v_index]) data_point.append(j_list[j_index]) if vj_counts_dict[v_family][j_gene]: data_point.append(vj_counts_dict[v_family][j_gene]) else: data_point.append(0) # Append sample id data_point.append(clonofilter_id); if data_point[2] > 0: data.append(data_point) # Functionality stats comp_functionality = [] for cf in clonofilters: cf_functionality = {} values = {} for key, value in cf.functionality_dict().iteritems(): values[key] = value cf_functionality['key'] = cf.id cf_functionality['values'] = values comp_functionality.append(cf_functionality) context = {'data': json.dumps(data), 'v_list': json.dumps(v_list), 'j_list': json.dumps(j_list), 'sample_names': json.dumps(comparison.sample_names()), 'sample_colors': json.dumps(comparison.colors()), 'comparison_id': comparison.id, 'functionality': json.dumps(comp_functionality), } return context
def j_usage_graph(request, clonofilter_id): ''' Returns a PNG of j usage as a line graph ''' from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas import matplotlib.pyplot as plt response = HttpResponse(content_type='image/png') fig, ax = plt.subplots() canvas = FigureCanvas(fig) # variables which will contain data to be plotted usage = [] gene_index = [] # Retreive the clonofilter cf = ClonoFilter.objects.get(id=clonofilter_id) # Get v usage data j_usage_dict = cf.j_usage_dict() # Get a list of v family names j_gene_names = sorted(Recombination.j_gene_names()) # Convert v usage dict into two lists for plotting for j_index, j_gene in enumerate(j_gene_names): if j_gene in j_usage_dict: usage.append(j_usage_dict[j_gene]) else: usage.append(0) gene_index.append(j_index) # Generate the image ax.plot(gene_index, usage, '-', marker='o') # Axes labels and title ax.set_title('%s J Usage' % cf.sample) ax.set_xlabel('J Gene Family') ax.set_ylabel('Usage') xtickNames = plt.setp(ax, xticklabels=j_gene_names) plt.setp(xtickNames, rotation=90, fontsize=9) ax.xaxis.set_ticks(range(len(j_gene_names))) canvas.print_png(response) return response
def test_j_gene_names_returns_list_of_distinct_j_gene_names(self): make_fake_patient_with_3_clonotypes() self.assertIsInstance(Recombination.j_gene_names(), list) self.assertEqual( [u'TRBJ1-1', u'TRBJ2-4'], Recombination.j_gene_names())
def test_functionality_states_returns_list_of_all_functionality_states_in_db(self): make_fake_patient_with_3_clonotypes() self.assertIsInstance(Recombination.functionality_states(), list) self.assertEqual([u'Productive', u'Out of frame'], Recombination.functionality_states())
def test_vj_counts_returns_an_empty_2d_list_with_dimensions_len_vfam_by_jgene(self): v_family_names = Recombination.v_gene_names() j_gene_names = Recombination.j_gene_names() vj_counts = self.f.vj_counts() self.assertEqual(len(v_family_names), len(vj_counts)) self.assertEqual(len(j_gene_names), len(vj_counts[0]))
def bubble(request, clonofilter): ''' Takes in a filter and returns a bubble image generated by matplolib ''' # from matplotlib.figure import Figure from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas import matplotlib.pyplot as plt import numpy as np from math import sqrt from mpl_toolkits.axes_grid1 import make_axes_locatable import pylab # Initialize required stuff for plotting response = HttpResponse(content_type='image/png') # fig = Figure() # ax = fig.add_subplot(111) fig, ax = plt.subplots() canvas = FigureCanvas(fig) # Initialize empty data variables x = [] y = [] color = [] data = [] area = [] vj_counts_dict = clonofilter.vj_counts_dict() # Calculate the plotting area by finding the number of v's and j's v_list = sorted(Recombination.v_gene_names()) j_list = sorted(Recombination.j_gene_names()) width = len(v_list) height = len(j_list) for v_index, v_family in enumerate(v_list): for j_index, j_gene in enumerate(j_list): x.append(v_index) y.append(j_index) if vj_counts_dict[v_family][j_gene]: color.append(vj_counts_dict[v_family][j_gene]) data.append(vj_counts_dict[v_family][j_gene]) else: color.append(0) data.append(0) # Normalize bubble areas if data: # Scales the area of a v-j junction by the size of the plot # and the maximum data value area_norm_factor = (2.0 / max(data)) * width * height area = [datapoint * area_norm_factor for datapoint in data] # Use big outlines # sct = ax.scatter(x, y, c=color, s=area, linewidths=2, edgecolor='w') # Use small sct = ax.scatter( x, y, c=color, s=area, linewidths=1, edgecolor='lightgrey', alpha=.5) # sct.set_alpha(0.5) # Labels, legends, titles etc # Set titles ax.set_title('%s V-J Usage Bubbleplot' % clonofilter.sample) ax.set_xlabel('V Gene Family') ax.set_ylabel('J Gene') # Add a grid ax.yaxis.grid(True, linestyle='-', which='both', color='lightgrey', alpha=0.5) ax.xaxis.grid(True, linestyle='-', which='both', color='lightgrey', alpha=0.5) # Set the axes and plotting area ax.set_xlim(-1, width) ax.set_ylim(-1, height) xtickNames = plt.setp(ax, xticklabels=v_list) plt.setp(xtickNames, rotation=90, fontsize=9) ax.xaxis.set_ticks(range(len(v_list))) ytickNames = plt.setp(ax, yticklabels=j_list) plt.setp(ytickNames, fontsize=9) ax.yaxis.set_ticks(range(len(j_list))) # Shrink the plot to make room for legends box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) # Draw the bubble size legend max_data = max(data) bubbles = [] bubble_labels = [] # Exponentially decreasing areas from max_data to max_data * 2^-8 for bubble_area in [max_data * 1.35 ** (-exp) for exp in range(0, 15)]: # Determine marker size marker_size = sqrt( bubble_area / max_data * (len(j_list) * len(v_list * 2))) # Determine marker color. np.nextafter() is used because pylab.cm.jet # function is looking for a range from [0,1) marker_color = pylab.cm.jet(np.nextafter(bubble_area / max_data, -1)) bubbles.append(plt.Line2D(range(1), range(1), marker='o', markersize=marker_size, color=marker_color, linewidth=0, markeredgecolor="lightgrey", alpha=0.5)) bubble_labels.append('%g' % bubble_area) # Draw the legend #ax.legend(bubbles, bubble_labels, numpoints=1, loc='center left', bbox_to_anchor=(1.175, .5), prop={'size': 10}) ax.legend(bubbles, bubble_labels, numpoints=1, loc='center left', bbox_to_anchor=(1.175, .5), labelspacing=1.5, prop={'size': 10}, title="Counts") # Colorbar # Make the colorbar a bit smaller divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="2%", pad=0.05) cb = plt.colorbar(sct, cax=cax) # Old way # cb = plt.colorbar(sct, ax=ax) cb.set_label("Counts") # Print the png and return canvas.print_png(response) return response
def bubble(request, comparison_id): ''' Takes in a comparison id and generates a bubble plot such that each clonofilter within the comparison instance gets its own color ''' from matplotlib.figure import Figure from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas import matplotlib.pyplot as plt from clonotypes.models import Recombination from math import sqrt import pylab import numpy as np comparison = Comparison.objects.get(id=comparison_id) clonofilters = sorted(comparison.clonofilters_all()) response = HttpResponse(content_type='image/png') fig = Figure() canvas = FigureCanvas(fig) ax = fig.add_subplot(111) clonofilter_colors = comparison.colors_list() x = [] y = [] color = [] data = [] area = [] # Calculate the plotting area by finding the number of v's and j's v_list = sorted(Recombination.v_gene_names()) j_list = sorted(Recombination.j_gene_names()) width = len(v_list) height = len(j_list) # get a list of vj_counts vj_counts_dict_list = [clonofilter.vj_counts_dict() for clonofilter in clonofilters] for clonofilter_index, vj_counts_dict in enumerate(vj_counts_dict_list): for v_index, v_family in enumerate(v_list): for j_index, j_gene in enumerate(j_list): x.append(v_index) y.append(j_index) color.append(clonofilter_colors[clonofilter_index]) if vj_counts_dict[v_family][j_gene]: data.append(vj_counts_dict[v_family][j_gene]) else: data.append(0) # Normalize area by the median and plot area #area_norm_factor = (1.0/median(data)) * width * height if data: area_norm_factor = (2.0/max(data)) * width * height area = [datapoint * area_norm_factor for datapoint in data] sct = ax.scatter(x, y, c=color, s=area, linewidths=2, edgecolor='w') sct.set_alpha(0.4) # Labels, legends, titles etc # Set titles ax.set_title('V-J Usage Comparison') ax.set_xlabel('V Gene Family') ax.set_ylabel('J Gene') # Add a grid ax.yaxis.grid(True, linestyle='-', which='both', color='lightgrey', alpha=0.5) ax.xaxis.grid(True, linestyle='-', which='both', color='lightgrey', alpha=0.5) # Set the axes and plotting area ax.set_xlim(-1, width) ax.set_ylim(-1, height) xtickNames = plt.setp(ax, xticklabels=v_list) plt.setp(xtickNames, rotation=90, fontsize=9) ax.xaxis.set_ticks(range(len(v_list))) ytickNames = plt.setp(ax, yticklabels=j_list) plt.setp(ytickNames, fontsize=9) ax.yaxis.set_ticks(range(len(j_list))) # Shrink the plot to make room for legends box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) # Draw the bubble size legend max_data = max(data) bubbles = [] bubble_labels = [] # Labels for exponentially decreasing bubble sizes for bubble_area in [max_data * 1.45 ** (-exp) for exp in range(0, 10)]: # Determine marker size marker_size = sqrt( bubble_area / max_data * (len(j_list) * len(v_list * 2))) marker_color = 'lightgrey' bubbles.append(plt.Line2D(range(1), range(1), marker='o', markersize=marker_size, color=marker_color, linewidth=0, markeredgecolor="lightgrey", alpha=0.5)) bubble_labels.append('%g' % bubble_area) # Labels for sample colors for index, clonofilter in enumerate(clonofilters): marker_size = sqrt(width * height/ 1.5) marker_color = clonofilter_colors[index] bubbles.append(plt.Line2D(range(1), range(1), marker='o', markersize=marker_size, color=marker_color, linewidth=0, markeredgecolor="lightgrey", alpha=0.5)) bubble_labels.append('%s' % clonofilter.sample) ax.legend(bubbles, bubble_labels, numpoints=1, loc='center left', bbox_to_anchor=(1., .5), labelspacing=1.5, prop={'size': 10}, title="Counts") canvas.print_png(response) return response