Пример #1
0
def search(request):
    if request.method == "GET":
        line = request.GET.getlist("line", [])
        refacc = str(request.GET.get("refacc", "")).strip()
        # order = request.GET.get('order', 'seqname')

        if request.GET.get("align_selected"):
            align_type = "include"

        elif request.GET.get("align_unselected"):
            align_type = "exclude"

        else:
            align_type = "all"

        if align_type == "include" or align_type == "exclude":
            homo_ids = []

            for i in request.GET:
                if re.search("homo_\d+", i):
                    homo_ids.append(int(request.GET.get(i)))

            if not homo_ids:
                # No selected items
                align_type = "all"

        if "line" in request.GET:
            archive_search_form = forms.ArchiveSearchForm(request.GET)
        else:
            archive_search_form = forms.ArchiveSearchForm()

    else:
        raise Http404

    homology_set = Homology.objects.filter(hit_name_id=refacc).order_by("query_name")

    # Store selection information of homology objects
    select_status = {}

    if homology_set.exists():
        refseq = Refseq.objects.get(accession=refacc)

        align_dna_set = []
        align_protein_set = []
        homology_subset = []

        align_protein_set.append((refseq.accession, 0, refseq.seq))

        for homology in homology_set:
            transcript = Transcript.objects.get(seqname=homology.query_name_id)

            if transcript.line in line:
                homology_subset.append(homology)

                if align_type == "include":
                    if homology.id in homo_ids:
                        align_dna_set.append((transcript.seqname, homology.query_frame, transcript.seq))
                        align_protein_set.append((transcript.seqname, homology.query_frame, transcript.seq))
                        select_status.update({homology.id: 1})
                    else:
                        select_status.update({homology.id: 0})

                elif align_type == "exclude":
                    if homology.id not in homo_ids:
                        align_dna_set.append((transcript.seqname, homology.query_frame, transcript.seq))
                        align_protein_set.append((transcript.seqname, homology.query_frame, transcript.seq))
                        select_status.update({homology.id: 1})
                    else:
                        select_status.update({homology.id: 0})

                else:
                    align_dna_set.append((transcript.seqname, homology.query_frame, transcript.seq))
                    align_protein_set.append((transcript.seqname, homology.query_frame, transcript.seq))
                    select_status.update({homology.id: 1})

        if not align_dna_set:
            # No matched results
            return render_to_response(
                "transcriptome/archive.jinja2",
                {"archive_search_form": archive_search_form, "params": request.GET},
                context_instance=RequestContext(request),
            )
        else:
            # Run multiple sequence alignments
            if len(align_dna_set) > 1:
                multiple_dna_alignment = alignment.multiple_dna(*align_dna_set)
                multiple_dna_alignment_html = formatter.clustal_to_html(multiple_dna_alignment, "n")
            else:
                multiple_dna_alignment_html = ""

            multiple_protein_alignment = alignment.multiple_protein(*align_protein_set)
            multiple_protein_alignment_html = formatter.clustal_to_html(multiple_protein_alignment, "a")

            return render_to_response(
                "transcriptome/archive.jinja2",
                {
                    "archive_search_form": archive_search_form,
                    "select_status": select_status,
                    "homology_subset": homology_subset,
                    "multiple_dna_alignment_html": multiple_dna_alignment_html,
                    "multiple_protein_alignment_html": multiple_protein_alignment_html,
                    "params": request.GET,
                },
                context_instance=RequestContext(request),
            )

    else:
        # No matched results
        return render_to_response(
            "transcriptome/archive.jinja2",
            {"archive_search_form": archive_search_form, "params": request.GET},
            context_instance=RequestContext(request),
        )
Пример #2
0
def details(request, commonset, refacc):

    # Check if object existed
    search_options = []

    if refacc:
        search_options.append(Q(accession=refacc))

    commonset_option = {
        'for': Q(commonmutation__formothion=1),
        'fen': Q(commonmutation__fenthion=1),
        'met': Q(commonmutation__methomyl=1),
        'for_fen': Q(commonmutation__for_fen=1),
        'for_met': Q(commonmutation__for_met=1),
        'fen_met': Q(commonmutation__fen_met=1),
        'for_fen_met': Q(commonmutation__for_fen_met=1),
    }

    if commonset_option.get(commonset):
        search_options.append(commonset_option.get(commonset))
        refseqset = Refseq.objects.filter(*search_options)
        if refseqset.count() == 0:
            raise Http404
    else:
        raise Http404

    # Obtain msapset
    msapset = Msap.objects.filter(hit_name=refacc)

    alignment_results = {}
    msaps = {}
    parser_dna = msaparser.Parser()
    parser_protein = msaparser.Parser()

    for insecticide in commonset.split('_'):
        try:
            msap = msapset.get(insecticide=insecticide)
        except ObjectDoesNotExist:
            continue
        else:
            msaps.update({insecticide: msap})

            try:
                ss_transcript = Transcript.objects.get(seqname=msap.ss_name_id)
                rs_transcript = Transcript.objects.get(seqname=msap.rs_name_id)
                rc_transcript = Transcript.objects.get(seqname=msap.rc_name_id)
            except ObjectDoesNotExist:
                raise Http404

            # Executes multiple sequence alignments
            alignment_result_dna = NamedTemporaryFile(prefix='clu_')
            alignment_result_dna.write(alignment.multiple_dna(*[(ss_transcript.seqname,
                                                                 int(msap.ss_frame),
                                                                 ss_transcript.seq),
                                                                (rs_transcript.seqname,
                                                                 int(msap.rs_frame),
                                                                 rs_transcript.seq),
                                                                (rc_transcript.seqname,
                                                                 int(msap.rc_frame),
                                                                 rc_transcript.seq)]))
            alignment_result_dna.flush()
            parser_dna.parse(alignment_result_dna.name, 'n')
            alignment_result_dna.close()

            alignment_result_protein = NamedTemporaryFile(prefix='clu_')
            alignment_result_protein.write(alignment.multiple_protein(*[(ss_transcript.seqname,
                                                                         int(msap.ss_frame),
                                                                         ss_transcript.seq),
                                                                        (rs_transcript.seqname,
                                                                         int(msap.rs_frame),
                                                                         rs_transcript.seq),
                                                                        (rc_transcript.seqname,
                                                                         int(msap.rc_frame),
                                                                         rc_transcript.seq)]))
            alignment_result_protein.flush()
            parser_protein.parse(alignment_result_protein.name, 'a')
            alignment_result_protein.close()

            alignment_results.update({insecticide: (parser_dna.get_clustal_html(),
                                                    parser_protein.get_clustal_html())})

    return render_to_response('transcriptome/svdetails.jinja2',
                              {'msaps': msaps,
                               'commonset': commonset,
                               'refacc': refacc,
                               'refdes': refseqset[0].description,
                               'alignment_results': alignment_results},
                              context_instance=RequestContext(request))