Beispiel #1
0
def main(request):
    CUTOFF=20
    params = {}
    params['queries'] = internal.ListQueries(request, {'projectID': [request.session['projectID']]})
    querynames = request.GET.getlist('query') or None
    querynames = request.GET.getlist('query') or None
    inputdataset = request.GET.get('dataset') or None
    category = request.GET.get('category') or None
    method = request.GET.get('method') or None
    print querynames
    if querynames:
        if not querynames or not inputdataset or not method or not category: return render(request, 'alpha.html', params)
        #entities = []
        entities = myutils.topTaxa(request.session['projectID'], querynames, inputdataset, method, category, CUTOFF)
        elementOfGlory = {en: {q:{} for q in querynames} for en in entities}
        for query in querynames:
            filename = "/tmp/%s-%d.txt" %(request.user.pk, int(time.time()))
            datahash = {}
            for ent in entities: datahash[ent] = []
            longest_list = -1
            print "Query:", query
            dataset = internal.GetDataset(request, params={'queryname': [query], 'projectID': [request.session['projectID']], 'dataset': [inputdataset], 'category': [category], 'method': [method], 'entity': entities})
            for d in dataset:
                taxa = d.entity
                datahash[taxa].append(d.profile)
                if len(datahash[taxa]) > longest_list: longest_list = len(datahash[taxa])
            with open(filename, 'a') as f:
                for taxa in datahash: 
                    f.write(str(taxa))
                    count = 0
                    for profile in datahash[taxa]: 
                        count +=1
                        f.write(','+str(profile))
                    elementOfGlory[taxa][query]['n'] = str(count)
                    while count < longest_list: 
                        f.write(','+str('0'))
                        count+=1
                    f.write('\n')
            boxplot = myutils.runRscript('16sProfilesBoxplot.R', filename)
            os.remove(filename)
            tmp = boxplot.split('\n')
            boxplot = []
            for bp in tmp: 
              if bp == "": continue
              tmp2 = bp.split(',')
              tmp2 = [float(s) if isfloat(s) else s for s in tmp2]
              if tmp2[0] in elementOfGlory: elementOfGlory[tmp2[0]][query]['stats'] = tmp2 
              else: elementOfGlory[tmp2[0]] = {query : {'stats': tmp2}}
        finaldata = [ [], [], [], len(querynames) ]
        i = 0
        for en in entities:
          for query in querynames:
            bp = elementOfGlory[en][query]['stats']
            bp.pop(0)
            if len(bp[5:]): [finaldata[2].append([i,float(x)]) for x in bp[5:] if x != '']
            finaldata[0].append(en+' ('+elementOfGlory[en][query].get('n', '?')+')')
            finaldata[1].append(bp[:5])
            i+=1  
        return HttpResponse(json.dumps(finaldata), content_type="application/json")
    return render(request, '16sProfileBoxplot.html', params)
Beispiel #2
0
def alpha(request):
    params = {}
    params['queries'] = internal.ListQueries(request, {'projectID': [request.session['projectID']]})
    querynames = 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
    if querynames:
        if not querynames or not inputdataset or not method or not category: return render(request, 'alpha.html', params)
        datapoints = []
        outlierpoints = []
        count = 0
        for query in querynames:
            entities = []
            datahash = {}
            dataset =  internal.GetDataset(request, params={'queryname': [query], 'projectID': [request.session['projectID']], 'dataset': [inputdataset], 'category': [category], 'method': [method]})
            for d in dataset:
                if d.entity not in entities:
                    entities.append(d.entity)
                if d.sample not in datahash: datahash[d.sample] = {}
                datahash[d.sample][d.entity] = d.numreads
            filename = "/tmp/%s-%d.txt" %(request.user.username, int(time.time()))
            with open(filename, 'w') as f:
                for taxa in entities: f.write(','+str(taxa))
                f.write('\n')
                for sample in datahash:
                    mycontent = sample
                    for taxa in entities:
                        mycontent+=','
                        if taxa in datahash[sample]: mycontent+=str(datahash[sample][taxa])
                        else: mycontent += str('0')
                    mycontent+='\n'
                    f.write(mycontent)
            boxplot = myutils.runRscript('alphaDiversity.R', filename)
            boxplot,outliers = boxplot.split('\n')[:2]
            boxplot = boxplot.split(',')
            outliers = outliers.split(',')
            if len(boxplot): boxplot = [float(x) for x in boxplot]
            if len(outliers): outliers = [[count,float(x)] for x in outliers if x != '']
            os.remove(filename)
            datapoints.append(boxplot)
            outlierpoints.append(outliers)
            count+=1
        finaldata = [querynames, datapoints, outlierpoints]
        return HttpResponse(json.dumps(finaldata), content_type="application/json")
    return render(request, 'alpha.html', params)
Beispiel #3
0
def main(request):
    params = {}
    params['queries'] = internal.ListQueries(request, {'projectID': [request.session['projectID']]})
    querynames = 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
    if querynames:
        if not querynames or not inputdataset or not method or not category: return render(request, 'alpha.html', params)
        entities = []
        samples = []
        datahash = {}
        filename = "/tmp/%s-%d.txt" %(request.user.username, int(time.time()))
        for query in querynames:
            dataset =  internal.GetDataset(request, params={'queryname': [query], 'projectID': [request.session['projectID']], 'dataset': [inputdataset], 'category': [category], 'method': [method]})
            for d in dataset:
                if d.entity not in entities:
                    entities.append(d.entity)
                if d.sample not in samples:
                    samples.append(d.sample)
                    datahash[d.sample] = {'query' : [query]}
                datahash[d.sample][d.entity] = d.profile
                if query not in datahash[d.sample]['query']: datahash[d.sample]['query'].append(query)
        with open(filename, 'w') as f:
            f.write('Rank,Taxa,')
            for sample in samples: f.write(str(sample)+',')
            f.write('\n')
            for entity in entities:
                mycontent = category+','+entity+','
                for sample in samples:
                    if entity in datahash[sample]: mycontent+=str(datahash[sample][entity])
                    else: mycontent += str('0')
		    mycontent+=','
                mycontent+='\n'
                f.write(mycontent)
        pca = myutils.runRscript('pca.R', filename)
        pca = pca.split('\n')
        pca.pop(0) 
        pca.pop() 
        finaldata = [{'name' : querynames[0], 'color': '#ff0000', 'data': []}, {'name': querynames[1], 'color': '#0000ff', 'data': []}, {'name': 'Both', 'color': '#00ff00', 'data' : []}]
        i = 0
        for i in range(len(pca)):
            row = pca[i]
            if row.startswith('-----'): break
            cols = row.split(',')
            sample = cols[0]
            if sample[0] == 'X' and sample not in datahash: sample = sample[1:]
            xy = [float(cols[1]), float(cols[2])]
            if len(datahash[sample]['query']) > 1: finaldata[2]['data'].append(xy)
            elif querynames[0] in datahash[sample]['query']: finaldata[0]['data'].append(xy)
            else: finaldata[1]['data'].append(xy)
        i+=1
        cutoff = i
        variances = []
        for i in range(cutoff, len(pca)):
            row = pca[i]
            if row.startswith('-----'): break
            cols = row.split(',')	
            variances.append(cols)
        i+=1
        cutoff = i
        finaldata.append(variances)
        keytaxa = []
        for i in range(cutoff, len(pca)):
            row = pca[i]
            cols = row.split(',')       
            keytaxa.append(cols)
        finaldata.append(keytaxa)
        #os.remove(filename)
        return HttpResponse(json.dumps(finaldata), content_type="application/json")
    return render(request, 'pca.html', params)
Beispiel #4
0
def main(request):
    CUTOFF = 20
    params = {}
    params['queries'] = internal.ListQueries(
        request, {'projectID': [request.session['projectID']]})
    querynames = request.GET.getlist('query') or None
    querynames = request.GET.getlist('query') or None
    inputdataset = request.GET.get('dataset') or None
    category = request.GET.get('category') or None
    method = request.GET.get('method') or None
    print querynames
    if querynames:
        if not querynames or not inputdataset or not method or not category:
            return render(request, 'alpha.html', params)
        #entities = []
        entities = myutils.topTaxa(request.session['projectID'], querynames,
                                   inputdataset, method, category, CUTOFF)
        elementOfGlory = {en: {q: {} for q in querynames} for en in entities}
        for query in querynames:
            filename = "/tmp/%s-%d.txt" % (request.user.pk, int(time.time()))
            datahash = {}
            for ent in entities:
                datahash[ent] = []
            longest_list = -1
            print "Query:", query
            dataset = internal.GetDataset(request,
                                          params={
                                              'queryname': [query],
                                              'projectID':
                                              [request.session['projectID']],
                                              'dataset': [inputdataset],
                                              'category': [category],
                                              'method': [method],
                                              'entity':
                                              entities
                                          })
            for d in dataset:
                taxa = d.entity
                datahash[taxa].append(d.profile)
                if len(datahash[taxa]) > longest_list:
                    longest_list = len(datahash[taxa])
            with open(filename, 'a') as f:
                for taxa in datahash:
                    f.write(str(taxa))
                    count = 0
                    for profile in datahash[taxa]:
                        count += 1
                        f.write(',' + str(profile))
                    elementOfGlory[taxa][query]['n'] = str(count)
                    while count < longest_list:
                        f.write(',' + str('0'))
                        count += 1
                    f.write('\n')
            boxplot = myutils.runRscript('16sProfilesBoxplot.R', filename)
            os.remove(filename)
            tmp = boxplot.split('\n')
            boxplot = []
            for bp in tmp:
                if bp == "": continue
                tmp2 = bp.split(',')
                tmp2 = [float(s) if isfloat(s) else s for s in tmp2]
                if tmp2[0] in elementOfGlory:
                    elementOfGlory[tmp2[0]][query]['stats'] = tmp2
                else:
                    elementOfGlory[tmp2[0]] = {query: {'stats': tmp2}}
        finaldata = [[], [], [], len(querynames)]
        i = 0
        for en in entities:
            for query in querynames:
                bp = elementOfGlory[en][query]['stats']
                bp.pop(0)
                if len(bp[5:]):
                    [
                        finaldata[2].append([i, float(x)]) for x in bp[5:]
                        if x != ''
                    ]
                finaldata[0].append(en + ' (' +
                                    elementOfGlory[en][query].get('n', '?') +
                                    ')')
                finaldata[1].append(bp[:5])
                i += 1
        return HttpResponse(json.dumps(finaldata),
                            content_type="application/json")
    return render(request, '16sProfileBoxplot.html', params)
Beispiel #5
0
def alpha(request):
    params = {}
    params['queries'] = internal.ListQueries(
        request, {'projectID': [request.session['projectID']]})
    querynames = 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
    if querynames:
        if not querynames or not inputdataset or not method or not category:
            return render(request, 'alpha.html', params)
        datapoints = []
        outlierpoints = []
        count = 0
        for query in querynames:
            entities = []
            datahash = {}
            dataset = internal.GetDataset(request,
                                          params={
                                              'queryname': [query],
                                              'projectID':
                                              [request.session['projectID']],
                                              'dataset': [inputdataset],
                                              'category': [category],
                                              'method': [method]
                                          })
            for d in dataset:
                if d.entity not in entities:
                    entities.append(d.entity)
                if d.sample not in datahash: datahash[d.sample] = {}
                datahash[d.sample][d.entity] = d.numreads
            filename = "/tmp/%s-%d.txt" % (request.user.username,
                                           int(time.time()))
            with open(filename, 'w') as f:
                for taxa in entities:
                    f.write(',' + str(taxa))
                f.write('\n')
                for sample in datahash:
                    mycontent = sample
                    for taxa in entities:
                        mycontent += ','
                        if taxa in datahash[sample]:
                            mycontent += str(datahash[sample][taxa])
                        else:
                            mycontent += str('0')
                    mycontent += '\n'
                    f.write(mycontent)
            boxplot = myutils.runRscript('alphaDiversity.R', filename)
            boxplot, outliers = boxplot.split('\n')[:2]
            boxplot = boxplot.split(',')
            outliers = outliers.split(',')
            if len(boxplot): boxplot = [float(x) for x in boxplot]
            if len(outliers):
                outliers = [[count, float(x)] for x in outliers if x != '']
            os.remove(filename)
            datapoints.append(boxplot)
            outlierpoints.append(outliers)
            count += 1
        finaldata = [querynames, datapoints, outlierpoints]
        return HttpResponse(json.dumps(finaldata),
                            content_type="application/json")
    return render(request, 'alpha.html', params)