示例#1
0
def idea(request):
    from api.models import *
    from django.db.models import Max
    params = {}
    params['queries'] = internal.ListQueries(request, {'projectID': [request.session['projectID']]})
    if request.method=='POST':
        queryname = request.POST.get('query') or None
        datasetname = request.POST.get('dataset') or None
        method = request.POST.get('method') or None
        category = request.POST.get('category') or None
        featurecount = request.POST.get('amount', 10)
        featurecount = int(featurecount) - 1
        if not queryname or not datasetname or not method or not category: return render(request, 'profile.html', params)
        query = internal.ListQueries(request, {'projectID': [request.session['projectID']], 'full': [True], 'queryname': [queryname]})
        if not query or not len(query): return render(request, 'profile.html', params)
        else: query = query[0]

        filename = md5('stackedbars.%d.%d.%s.%s.%s.%d' % ( query['id'], query['project_id'], datasetname, method, category, featurecount )).hexdigest() + '.json'
        furl = murl + filename
        fpath = mroot + filename

        if not os.path.isfile(fpath):
	    samplelist = myutils.fieldexpand(query['results']) 
            toptaxa = Analysis.objects.filter(project=request.session['projectID'], dataset=datasetname, method=method, category=category, sample__in=samplelist).values('entity').annotate(maxprofile=Max('profile')).distinct().order_by('-maxprofile')[:featurecount] 
            taxadict = {}
            resp = [{'name': 'other', 'data': []}]
            for i,t in enumerate(toptaxa, start=1):
                taxadict[t['entity']] = {'loc': i, 'samples': []}
                resp.append({'name': t['entity'], 'data': []}) 
            rows = Analysis.objects.filter(project=request.session['projectID'], dataset=datasetname, method=method, category=category, sample__in=samplelist, entity__in=taxadict.keys()).order_by('-profile')
            sampleloc = dict()
            sampleorder = []
            for row in rows:
               if row.sample not in sampleloc:
                   sampleloc[row.sample] = {'group': row.entity, 'loc': len(taxadict[row.entity]['samples']), 'other': 100.0}
                   taxadict[row.entity]['samples'].append([{'sample': row.sample, 'entity': row.entity, 'y': row.profile}])
               else: 
                   taxadict[sampleloc[row.sample]['group']]['samples'][sampleloc[row.sample]['loc']].append({'sample': row.sample, 'entity': row.entity, 'y': row.profile})
               sampleloc[row.sample]['other'] = sampleloc[row.sample]['other'] - row.profile
            ctr = 0
            for entitydict in toptaxa:
               entity = entitydict['entity']
               for samplelist in taxadict[entity]['samples']:
                  resp[0]['data'].append({'x': ctr, 'y': sampleloc[samplelist[0]['sample']]['other']})
                  sampleorder.append(samplelist[0]['sample'])
                  for sample in samplelist:
                     sample.pop("sample", None)
                     cur_entity = sample.pop("entity", None)
                     sample['x'] = ctr
                     resp[taxadict[cur_entity]['loc']]['data'].append(sample)
                  ctr += 1
            with open(fpath, 'w') as outfile:
                json.dump([sampleorder, resp], outfile)
        return HttpResponse(json.dumps(furl), content_type="application/json")
    return render(request, 'ideastackedbars.html', params)
示例#2
0
文件: area.py 项目: nisheth/OREAN
def main(request):
    params = {}
    params['time'] = []
    params['queries'] = internal.ListQueries(request, {'projectID': [request.session['projectID']]})
    if request.method=='POST':
        start = time.time()
        queryname = request.POST.get('query') or None
        dataset = request.POST.get('dataset') or None
        method = request.POST.get('method') or None
        category = request.POST.get('category') or None
        if not queryname or not dataset or not method or not category: return render(request, 'profile.html', params)
        params['feedback'] = 'Query selected: "%s"' % queryname
        query = internal.ListQueries(request, {'projectID': [request.session['projectID']], 'full': [True], 'queryname': [queryname]})
        if not query or not len(query): return render(request, 'profile.html', params)
        else: query = query[0]
        samplelist = myutils.fieldexpand(query['results'])
        mark = time.time()
        dataset =  internal.GetDataset(request, params={'queryname': [queryname], 'projectID': [query['project_id']], 'dataset': [dataset], 'category': [category], 'method': [method]})
        jsondata = []
        magichash = {}
        maxhash = {}
        sorterhash = {}
        entityorder = []
        first = True
        for d in dataset:
            if first: 
                params['time'].append('parsed input and fetched API data: %.02f' %(time.time() - start))
                first=False
            if d.profile < 1: continue
            if d.sample not in maxhash or d.profile > maxhash[d.sample]['val']: maxhash[d.sample] = {'entity': d.entity, 'val': d.profile}
            if d.entity in magichash: magichash[d.entity][d.sample] = d.profile
            else: magichash[d.entity] = {d.sample:d.profile}
        params['time'].append('computed max profile values: %.02f ' %(time.time() - start))
        for sample in sorted(maxhash, key=lambda x: maxhash[x]['val'], reverse=True):
            if maxhash[sample]['val'] < 5: continue 
            if maxhash[sample]['entity'] not in sorterhash: 
                sorterhash[maxhash[sample]['entity']] = [sample]
                entityorder.append(maxhash[sample]['entity'])
            else: sorterhash[maxhash[sample]['entity']].append(sample)
        params['time'].append('sorted taxa: %.02f' %(time.time() - start))
        samplelist = []
        for element in entityorder:
            for x in sorterhash[element]: samplelist.append(x)
        params['time'].append('sorted samples: %.02f' %(time.time() - start))
        for m in magichash:
            tmp = {'name': m, 'data': []}
            for i,s in enumerate(samplelist):
                if s in magichash[m]: tmp['data'].append(magichash[m][s])
                else: tmp['data'].append(0)
            jsondata.append(tmp)
        params['time'].append('formatted data for JSON conversion: %.02f' %(time.time() - start))
        params['json'] = json.dumps([samplelist, jsondata, params['time']])
        return HttpResponse(params['json'], content_type="application/json")
    return render(request, 'profile.html', params)
示例#3
0
def main(request):
    params = {}
    params['queries'] = internal.ListQueries(request, {'projectID': [request.session['projectID']]})
    if request.method=='POST':
        start = time.time()
        queryname = request.POST.get('query') or None
        datasetname = request.POST.get('dataset') or None
        method = request.POST.get('method') or None
        category = request.POST.get('category') or None
        if not queryname or not datasetname or not method or not category: return render(request, 'profile.html', params)
        params['feedback'] = 'Query selected: "%s"' % queryname

        query = internal.ListQueries(request, {'projectID': [request.session['projectID']], 'full': [True], 'queryname': [queryname]})
        if not query or not len(query): return render(request, 'profile.html', params)
        else: query = query[0]
        filename = 'stackedbars.%d.%d.%s.%s.%s.csv' % ( query['id'], query['project_id'], datasetname, method, category )
        furl = murl + filename
        fpath = mroot + filename

        if not os.path.isfile(fpath):
            samplelist = myutils.fieldexpand(query['results'])
            dataset =  internal.GetDataset(request, params={'queryname': [queryname], 'projectID': [query['project_id']], 'dataset': [datasetname], 'category': [category], 'method': [method]})
            with open(fpath, 'w') as f:
              f.write('sample,taxa,profile\n')
              #for d in dataset.order_by('sample'):
              # if d.profile > 0.1: f.write("%s,%s,%f\n" % ( d.sample, d.entity, d.profile ))

              taxahash = dict()
              samplemax = dict()
              datahash = dict()
              for d in dataset:
                if d.profile < 0.1: continue
                if d.entity not in taxahash or d.profile > taxahash[d.entity]: 
                  taxahash[d.entity] = d.profile
                if d.entity not in datahash: datahash[d.entity] = dict()
                if d.sample not in samplemax: 
                  samplemax[d.sample] = {'e': d.entity, 'p': d.profile}
                  datahash[d.entity][d.sample] = {d.entity: d.profile} 
                elif d.profile > samplemax[d.sample]['p']:
                  current = samplemax[d.sample]
                  samplemax[d.sample] = {'e': d.entity, 'p': d.profile}
                  sampledata = datahash[current['e']][d.sample]
                  del datahash[current['e']][d.sample]
                  sampledata[d.entity] = d.profile
                  datahash[samplemax[d.sample]['e']][d.sample] = sampledata
                else:
                  datahash[samplemax[d.sample]['e']][d.sample][d.entity] = d.profile
              for t, tmax in sorted(taxahash.items(), key=lambda x: -x[1]):
                for sample, data in sorted(datahash[t].items(), key= lambda x: -x[1][t]):
                  for entity, profile in sorted(data.items(), key=lambda x: x[1]):
                    f.write("%s,%s,%s\n" % (sample, entity, profile)) 
        return HttpResponse(json.dumps(furl), content_type="application/json")
    return render(request, 'stackedbars.html', params)
示例#4
0
文件: area.py 项目: iojas/OREAN
def main(request):
    params = {}
    params['time'] = []
    params['queries'] = internal.ListQueries(
        request, {'projectID': [request.session['projectID']]})
    if request.method == 'POST':
        start = time.time()
        queryname = request.POST.get('query') or None
        dataset = request.POST.get('dataset') or None
        method = request.POST.get('method') or None
        category = request.POST.get('category') or None
        if not queryname or not dataset or not method or not category:
            return render(request, 'profile.html', params)
        params['feedback'] = 'Query selected: "%s"' % queryname
        query = internal.ListQueries(
            request, {
                'projectID': [request.session['projectID']],
                'full': [True],
                'queryname': [queryname]
            })
        if not query or not len(query):
            return render(request, 'profile.html', params)
        else:
            query = query[0]
        samplelist = myutils.fieldexpand(query['results'])
        mark = time.time()
        dataset = internal.GetDataset(request,
                                      params={
                                          'queryname': [queryname],
                                          'projectID': [query['project_id']],
                                          'dataset': [dataset],
                                          'category': [category],
                                          'method': [method]
                                      })
        jsondata = []
        magichash = {}
        maxhash = {}
        sorterhash = {}
        entityorder = []
        first = True
        for d in dataset:
            if first:
                params['time'].append(
                    'parsed input and fetched API data: %.02f' %
                    (time.time() - start))
                first = False
            if d.profile < 1: continue
            if d.sample not in maxhash or d.profile > maxhash[d.sample]['val']:
                maxhash[d.sample] = {'entity': d.entity, 'val': d.profile}
            if d.entity in magichash: magichash[d.entity][d.sample] = d.profile
            else: magichash[d.entity] = {d.sample: d.profile}
        params['time'].append('computed max profile values: %.02f ' %
                              (time.time() - start))
        for sample in sorted(maxhash,
                             key=lambda x: maxhash[x]['val'],
                             reverse=True):
            if maxhash[sample]['val'] < 5: continue
            if maxhash[sample]['entity'] not in sorterhash:
                sorterhash[maxhash[sample]['entity']] = [sample]
                entityorder.append(maxhash[sample]['entity'])
            else:
                sorterhash[maxhash[sample]['entity']].append(sample)
        params['time'].append('sorted taxa: %.02f' % (time.time() - start))
        samplelist = []
        for element in entityorder:
            for x in sorterhash[element]:
                samplelist.append(x)
        params['time'].append('sorted samples: %.02f' % (time.time() - start))
        for m in magichash:
            tmp = {'name': m, 'data': []}
            for i, s in enumerate(samplelist):
                if s in magichash[m]: tmp['data'].append(magichash[m][s])
                else: tmp['data'].append(0)
            jsondata.append(tmp)
        params['time'].append('formatted data for JSON conversion: %.02f' %
                              (time.time() - start))
        params['json'] = json.dumps([samplelist, jsondata, params['time']])
        return HttpResponse(params['json'], content_type="application/json")
    return render(request, 'profile.html', params)
示例#5
0
def idea(request):
    from api.models import *
    from django.db.models import Max
    params = {}
    params['queries'] = internal.ListQueries(
        request, {'projectID': [request.session['projectID']]})
    if request.method == 'POST':
        queryname = request.POST.get('query') or None
        datasetname = request.POST.get('dataset') or None
        method = request.POST.get('method') or None
        category = request.POST.get('category') or None
        featurecount = request.POST.get('amount', 10)
        featurecount = int(featurecount) - 1
        if not queryname or not datasetname or not method or not category:
            return render(request, 'profile.html', params)
        query = internal.ListQueries(
            request, {
                'projectID': [request.session['projectID']],
                'full': [True],
                'queryname': [queryname]
            })
        if not query or not len(query):
            return render(request, 'profile.html', params)
        else:
            query = query[0]

        filename = md5('stackedbars.%d.%d.%s.%s.%s.%d' %
                       (query['id'], query['project_id'], datasetname, method,
                        category, featurecount)).hexdigest() + '.json'
        furl = murl + filename
        fpath = mroot + filename

        if not os.path.isfile(fpath):
            samplelist = myutils.fieldexpand(query['results'])
            toptaxa = Analysis.objects.filter(
                project=request.session['projectID'],
                dataset=datasetname,
                method=method,
                category=category,
                sample__in=samplelist).values('entity').annotate(
                    maxprofile=Max('profile')).distinct().order_by(
                        '-maxprofile')[:featurecount]
            taxadict = {}
            resp = [{'name': 'other', 'data': []}]
            for i, t in enumerate(toptaxa, start=1):
                taxadict[t['entity']] = {'loc': i, 'samples': []}
                resp.append({'name': t['entity'], 'data': []})
            rows = Analysis.objects.filter(
                project=request.session['projectID'],
                dataset=datasetname,
                method=method,
                category=category,
                sample__in=samplelist,
                entity__in=taxadict.keys()).order_by('-profile')
            sampleloc = dict()
            sampleorder = []
            for row in rows:
                if row.sample not in sampleloc:
                    sampleloc[row.sample] = {
                        'group': row.entity,
                        'loc': len(taxadict[row.entity]['samples']),
                        'other': 100.0
                    }
                    taxadict[row.entity]['samples'].append([{
                        'sample': row.sample,
                        'entity': row.entity,
                        'y': row.profile
                    }])
                else:
                    taxadict[sampleloc[row.sample]['group']]['samples'][
                        sampleloc[row.sample]['loc']].append({
                            'sample': row.sample,
                            'entity': row.entity,
                            'y': row.profile
                        })
                sampleloc[row.sample]['other'] = sampleloc[
                    row.sample]['other'] - row.profile
            ctr = 0
            for entitydict in toptaxa:
                entity = entitydict['entity']
                for samplelist in taxadict[entity]['samples']:
                    resp[0]['data'].append({
                        'x':
                        ctr,
                        'y':
                        sampleloc[samplelist[0]['sample']]['other']
                    })
                    sampleorder.append(samplelist[0]['sample'])
                    for sample in samplelist:
                        sample.pop("sample", None)
                        cur_entity = sample.pop("entity", None)
                        sample['x'] = ctr
                        resp[taxadict[cur_entity]['loc']]['data'].append(
                            sample)
                    ctr += 1
            with open(fpath, 'w') as outfile:
                json.dump([sampleorder, resp], outfile)
        return HttpResponse(json.dumps(furl), content_type="application/json")
    return render(request, 'ideastackedbars.html', params)
示例#6
0
def main(request):
    params = {}
    params['queries'] = internal.ListQueries(
        request, {'projectID': [request.session['projectID']]})
    if request.method == 'POST':
        start = time.time()
        queryname = request.POST.get('query') or None
        datasetname = request.POST.get('dataset') or None
        method = request.POST.get('method') or None
        category = request.POST.get('category') or None
        if not queryname or not datasetname or not method or not category:
            return render(request, 'profile.html', params)
        params['feedback'] = 'Query selected: "%s"' % queryname

        query = internal.ListQueries(
            request, {
                'projectID': [request.session['projectID']],
                'full': [True],
                'queryname': [queryname]
            })
        if not query or not len(query):
            return render(request, 'profile.html', params)
        else:
            query = query[0]
        filename = 'stackedbars.%d.%d.%s.%s.%s.csv' % (
            query['id'], query['project_id'], datasetname, method, category)
        furl = murl + filename
        fpath = mroot + filename

        if not os.path.isfile(fpath):
            samplelist = myutils.fieldexpand(query['results'])
            dataset = internal.GetDataset(request,
                                          params={
                                              'queryname': [queryname],
                                              'projectID':
                                              [query['project_id']],
                                              'dataset': [datasetname],
                                              'category': [category],
                                              'method': [method]
                                          })
            with open(fpath, 'w') as f:
                f.write('sample,taxa,profile\n')
                #for d in dataset.order_by('sample'):
                # if d.profile > 0.1: f.write("%s,%s,%f\n" % ( d.sample, d.entity, d.profile ))

                taxahash = dict()
                samplemax = dict()
                datahash = dict()
                for d in dataset:
                    if d.profile < 0.1: continue
                    if d.entity not in taxahash or d.profile > taxahash[
                            d.entity]:
                        taxahash[d.entity] = d.profile
                    if d.entity not in datahash: datahash[d.entity] = dict()
                    if d.sample not in samplemax:
                        samplemax[d.sample] = {'e': d.entity, 'p': d.profile}
                        datahash[d.entity][d.sample] = {d.entity: d.profile}
                    elif d.profile > samplemax[d.sample]['p']:
                        current = samplemax[d.sample]
                        samplemax[d.sample] = {'e': d.entity, 'p': d.profile}
                        sampledata = datahash[current['e']][d.sample]
                        del datahash[current['e']][d.sample]
                        sampledata[d.entity] = d.profile
                        datahash[samplemax[d.sample]['e']][
                            d.sample] = sampledata
                    else:
                        datahash[samplemax[d.sample]['e']][d.sample][
                            d.entity] = d.profile
                for t, tmax in sorted(taxahash.items(), key=lambda x: -x[1]):
                    for sample, data in sorted(datahash[t].items(),
                                               key=lambda x: -x[1][t]):
                        for entity, profile in sorted(data.items(),
                                                      key=lambda x: x[1]):
                            f.write("%s,%s,%s\n" % (sample, entity, profile))
        return HttpResponse(json.dumps(furl), content_type="application/json")
    return render(request, 'stackedbars.html', params)