def test_bulk_insert_should_throw_error_if_file_does_not_exist(self): p = Patient() p.save() s = Sample(patient=p) s.save() self.assertRaises( IOError, Clonotype.import_adaptive, s, '/fake/path/to/fake/file')
def migrate_library2sample_ingoringSpreadsheet(lib): # check if its been migrated already if Sample.objects.filter(sampleid=lib.sample_name).exists(): print "Library ", lib.library_code, "has been migrated already, skipping." else: #create an new samples.sample object newsampleobj = Sample(organism=lib.organism, lifestage=lib.lifestage, growthphase=lib.growthphase, phenotype=lib.phenotype, genotype=lib.genotype, source=lib.source, sourcename=Source.objects.get(pk=1), sample_concentration=0, sample_volume=0, sample_quantity=0, author_modified=User.objects.get(username="******")) newsampleobj.sampleid = 's' + lib.library_code if lib.sample_name: newsampleobj.sampleid = lib.sample_name newsampleobj.date_created = lib.date_created if lib.sample_notes != 'NA': newsampleobj.sample_notes = lib.sample_notes if lib.experiment_notes != 'NA': newsampleobj.sample_notes = newsampleobj.sample_notes + ' ' + lib.experiment_notes newsampleobj.collected_on = lib.collected_on newsampleobj.collected_at = lib.collected_at newsampleobj.collected_by = lib.collected_by newsampleobj.treatment = lib.treatment newsampleobj.collaborator = lib.collaborator newsampleobj.sampletype = 'RNA' #now save it newsampleobj.save() # save back sampleid into library table lib.sampleid = newsampleobj lib.save()
def test_import_adaptive_data_to_database(self): # Make a test sample p = Patient() p.save() s = Sample(patient=p) s.save() # Model can read in a file Clonotype.import_adaptive(s, 'clonotypes/tests/data/test_adaptive.tsv') all_clonotypes = Clonotype.objects.all() self.assertEquals(len(all_clonotypes), 100)
def test_import_miTCR_to_database(self): # Make a test sample p = Patient() p.save() s = Sample(patient=p) s.save() # Model can read in a file Clonotype.import_mitcr(s, 'clonotypes/tests/data/test_mitcr.txt') all_clonotypes = Clonotype.objects.all() self.assertEquals(len(all_clonotypes), 100) self.fail('todo')
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 test_create_clonotypes_for_a_sample(self): p = Patient() p.save() s = Sample(patient=p) s.save() r = RecombinationFactory() c = Clonotype( sample=s, recombination=r, frequency=9.336458E-6, count=2, ) c.save() # Get all clonotypes from database all_clonotypes = Clonotype.objects.all() self.assertEqual(all_clonotypes[0], c)
def migrate_library2sample_ingoringSpreadsheet(lib): # check if its been migrated already if Sample.objects.filter(sampleid=lib.sample_name).exists(): print "Library ", lib.library_code, "has been migrated already, skipping." else: #create an new samples.sample object newsampleobj = Sample( organism=lib.organism, lifestage=lib.lifestage, growthphase=lib.growthphase, phenotype=lib.phenotype, genotype=lib.genotype, source=lib.source, sourcename=Source.objects.get(pk=1), sample_concentration=0, sample_volume=0, sample_quantity=0, author_modified=User.objects.get(username="******")) newsampleobj.sampleid = 's' + lib.library_code if lib.sample_name: newsampleobj.sampleid = lib.sample_name newsampleobj.date_created = lib.date_created if lib.sample_notes != 'NA': newsampleobj.sample_notes = lib.sample_notes if lib.experiment_notes != 'NA': newsampleobj.sample_notes = newsampleobj.sample_notes + ' ' + lib.experiment_notes newsampleobj.collected_on = lib.collected_on newsampleobj.collected_at = lib.collected_at newsampleobj.collected_by = lib.collected_by newsampleobj.treatment = lib.treatment newsampleobj.collaborator = lib.collaborator newsampleobj.sampletype = 'RNA' #now save it newsampleobj.save() # save back sampleid into library table lib.sampleid = newsampleobj lib.save()
def add_oldstyle(request, username): """View for adding a new claim to old-style sample names. This is a nice example of a view of the app “samples” which is *extended* in the institute app. The template – in this case, :file:`institute/templates/samples/list_claims.html` – overrides and extends the default one, and adds a link to a URL listed in institute's URLconf and pointing to this view function. The important step is the template. This is the hook for your extensions. You override the template from “samples” by creating a file called the same in :file:`institute/templates/samples/`. Because ``TEMPLATE_DIRS`` and ``TEMPLATE_LOADERS`` are defined as recommended in :doc:`/programming/settings`, it shadows its counterpart. By giving the full path, you can still access the original. Thus, you may start your template with :: {% extends "samples/templates/samples/list_claims.html" %} in order to extend it. The `username` parameter of this view function is actually superfluous because it must be the currently logged-in user anyway. But this way, we don't get into trouble if a user happens to be called ``"add"``. Additionally, the URLs become RESTful. :param request: the current HTTP Request object :param username: the name of the user whose claim this will be; it must be the currently logged-in user :type request: HttpRequest :type username: unicode :return: the HTTP response object :rtype: HttpResponse """ _ = ugettext user = get_object_or_404(django.contrib.auth.models.User, username=username) if user != request.user: raise permissions.PermissionError( request.user, _("You are not allowed to add a claim in another user's name.")) if request.method == "POST": samples_form = SamplesForm(request.POST) substrate_form = SubstrateForm(request.POST) reviewer_form = ReviewerForm(request.POST) if samples_form.is_valid() and substrate_form.is_valid( ) and reviewer_form.is_valid(): reviewer = reviewer_form.cleaned_data["reviewer"] claim = SampleClaim(requester=user, reviewer=reviewer) claim.save() _ = lambda x: x send_email( _("Sample request from {requester}"), _("""Hello {reviewer}, {requester} wants to become the new “currently responsible person” of one or more samples. Please visit {url} for reviewing this request. If you don't want or cannot approve the request, please contact {requester} directly and ask him or her to withdraw the request. JuliaBase. """), reviewer, { "reviewer": get_really_full_name(reviewer), "requester": get_really_full_name(user), "url": request.build_absolute_uri( django.core.urlresolvers.reverse( "samples.views.claim.show", kwargs={"claim_id": claim.pk})) }) _ = ugettext samples = [] nobody = django.contrib.auth.models.User.objects.get( username="******") legacy = Topic.objects.get(name="Legacy") now = datetime.datetime.now() material, substrate_comments = substrate_form.cleaned_data[ "material"], substrate_form.cleaned_data["comments"] for name in samples_form.cleaned_data["samples"]: substrate = models.Substrate(operator=nobody, timestamp=now, material=material, comments=substrate_comments) substrate.save() sample = Sample(name=name, current_location="unknown", currently_responsible_person=nobody, topic=legacy) sample.save() sample.processes.add(substrate) samples.append(sample) claim.samples = samples return utils.successful_response( request, _("Sample claim {id_} was successfully submitted.").format( id_=claim.pk), "samples.views.claim.show", kwargs={"claim_id": claim.pk}) else: samples_form = SamplesForm() substrate_form = SubstrateForm() reviewer_form = ReviewerForm() return render( request, "samples/add_claim_oldstyle.html", { "title": _("Assert claim"), "samples": samples_form, "substrate": substrate_form, "reviewer": reviewer_form })
def load_orphan_sample(rowdic): print rowdic sampleid = rowdic['sampleid'] newsampleobj = Sample() if Sample.objects.filter(sampleid=sampleid).exists(): newsampleobj = Sample.objects.get(sampleid=sampleid) newsampleobj.sampleid = sampleid # Things not in spread sheet but essential newsampleobj.sampletype = "RNA" newsampleobj.growthphase = Growthphase.objects.get(growthphase="log") newsampleobj.phenotype = Phenotype.objects.get(phenotype="wildtype") newsampleobj.author_modified = User.objects.get(username="******") newsampleobj.sourcename = Source.objects.get(pk=1) # Things From spread sheet # parentsampleid = "None" if sampleid[-1].isdigit(): parentsampleid = "None" else: parentsampleid = rowdic['sampleid'][:-1] print parentsampleid newsampleobj.parent_sampleid = parentsampleid; # label_ontube = rowdic['oncap'] if rowdic['ontube']: label_ontube = rowdic['oncap'] + ' / ' + rowdic['ontube'] newsampleobj.label_ontube = label_ontube # newsampleobj.genotype = Genotype.objects.get(genotype=rowdic['genotype']) newsampleobj.lifestage = Lifestage.objects.get(lifestage=rowdic['stage']) print rowdic['genus'] print rowdic['species'] print rowdic['strain'] print rowdic['dateisolated'] newsampleobj.organism = Organism.objects.get(genus=rowdic['genus'], species=rowdic['species'], strain=rowdic['strain']) if rowdic['dateisolated']: newsampleobj.collected_on = rowdic['dateisolated'] newsampleobj.collected_by = rowdic['sender'] newsampleobj.collected_by_emailid = rowdic['senderemailid'] newsampleobj.isolation_method = rowdic['isolationmethod'] newsampleobj.sample_dilution = rowdic['dilution'] if rowdic['daterecieved']: newsampleobj.date_received = rowdic['daterecieved'] newsampleobj.sample_concentration = rowdic['concentration'] newsampleobj.sample_volume = rowdic['volume'] newsampleobj.sample_quantity = rowdic['quantity'] newsampleobj.biological_replicate_of = rowdic['replicate'] newsampleobj.freezer_location = rowdic['freezerlocation'] # newsampleobj.sample_notes = rowdic['notesfromsender'] print rowdic['pi'] newsampleobj.collaborator = Collaborator.objects.get(lastname=rowdic['pi']) print newsampleobj newsampleobj.save()
sampleobj.biological_replicate_of = rowdic['replicate'] sampleobj.freezer_location = rowdic['freezerlocation'] sampleobj.sample_notes = libobj.sample_notes + "\n" + rowdic['notesfromsender'] # parentsampleid = "None" if sampleid[-1].isdigit(): parentsampleid = "None" else: parentsampleid = rowdic['sampleid'][:-1] print parentsampleid sampleobj.parent_sampleid = parentsampleid sampleobj.author_modified = User.objects.get(username="******") print sampleobj sampleobj.save() samples_loaded[sampleid]=sampleobj.id for librarycode in librarycodes: libobj = Library.objects.get(library_code=librarycode) libobj.sampleid = sampleobj libobj.save() libraries_appended[librarycode]=libobj.id else: samples_notloaded[sampleid]= librarycode + " does not exist in Library table" print "------------------------------------------------------------" print "Samples not loaded:" print samples_notloaded print "Samples loaded:"
def load_orphan_sample(rowdic): print rowdic sampleid = rowdic['sampleid'] newsampleobj = Sample() if Sample.objects.filter(sampleid=sampleid).exists(): newsampleobj = Sample.objects.get(sampleid=sampleid) newsampleobj.sampleid = sampleid # Things not in spread sheet but essential newsampleobj.sampletype = "RNA" newsampleobj.growthphase = Growthphase.objects.get(growthphase="log") newsampleobj.phenotype = Phenotype.objects.get(phenotype="wildtype") newsampleobj.author_modified = User.objects.get(username="******") newsampleobj.sourcename = Source.objects.get(pk=1) # Things From spread sheet # parentsampleid = "None" if sampleid[-1].isdigit(): parentsampleid = "None" else: parentsampleid = rowdic['sampleid'][:-1] print parentsampleid newsampleobj.parent_sampleid = parentsampleid # label_ontube = rowdic['oncap'] if rowdic['ontube']: label_ontube = rowdic['oncap'] + ' / ' + rowdic['ontube'] newsampleobj.label_ontube = label_ontube # newsampleobj.genotype = Genotype.objects.get(genotype=rowdic['genotype']) newsampleobj.lifestage = Lifestage.objects.get(lifestage=rowdic['stage']) print rowdic['genus'] print rowdic['species'] print rowdic['strain'] print rowdic['dateisolated'] newsampleobj.organism = Organism.objects.get(genus=rowdic['genus'], species=rowdic['species'], strain=rowdic['strain']) if rowdic['dateisolated']: newsampleobj.collected_on = rowdic['dateisolated'] newsampleobj.collected_by = rowdic['sender'] newsampleobj.collected_by_emailid = rowdic['senderemailid'] newsampleobj.isolation_method = rowdic['isolationmethod'] newsampleobj.sample_dilution = rowdic['dilution'] if rowdic['daterecieved']: newsampleobj.date_received = rowdic['daterecieved'] newsampleobj.sample_concentration = rowdic['concentration'] newsampleobj.sample_volume = rowdic['volume'] newsampleobj.sample_quantity = rowdic['quantity'] newsampleobj.biological_replicate_of = rowdic['replicate'] newsampleobj.freezer_location = rowdic['freezerlocation'] # newsampleobj.sample_notes = rowdic['notesfromsender'] print rowdic['pi'] newsampleobj.collaborator = Collaborator.objects.get(lastname=rowdic['pi']) print newsampleobj newsampleobj.save()
sampleobj.freezer_location = rowdic['freezerlocation'] sampleobj.sample_notes = libobj.sample_notes + "\n" + rowdic[ 'notesfromsender'] # parentsampleid = "None" if sampleid[-1].isdigit(): parentsampleid = "None" else: parentsampleid = rowdic['sampleid'][:-1] print parentsampleid sampleobj.parent_sampleid = parentsampleid sampleobj.author_modified = User.objects.get(username="******") print sampleobj sampleobj.save() samples_loaded[sampleid] = sampleobj.id for librarycode in librarycodes: libobj = Library.objects.get(library_code=librarycode) libobj.sampleid = sampleobj libobj.save() libraries_appended[librarycode] = libobj.id else: samples_notloaded[ sampleid] = librarycode + " does not exist in Library table" print "------------------------------------------------------------" print "Samples not loaded:" print samples_notloaded print "Samples loaded:"
def DONTtest_summary_should_redirect_if_a_new_sample_is_provided_in_form(self): s2 = Sample(patient=self.p,cell_type="t", draw_date='1999-11-11') s2.save() url = "%s?clonofilter=1" % reverse('samples.views.summary', args=[s2.id]) self.assertRedirects(self.client.post(reverse('samples.views.summary', args=[self.s.id]), {'sample': s2.id}), url)
def add_oldstyle(request, username): """View for adding a new claim to old-style sample names. This is a nice example of a view of the app “samples” which is *extended* in the institute app. The template – in this case, :file:`institute/templates/samples/list_claims.html` – overrides and extends the default one, and adds a link to a URL listed in institute's URLconf and pointing to this view function. The important step is the template. This is the hook for your extensions. You override the template from “samples” by creating a file called the same in :file:`institute/templates/samples/`. Because ``DIRS`` and ``loaders`` are defined as recommended in :doc:`/programming/settings`, it shadows its counterpart. By giving the full path, you can still access the original. Thus, you may start your template with :: {% extends "samples/templates/samples/list_claims.html" %} in order to extend it. The `username` parameter of this view function is actually superfluous because it must be the currently logged-in user anyway. But this way, we don't get into trouble if a user happens to be called ``"add"``. Additionally, the URLs become RESTful. :param request: the current HTTP Request object :param username: the name of the user whose claim this will be; it must be the currently logged-in user :type request: HttpRequest :type username: unicode :return: the HTTP response object :rtype: HttpResponse """ _ = ugettext user = get_object_or_404(django.contrib.auth.models.User, username=username) if user != request.user: raise permissions.PermissionError(request.user, _("You are not allowed to add a claim in another user's name.")) if request.method == "POST": samples_form = SamplesForm(request.POST) substrate_form = SubstrateForm(request.POST) reviewer_form = ReviewerForm(request.POST) if samples_form.is_valid() and substrate_form.is_valid() and reviewer_form.is_valid(): reviewer = reviewer_form.cleaned_data["reviewer"] claim = SampleClaim(requester=user, reviewer=reviewer) claim.save() _ = lambda x: x send_email(_("Sample request from {requester}"), _("""Hello {reviewer}, {requester} wants to become the new “currently responsible person” of one or more samples. Please visit {url} for reviewing this request. If you don't want or cannot approve the request, please contact {requester} directly and ask him or her to withdraw the request. JuliaBase. """), reviewer, {"reviewer": get_really_full_name(reviewer), "requester": get_really_full_name(user), "url": request.build_absolute_uri(django.core.urlresolvers.reverse("samples.views.claim.show", kwargs={"claim_id": claim.pk}))}) _ = ugettext samples = [] nobody = django.contrib.auth.models.User.objects.get(username="******") legacy = Topic.objects.get(name="Legacy") now = datetime.datetime.now() material, substrate_comments = substrate_form.cleaned_data["material"], substrate_form.cleaned_data["comments"] for name in samples_form.cleaned_data["samples"]: substrate = models.Substrate(operator=nobody, timestamp=now, material=material, comments=substrate_comments) substrate.save() sample = Sample(name=name, current_location="unknown", currently_responsible_person=nobody, topic=legacy) sample.save() sample.processes.add(substrate) samples.append(sample) claim.samples = samples return utils.successful_response(request, _("Sample claim {id_} was successfully submitted.").format(id_=claim.pk), "samples.views.claim.show", kwargs={"claim_id": claim.pk}) else: samples_form = SamplesForm() substrate_form = SubstrateForm() reviewer_form = ReviewerForm() return render(request, "samples/add_claim_oldstyle.html", {"title": _("Assert claim"), "samples": samples_form, "substrate": substrate_form, "reviewer": reviewer_form})