Beispiel #1
0
def update_analysis_append_samples(request, analysis_id):

    if request.method == 'POST':

        form = webforms.DataUploadForm(request.POST, request.FILES)

        if form.is_valid():
            time_stamp = helper_functions.get_time_stamp()
            data_file_path = helper_functions.save_uploaded_file(form.cleaned_data['data_file'], time_stamp, output_file_name = constants.data_file_name)

            data_file_sha1sum = helper_functions.get_sha1sum(data_file_path)

            # just send the data file
            server_request = {'request': 'append_samples_to_analysis',
                              'analysis_id': analysis_id,
                              'data_file_path': data_file_path}

            server_response = server(server_request)

            if server_response['response'] == 'OK':
                return HttpResponse(get_template("simple.tmpl").render(Context({'content': "Your request is being processed."})))
            else:
                return HttpResponse(get_template("simple.tmpl").render(Context({'content': "something happened. has no clue. seriously :("})))
        else:
            return HttpResponse(get_template("error.tmpl").render(Context({'content': "Form wasn't valid :( Please click 'back' button of your browser and re-submit the form."})))

    else:
        form = webforms.DataUploadForm()
        tmpl = "data_upload_form.tmpl"

        return HttpResponse(get_template(tmpl).render(Context({'form': form, 'analysis_id': analysis_id})))
Beispiel #2
0
def split_fasta(request,analysis_id):
    """Really it would be better to just write the zip file directly to the socket somehow (if possible, I'm not sure how the zip format works). But this writing to disk and then reading off and rm-ing seems wasteful"""
    original_file = os.path.join(constants.analyses_dir, analysis_id,constants.data_file_name)
    sep = open(os.path.join(constants.analyses_dir,analysis_id,constants.seperator_file_name)).read()
    of = helper_functions.seqs(open(original_file))
    time_stamp = str(helper_functions.get_time_stamp())
    ids = []
    tmp_dir = '/tmp'
    for seq in of:
        seq_id = time_stamp+seq.split(sep)[0].strip('>')
        f = open(os.path.join(tmp_dir,seq_id),'a')
        f.write(seq)
        f.close()
        ids.append(seq_id)
    ids = set(ids)
    #zip all files into HTTP response
    zip_file = os.path.join(tmp_dir,'all_samples.zip')
    z = zipfile.ZipFile(zip_file, 'w',zipfile.ZIP_DEFLATED)
    for i in ids:
        z.write(os.path.join(tmp_dir,i),i[len(time_stamp):]+'.fna')#timestamp is clipped off for filename sent to user
    z.close()
    """
    args = ['zip',zipfile]+[ os.path.join('/tmp',time_stamp+'*')]
    subprocess.call(args)
    """
    response = HttpResponse(FileWrapper(open(zip_file)), content_type='application/zip')
    
    os.remove(zip_file)
    for i in ids:
        os.remove(os.path.join(tmp_dir,i))
    
    response['Content-Disposition'] = 'attachment; filename=all_samples.zip'
    return response
Beispiel #3
0
def refresh_all_diversity_images():
    server_request = {'request': 'status', 'time_stamp': helper_functions.get_time_stamp()}
    server_response = server(server_request)
    for analysis_id in server_response['done_analyses']:
        print "Refreshing: ", analysis_id, "..."
        server_request = {'request': 'refresh_analysis_files', 'analysis_id': analysis_id, 'refresh_requests': ['diversity_bar_image', 'shannon_diversity_bar_image'], 'time_stamp': helper_functions.get_time_stamp(), 'return_when_done': True}
        server_response = server(server_request)
        print server_response['response']
        print
Beispiel #4
0
def refresh_all_sample_maps():
    server_request = {'request': 'status', 'time_stamp': helper_functions.get_time_stamp()}
    server_response = server(server_request)
    for analysis_id in server_response['done_analyses']:
        server_request = {'request': 'get_sample_map_instances', 'analysis_id': analysis_id, 'time_stamp': helper_functions.get_time_stamp()}
        server_response = server(server_request)
        for instance in server_response['instances']:
            print "Refreshing: ", analysis_id, instance, "..."
            server_request = {'request': 'refresh_sample_map', 'analysis_id': analysis_id, 'instance': instance, 'return_when_done': True, 'time_stamp': helper_functions.get_time_stamp()}
            server_response = server(server_request)
            print server_response['response']
            print
Beispiel #5
0
def exec_env(request):
    if request.method == 'POST':
        form = webforms.EnvUploadForm(request.POST, request.FILES)
        if form.is_valid():
            job_description = form.cleaned_data['job_description']
            time_stamp = helper_functions.get_time_stamp()
            data_file_path = helper_functions.save_uploaded_file(form.cleaned_data['env_file'], time_stamp, output_file_name = constants.data_file_name)

            data_file_sha1sum = helper_functions.get_sha1sum(data_file_path)

            server_request = {'request': 'status'}
            server_response = server(server_request)

            if data_file_sha1sum in server_response['running_analyses']:
                #error we already have it lan
                tmpl = get_template("simple.tmpl")
                return HttpResponse(tmpl.render(Context({'content': "Your ENV file is still being analyzed"})))
            elif data_file_sha1sum in server_response['done_analyses']:
                tmpl = get_template("simple.tmpl")
                return HttpResponse(tmpl.render(Context({'content': "It seems we already have this ENV file analyzed"})))
            # TODO: check validity of the file here..

            server_request = {'request': 'exec_env',
                              'job_description': job_description,
                              'data_file_path': data_file_path,
                              'data_file_sha1sum': data_file_sha1sum}

            server_response = server(server_request)

            if server_response['response'] == 'OK':
                return HttpResponse(get_template("simple.tmpl").render(Context({'content': "Your ENV file is being processed. This is your process id: %s" % server_response['process_id']})))
            else:
                return HttpResponse(get_template("simple.tmpl").render(Context({'content': "something happened"})))
    else:
        form = webforms.EnvUploadForm()

    return HttpResponse(get_template("env_upload_form.tmpl").render(Context({'form': form})))
Beispiel #6
0
def new_analysis(request, analysis_type):
    if request.method == 'POST':

        #import pdb; pdb.set_trace()
        if analysis_type == "rdp":
            form = webforms.FastaUploadForm(request.POST, request.FILES)
        if analysis_type == "qpcr":
            form = webforms.QpcrUploadForm(request.POST, request.FILES)
        if analysis_type == "env":
            form = webforms.EnvUploadForm(request.POST, request.FILES)
        if analysis_type == "blast":
            form = webforms.BlastUploadForm(request.POST, request.FILES)
        if analysis_type == "vamps":
            form = webforms.VampsUploadForm(request.POST, request.FILES)

        if form.is_valid():
            job_description = form.cleaned_data['job_description']
            time_stamp = helper_functions.get_time_stamp()
            if len(request.FILES) == 1:
                data_file_path = helper_functions.save_uploaded_file(
                    form.cleaned_data['data_file'],
                    time_stamp, output_file_name = constants.data_file_name)
            elif len(request.FILES) > 1:
                files = [request.FILES[f] for f in request.FILES.keys() if
                         f.startswith("data_file")]
                data_file_path = cat(files)

            data_file_sha1sum = helper_functions.get_sha1sum(data_file_path)

            server_request = {'request': 'status'}
            server_response = server(server_request)

            if data_file_sha1sum in server_response['running_analyses']:
                return HttpResponse(get_template("simple.tmpl").render(Context({'content': "This data file is still being analyzed"})))
            elif data_file_sha1sum in server_response['done_analyses']:
                return HttpResponse(get_template("simple.tmpl").render(Context({'content': "This data file has already been analyzed. The analysis id is "+str(data_file_sha1sum)})))

            # TODO: check validity of the file here..

            #standard request dict for all analyses.
            server_request = {'request': 'exec_analysis',
                              'analysis_type': analysis_type,
                              'job_description': job_description,
                              'data_file_path': data_file_path,
                              'data_file_sha1sum': data_file_sha1sum}

            #update server request with special options for certain analysis types
            #if your module needs information not contained in a standard request,
            #(say phase of the moon or something), add it here in a tuple of
            #(name, function) where name is the name attribute in the form, and
            #the function converts the raw value or cleaned_data into
            #whatever it needs to be
            special_options = [("seperator",str),
                               ("qa_mode", int),
                               ("db_name",str),
                               ("threshold",float),
                               ("codes_primers",list),
                               ("homopolymer_length",int),
                               ("threshold_dict",dict)]
            for opt in special_options:
                if form.cleaned_data.get(opt[0]):
                    server_request[opt[0]] = opt[1](form.cleaned_data[opt[0]])
                    
             

            server_response = server(server_request)

            if server_response['response'] == 'OK':
                return HttpResponse(get_template("simple.tmpl").render(Context({'content': "Your request is being processed. This is your process id: %s" % server_response['process_id']})))
            else:
                return HttpResponse(get_template("simple.tmpl").render(Context({'content': "Server error:"+str(server_response.get("exception"))})))
        else:
            return HttpResponse(get_template(templates[analysis_type]).render(
                Context({"form":form})))
    else:
        if analysis_type == "rdp":
            form = webforms.FastaUploadForm()
            tmpl = "fasta_upload_form.tmpl"
        elif analysis_type == "qpcr":
            form = webforms.QpcrUploadForm()
            tmpl = "qpcr_upload_form.tmpl"
        elif analysis_type == "env":
            form = webforms.EnvUploadForm()
            tmpl = "env_upload_form.tmpl"
        elif analysis_type == 'blast':
            form = webforms.BlastUploadForm()
            tmpl = "blast_upload_form.tmpl"
        elif analysis_type == "vamps":
            form = webforms.EnvUploadForm()
            tmpl = "vamps_upload_form.tmpl"

        return HttpResponse(get_template(tmpl).render(Context({'form': form})))