def test_default_with_samples_does_not_create_a_new_Comparison_if_one_already_exists_for_those_samples(self): ''' Should check for a Comparison with the samples prior to creating a new one ''' samples = [self.s] comp_1 = Comparison.default_from_samples(samples) comp_2 = Comparison.default_from_samples(samples) self.assertEqual(1, Comparison.objects.all().count())
def test_get_or_create_from_clonofilters_does_not_create_a_new_comparison_if_one_already_exists_for_the_set_of_clonofilters(self): cf_1 = ClonoFilter(sample=self.s) cf_1.save() cf_2 = ClonoFilter(sample=self.s) cf_2.save() Comparison.get_or_create_from_clonofilters([cf_1, cf_2]) Comparison.get_or_create_from_clonofilters([cf_1, cf_2]) self.assertEqual(1, Comparison.objects.all().count())
def summary_to_comparison(request, sample_id): ''' Given a sample id, create a comparison and foward user to comparison view ''' s = Sample.objects.get(id=sample_id) comp = Comparison.default_from_samples([s]) return HttpResponseRedirect(reverse('cf_comparisons.views.compare_v3', args=[comp.id]))
def test_sample_compare_redirects_to_compare_view_after_post(self): sample_ids = [sample.id for sample in Sample.objects.all()] comparison = Comparison.default_from_samples(Sample.objects.all()) response = self.client.post( reverse('cf_comparisons.views.sample_compare'), {'samples': sample_ids}) self.assertRedirects(response, reverse('cf_comparisons.views.compare', args=[comparison.id]))
def make_fake_comparison_with_2_samples(): from clonotypes.models import AminoAcid make_fake_patient_with_2_clonotypes() p = Patient.objects.get() s1 = Sample.objects.get() s2 = Sample(patient=p, draw_date='2012-12-13', cell_type='cd4+') s2.save() # Make sure there's an overlapping amino_acid here aa = AminoAcid.objects.all()[0] r = aa.recombination_set.all()[0] c = ClonotypeFactory( sample=s2, recombination = r ) Comparison.default_from_samples([s1, s2])
def sample_compare(request): from cf_comparisons.forms import SampleCompareForm if request.method == 'POST': sample_compare_form = SampleCompareForm(request.POST) if sample_compare_form.is_valid(): comparison = Comparison.default_from_samples( sample_compare_form.cleaned_data['samples']) return HttpResponseRedirect(reverse('cf_comparisons.views.compare', args=[comparison.id])) sample_compare_form = SampleCompareForm() context = {'sample_compare_form': sample_compare_form} return render(request, 'sample_compare.html', context)
def remove_clonofilter(request): ''' Given a comparison id and a clonofilter id from post get or create a comparison without the specified clonofilter. Returns updated comparison id via http response. ''' if request.method == "POST": comp = Comparison.objects.get(id=request.POST['comparison']) cfs = [cf for cf in comp.clonofilters_all() if cf.id != int(request.POST['clonofilter'])] new_comp, created = Comparison.get_or_create_from_clonofilters(cfs) return HttpResponse(new_comp.id) else: return HttpResponseRedirect(reverse('cf_comparisons.views.compare_v3'))
def test_default_with_samples_populates_a_comparison_object_with_default_clonofilters(self): ''' We should be able to create a Comparison instance given just a list of samples. The ClonoFilters in the Comparison object should be the default ClonoFilter of each sample. ''' samples = [self.s] comp = Comparison.default_from_samples(samples) cf = ClonoFilter.objects.get() self.assertEqual(set([cf]), set(comp.clonofilters_all()))
def compare(request, comparison_id): ''' Compare view takes in a comparison object and generates a summary view that compares an arbitrary number of clonofilters ''' if request.method == 'POST': num_forms = int(request.POST['num_forms']) cf_forms = [ClonoFilterForm(request.POST, prefix=str(x)) for x in range(0, num_forms)] if all([cf_form.is_valid() for cf_form in cf_forms]): clonofilters = [] for cf_form in cf_forms: cf, created = ClonoFilter.objects.get_or_create( **cf_form.cleaned_data) clonofilters.append(cf) comparison, created = Comparison.get_or_create_from_clonofilters(clonofilters) else: comparison = Comparison.objects.get(id=comparison_id) for cf_form in cf_forms: print cf_form.errors return HttpResponseRedirect(reverse('cf_comparisons.views.compare', args=[comparison.id])) comparison = Comparison.objects.get(id=comparison_id) filter_forms = comparison.filter_forms_list() shared_amino_acids = comparison.get_shared_amino_acids_related() samples = comparison.get_samples() context = { 'comparison': comparison, 'samples': samples, 'shared_amino_acids': shared_amino_acids, } if request.is_ajax(): return render(request, 'compare_ajax.html', context) else: # context.update({'comparison': comparison}) return render(request, 'compare.html', context)
def test_get_or_create_from_clonofilters_returns_one_comparison_if_two_supersets_exist(self): ''' This test replicates a bug where multiple comparisons are returned if multiple comparisons exist which contain a subset of clonofilters. This is most evident when creating a new comparison from a list of samples ''' s1 = Sample.objects.all()[0] s2 = Sample.objects.all()[1] Comparison.default_from_samples([s1]) Comparison.default_from_samples([s1, s2]) Comparison.default_from_samples([s1]) self.assertEqual(2, Comparison.objects.all().count())
def update_clonofilters(request, comparison_id): comparison = Comparison.objects.get(id=comparison_id) if request.method == 'POST': # num_forms = int(request.POST['num_forms']) cf_forms = [ClonoFilterForm(request.POST, prefix=str(x)) for x in range(len(comparison.clonofilters_all()))] if all([cf_form.is_valid() for cf_form in cf_forms]): clonofilters = [] for cf_form in cf_forms: cf, created = ClonoFilter.objects.get_or_create( **cf_form.cleaned_data) clonofilters.append(cf) comparison, created = Comparison.get_or_create_from_clonofilters(clonofilters) else: omparison = Comparison.objects.get(id=comparison_id) for cf_form in cf_forms: print cf_form.errors return HttpResponse(comparison.id, mimetype='application/json')
def add_samples_v2(request): ''' This should be moved to cf_comparisons Processes an ajax request. Given an array of sampleId's, returns an array of clonofilter forms. The clonofilter forms are then submitted to generate a new comparison id which is used to generate all interactive dataplots. ''' if request.method == "POST": sample_ids = [int(i) for i in request.POST.getlist('samples')] samples = Sample.objects.filter(id__in=sample_ids) # See if there is an existing sample try: old_comparison = Comparison.objects.get(id=request.POST['comparison']) comparison = old_comparison.add_samples(samples) except: comparison = Comparison.default_from_samples(samples) return HttpResponse(comparison.id) else: return HttpResponseRedirect(reverse('cf_comparisons.views.compare_v3'))
def test_get_shared_clonotypes_returns_empty_dict_if_only_one_clonofilter_is_provided(self): cf = ClonoFilter.objects.all()[0] comparison, created = Comparison.get_or_create_from_clonofilters([cf]) self.assertEqual({}, comparison.get_shared_clonotypes())