예제 #1
0
 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')
예제 #2
0
    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)
예제 #3
0
    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')
예제 #4
0
 def test_render_template_on_get(self):
     user, proj = self.default_set_up_for_visit_robjects_pages()
     robj = Robject.objects.create(name="rob")
     assign_perm("projects.can_visit_project", user, proj)
     Sample(code='1a2b3c')
     response = self.client.get(self.SAMPLE_LIST_URL)
     self.assertTemplateUsed(response, "samples/samples_list.html")
예제 #5
0
    def test_get_fields(self):
        proj_instance = Project.objects.create(name='proj1_instance')
        rob_instance = Robject.objects.create(name='random1_robject',
                                              project=proj_instance)
        sample_instance = Sample.objects.create(code='sample1_instance',
                                                robject=rob_instance)
        # get a list of verbose names for each field
        my_fields = [
            'robject', 'code', 'owner', 'create_date', 'modify_date',
            'modify_by', 'notes', 'id', 'form', 'source', 'status'
        ]

        fields_verbose = []
        fields = Sample._meta.get_fields()
        # get list of verbose_names from fields
        for field in fields:
            fields_verbose.append(field.verbose_name)
        # get a list of genral fields from robject method

        method_fields = Sample.get_fields(sample_instance, my_fields)
        method_fields_names = list(zip(*method_fields))[0]
        # create list from set
        method_fields_names_list = list(method_fields_names)

        # check equal
        self.assertCountEqual(fields_verbose, method_fields_names_list)
예제 #6
0
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])
예제 #7
0
    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 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()
        print "\t", rowdic
        librarycode = rowdic['librarycode']

        #some samples have two or more libraries generated outof them
        librarycodes = librarycode.split("/")
        librarycode = librarycodes[0]
        print "Library codes:", librarycodes
        print "library code :", librarycode
        libobj = ""
        if Library.objects.filter(library_code=librarycode).exists():
            libobj = Library.objects.get(library_code=librarycode)


            #check if the sample is loaded into Samples.sample already.
            #
            sampleobj = Sample()
            if Sample.objects.filter(sampleid=sampleid).exists():
                sampleobj = Sample.objects.get(sampleid=sampleid)

            sampleobj.sampleid = sampleid
            sampleobj.sampletype = 'RNA'

            #
            label_ontube = rowdic['oncap']
            if rowdic['ontube']:
                label_ontube =  rowdic['oncap'] + ' / ' + rowdic['ontube']
            sampleobj.label_ontube = label_ontube

            # from library object
            sampleobj.organism = libobj.organism
            sampleobj.lifestage = libobj.lifestage
예제 #11
0
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()
예제 #12
0
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()
예제 #13
0
 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)
예제 #14
0
파일: claim.py 프로젝트: msincan/juliabase
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})
예제 #15
0
 def test_render_template_on_get(self):
     user, proj = self.default_set_up_for_visit_robjects_pages()
     assign_perm("projects.can_visit_project", user, proj)
     samp = Sample(code='1a2b3c')
     response = self.client.get(f"/projects/{proj.name}/samples/")
     self.assertTemplateUsed(response, "samples/samples_list.html")
예제 #16
0
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
        })
예제 #17
0
        rowdic = sheetdata[sampleid]
        print "\t", rowdic
        librarycode = rowdic['librarycode']

        #some samples have two or more libraries generated outof them
        librarycodes = librarycode.split("/")
        librarycode = librarycodes[0]
        print "Library codes:", librarycodes
        print "library code :", librarycode
        libobj = ""
        if Library.objects.filter(library_code=librarycode).exists():
            libobj = Library.objects.get(library_code=librarycode)

            #check if the sample is loaded into Samples.sample already.
            #
            sampleobj = Sample()
            if Sample.objects.filter(sampleid=sampleid).exists():
                sampleobj = Sample.objects.get(sampleid=sampleid)

            sampleobj.sampleid = sampleid
            sampleobj.sampletype = 'RNA'

            #
            label_ontube = rowdic['oncap']
            if rowdic['ontube']:
                label_ontube = rowdic['oncap'] + ' / ' + rowdic['ontube']
            sampleobj.label_ontube = label_ontube

            # from library object
            sampleobj.organism = libobj.organism
            sampleobj.lifestage = libobj.lifestage