Beispiel #1
0
def viewTimecourseInTable(request):
    params = {}
    params['queries'] = internal.ListQueries(
        request, {'projectID': [request.session['projectID']]})
    params['attributes'] = sorted([
        x.name for x in internal.ListAttributes(
            request, {'projectID': [request.session['projectID']]})
    ])
    if request.method == "POST":
        name = request.POST.get('query', None)
        attr = request.POST.get('attribute', None)
        if name and attr:
            query = Query.objects.get(project=request.session['projectID'],
                                      name=name)
            samples = query.expandsamples
            svm = SubjectMap.objects.filter(sample__in=samples)
            subjects = svm.values_list('subject', flat=True)
            params['subjects'] = {}
            for subj in subjects:
                params['subjects'][subj] = []
                visits = svm.filter(subject=subj).values_list(
                    'visit', flat=True).distinct()
                data = {}
                data['samples'] = []
                for v in visits:
                    data['id'] = v
                    data['samples'] += svm.filter(
                        subject=subj,
                        visit=v).values_list('sample', flat=True).distinct()
                params['subjects'][subj].append(data)
    return render(request, 'viewTimecourseInTable.html', params)
Beispiel #2
0
def comparison(request):
    params = {}
    params['queries'] = internal.ListQueries(
        request, {'projectID': [request.session['projectID']]})
    params['attributes'] = sorted([
        x.name for x in internal.ListAttributes(
            request, {'projectID': [request.session['projectID']]})
    ])
    return render(request, 'visualComparison.html', params)
Beispiel #3
0
def timeseries(request):
    params = {}
    params['queries'] = internal.ListQueries(
        request, {'projectID': [request.session['projectID']]})
    kwargs = {}
    kwargs['projectID'] = [request.session['projectID']]
    params['attributes'] = sorted(
        [x.name for x in internal.ListAttributes(request, kwargs)])
    if request.method == 'POST':
        queryname = request.POST.get('query') or None
        attribute = request.POST.get('attribute') or 'Race'
        if not queryname: return render(request, 'home.html', params)
        query = Query.objects.get(project_id=request.session['projectID'],
                                  name=queryname)
        samplelist = query.expandsamples
        subjects = Attributes.objects.filter(
            project_id=request.session['projectID'],
            sample__in=samplelist,
            field='tssubject.').values_list('value', flat=True)
        allsamples = Attributes.objects.filter(
            project_id=request.session['projectID'],
            field='tssubject.',
            value__in=subjects).values('sample', 'value')
        subjectmap = {}
        for s in allsamples:
            subjectmap.setdefault(s['sample'], {})
            subjectmap[s['sample']]['subject'] = s['value']
        timepoints = Attributes.objects.filter(
            project_id=request.session['projectID'],
            field='tstimepoint.',
            sample__in=subjectmap.keys()).values('sample', 'value')
        for s in timepoints:
            subjectmap[s['sample']]['timepoint'] = float(s['value'])
        attributedata = Attributes.objects.filter(
            project_id=request.session['projectID'],
            sample__in=subjectmap.keys(),
            field=attribute).values('sample', 'value')
        for a in attributedata:
            subjectmap[a['sample']]['feature'] = a['value']
        resp = []
        for sample in subjectmap:
            try:
                resp.append({
                    'subject': subjectmap[sample]['subject'],
                    'timepoint': subjectmap[sample]['timepoint'],
                    'feature': subjectmap[sample]['feature'],
                    'sample': sample
                })
            except:
                continue
        return HttpResponse(json.dumps(resp), content_type="application/json")
    return render(request, 'metadataTimeline.html', params)
Beispiel #4
0
def main(request):
    params = {}
    params['queries'] = internal.ListQueries(
        request, {'projectID': [request.session['projectID']]})
    kwargs = dict(request.GET)
    kwargs['projectID'] = [request.session['projectID']]
    params['attributes'] = sorted(
        [x.name for x in internal.ListAttributes(request, kwargs)])
    if request.method == 'POST':
        queryname = request.POST.get('query') or None
        attribute = request.POST.get('attribute') or 'Race'
        if not queryname: return render(request, 'home.html', params)
        params['feedback'] = 'Query selected: "%s"' % queryname
        query = internal.ListQueries(request, {
            'projectID': [request.session['projectID']],
            'full': [True]
        })
        if not query or not len(query):
            return render(request, 'home.html', params)
        else:
            query = query[0]
        samplelist = myutils.fieldexpand(query['results'])
        dataset = internal.ShowDistribution(
            request, {
                'queryname': [queryname],
                'projectID': [query['project_id']],
                'attribute': [
                    attribute,
                ],
            })
        jsondata = {'key': attribute, 'values': []}
        for d in sorted(dataset[1:], key=lambda x: x[0]):
            tmp = {'label': d[0], 'value': d[1]}
            jsondata['values'].append(tmp)
        params['data'] = dataset
        params['json'] = json.dumps([jsondata])
        params['selectedquery'] = queryname
        params['selectedattribute'] = attribute
        if request.POST.get('format') == 'json':
            return HttpResponse(params['json'],
                                content_type="application/json")
        #params['data'] = sorted(dataset, key=lambda x: x[1])
        #params['data'] = sorted(params['data'], key=lambda x: x[5])
    return render(request, 'attributes.html', params)
Beispiel #5
0
def main(request):
    params = {}
    params['queries'] = internal.ListQueries(
        request, {'projectID': [request.session['projectID']]})
    params['attributes'] = internal.ListAttributes(
        request, {'projectID': [request.session['projectID']]})
    queryname = request.GET.get('query') or None
    inputdataset = request.GET.get('dataset') or None
    method = request.GET.get('method') or None
    category = request.GET.get('category') or None
    attribute = request.GET.get('attribute') or None
    mydata = {}
    sa = {}
    if queryname:
        if not queryname or not inputdataset or not method or not category:
            return HttpResponse(
                "required GET parameters: query, dataset, method, category",
                content_type='text/plain')
        dataset = internal.GetDataset(request,
                                      params={
                                          'queryname': [queryname],
                                          'projectID':
                                          [request.session['projectID']],
                                          'dataset': [inputdataset],
                                          'category': [category],
                                          'method': [method]
                                      })
        attributes = internal.GetData(
            request, {
                'queryname': [queryname],
                'projectID': [request.session['projectID']],
                'attribute': [
                    attribute,
                ],
            })
        attributes.pop(0)
        for a in attributes:
            sa[a[0]] = a[1]
        samples = list(set(dataset.values_list('sample', flat=True)))
        for row in dataset:
            if row.entity in mydata:
                mydata[row.entity][row.sample] = row.profile / 100
            else:
                mydata[row.entity] = {row.sample: row.profile / 100}

        inputfile = "/tmp/%d-%d-%s.txt" % (request.user.pk, int(
            time.time()), attribute.replace(' ', '_'))
        formatfile = inputfile + ".format"
        resultfile = inputfile + ".result"
        with open(inputfile, 'w') as f:
            f.write(attribute)
            for s in samples:
                if s in sa: f.write("\t" + str(sa[s]).lower())
                else: f.write("\tNA")
            f.write("\nsubject_id")
            for s in samples:
                f.write("\t" + str(s))
            for e in sorted(mydata):
                f.write("\n" + e)
                for s in samples:
                    if s in mydata[e]: f.write("\t" + str(mydata[e][s]))
                    else: f.write("\t0")
        formatresp = myutils.runPython("lefse/format_input.py", inputfile,
                                       [formatfile, "-u2", "-o1000000"])
        lefseresp = myutils.runPython("lefse/run_lefse.py", formatfile,
                                      [resultfile]).strip()
        lefseresp = lefseresp.replace('\n', '<br />')
        lefseresp = "<strong>Query:</strong> " + queryname + "<br><strong>Attribute:</strong> " + attribute + "<hr>" + lefseresp
        #lefseresp = myutils.runCmd("python /home/orean/OREAN/scripts/misc/lefse/run_lefse.py "+ formatfile+" "+resultfile)
        rows = ""
        with open(resultfile, 'r') as f:
            rows = f.read()
        os.remove(inputfile)
        os.remove(formatfile)
        os.remove(resultfile)
        return HttpResponse(json.dumps({
            'msg': lefseresp,
            'data': rows
        }),
                            content_type='application/json')
    return render(request, "lefse.html", params)
Beispiel #6
0
def manager(request):
    params = dict()
    params['queries'] = internal.ListQueries(
        request, {'projectID': [request.session['projectID']]})
    params['attributes'] = internal.ListAttributes(
        request, {'projectID': [request.session['projectID']]})
    if request.method == 'POST':
        queryname = request.POST.get('query', None)
        dataset = request.POST.get('dataset', None)
        method = request.POST.get('method', None)
        category = request.POST.get('category', None)
        metadatafield = request.POST.get('attribute', None)
        if not queryname or not dataset or not method or not category or not metadatafield:
            return HttpResponse(
                json.dumps('Please check that the input form is complete.'),
                content_type='application/json')
        query = Query.objects.get(project=request.session['projectID'],
                                  name=queryname)
        filename = md5(request.session['projectName'] + query.name + dataset +
                       method + category + metadatafield).hexdigest() + '.pdf'
        outputpath = mroot + filename
        outputurl = murl + filename
        if not os.path.isfile(outputpath):
            from rpy2 import robjects
            from api.models import Analysis, Attributes
            # fetch Analysis data of interest
            clusterScript = SCRIPTPATH + 'r/hierarchicalClustering.R'
            robjects.r.source(clusterScript)
            clusterCommand = robjects.r['clusterSamples']

            profiles1 = Analysis.objects.filter(
                project=request.session['projectID'],
                dataset=dataset,
                method=method,
                category=category,
                sample__in=query.expandsamples).values_list(
                    'sample', 'entity', 'profile')

            # format analysis data and load into R
            profiles = zip(*profiles1)
            profileRdata = robjects.DataFrame({
                'samples':
                robjects.StrVector(profiles[0]),
                'entity':
                robjects.StrVector(profiles[1]),
                'profile':
                robjects.FloatVector(profiles[2])
            })
            metadata = Attributes.objects.filter(
                project=request.session['projectID'],
                field=metadatafield,
                sample__in=query.expandsamples).values_list('sample', 'value')
            metadata = zip(*metadata)

            # load metadata into R and merge with ordered samples
            metadataRdata = robjects.DataFrame({
                'samples':
                robjects.StrVector(metadata[0]),
                metadatafield:
                robjects.StrVector(metadata[1])
            })

            clusterCommand(profileRdata, metadataRdata, outputpath)
        return HttpResponse(json.dumps(outputurl),
                            content_type='application/json')
    return render(request, 'clustering.html', params)
Beispiel #7
0
 def get_queryset(self):
     kwargs = dict(self.request.QUERY_PARAMS)
     #kwargs['projectID'] = Project.objects.filter(name__in=kwargs['projectID']).values_list('pk', flat=True)
     return internal.ListAttributes(self.request, kwargs)
Beispiel #8
0
def main(request):
    params = {}
    params['queries'] = internal.ListQueries(request, {'projectID': [request.session['projectID']]})
    params['attributes'] = internal.ListAttributes(request, {'projectID': [request.session['projectID']]})
    querynamelist = request.GET.getlist('query') or None
    inputdataset = request.GET.get('dataset') or None
    method = request.GET.get('method') or None
    category = request.GET.get('category') or None
    bothcount = 0
    mydata = {}
    sa = {}
    if querynamelist:
        if not len(querynamelist) == 2 or not inputdataset or not method or not category: return HttpResponse("required GET parameters: query, dataset, method, category, query2" , content_type='text/plain')
        queryname, queryname2 = querynamelist
        query1dataset =  internal.GetDataset(request, params={'queryname': [queryname], 'projectID': [request.session['projectID']], 'dataset': [inputdataset], 'category': [category], 'method': [method]})
        query2dataset =  internal.GetDataset(request, params={'queryname': [queryname2], 'projectID': [request.session['projectID']], 'dataset': [inputdataset], 'category': [category], 'method': [method]})
        #query1dataset =  internal.GetDataset(request, params={'queryname': [queryname], 'projectID': [request.session['projectID']], 'dataset': [inputdataset], 'method': [method]})
        #query2dataset =  internal.GetDataset(request, params={'queryname': [queryname2], 'projectID': [request.session['projectID']], 'dataset': [inputdataset], 'method': [method]})
        query1samples = internal.ListQueries(request, {'projectID': [request.session['projectID']], 'full': [True], 'queryname': [queryname]})[0]['results'].split(',')
        query2samples = internal.ListQueries(request, {'projectID': [request.session['projectID']], 'full': [True], 'queryname': [queryname2]})[0]['results'].split(',')
        totalsamples = list(set(query1samples+query2samples))
        dataset = query1dataset | query2dataset
        for s in totalsamples:
            if s in query1samples and s in query2samples: 
                sa[s] = "Both"
                bothcount+=1
            elif s in query1samples and s not in query2samples:
                sa[s] = queryname
            elif s not in query1samples and s in query2samples:
	        sa[s] = queryname2
            else: 
                return HttpResponse(json.dumps({'msg': "Could not determine how to group sample '%s'. Aborting LefSE analysis." % s, 'data': False}), content_type='application/json')
        if bothcount == len(totalsamples): return HttpResponse(json.dumps({'msg': "Queries have the same sample composition. Cannot perform an analysis", 'data': False}), content_type='application/json')
        samples = list(set(dataset.values_list('sample', flat=True)))
        for row in dataset:
            #if row.entity in mydata: mydata[row.entity][row.sample] = row.profile/100
            #else: mydata[row.entity] = {row.sample: row.profile/100}
            taxaName = row.entity
            if row.taxatree is not None: taxaName = row.taxatree.full_tree
            if taxaName in mydata: mydata[taxaName][row.sample] = row.profile/100
            else: mydata[taxaName] = {row.sample: row.profile/100}
        inputfile = "/tmp/%d-%d-lefse.txt" %(request.user.pk, int(time.time()))
        formatfile = inputfile+".format"
        resultfile = inputfile+".result"
        with open(inputfile, 'w') as f:
            f.write("QueryStatus")
            for s in samples: 
                if s in sa: f.write("\t"+str(sa[s]).lower())
                else: f.write("\tNA")
            f.write("\nsubject_id")
            for s in samples: f.write("\t"+str(s))
            for e in sorted(mydata):
                f.write("\n"+e)
                for s in samples:
                    if s in mydata[e]: f.write("\t"+str(mydata[e][s]))
                    else: f.write("\t0")
        formatresp = myutils.runPython("lefse/format_input.py", inputfile, [formatfile, "-u2", "-o1000000"])
        lefseresp = myutils.runPython("lefse/run_lefse.py", formatfile, [resultfile]).strip()
        lefseresp = lefseresp.replace('\n', '<br />')
        lefseresp = "<strong>Query 1:</strong> "+queryname+"<br><strong>Query 2:</strong> "+queryname2+"<hr>"+lefseresp
        #lefseresp = myutils.runCmd("python /home/orean/OREAN/scripts/misc/lefse/run_lefse.py "+ formatfile+" "+resultfile)
        rows = ""
        with open(resultfile, 'r') as f:
            rows = f.read()
        #os.remove(inputfile)
        #os.remove(formatfile)
        #os.remove(resultfile)
        return HttpResponse(json.dumps({'msg': lefseresp, 'data': rows}), content_type='application/json')
    return render(request, "lefse2.html", params)