def intersections_table(request, ratio=2., pvalue=0.05, fold_change=None, exp=None, set=None, benjamini=None): id = 'seq_id' ratio = float(ratio) pvalue = float(pvalue) if fold_change == "None": fold_change = None if exp == "None": exp = None if benjamini == "None": benjamini = None filter = TranscriptFilterSet(request.GET, transcripts) intersections = [] #signatures = Signature.objects.differential(ratio, pvalue) # if set: set = Set.objects.get(pk=set) signatures = set.signatures.all() else: set = Set.objects.get(pk=1) signatures = set.signatures.all() for signature in signatures: # signature.differential( ratio, pvalue, fold_change, exp, benjamini, id) # Might a good function for a custom manager. # Construct table: rows = [] row = ['Signature'] limit = len(signatures) for a_signature in signatures: row.append(a_signature.name) row_count = 0 rows.append("\t".join(row)) for row_number, a_signature in enumerate(signatures): row = [a_signature.name] for column_number, another_signature in enumerate(signatures): if column_number == row_number: intersection = '' elif column_number <= row_number: intersection = Intersection(a_signature, another_signature) intersection = "%s" % (pf(intersection.up_pvalue, 1)) #/%s #pf(intersection.diff_pvalue, 1), #)#pf(intersection.down_pvalue, 1) else: intersection = Intersection(a_signature, another_signature) intersection = "%s" % ( len(intersection.up) ) #) #/%s/%slen(intersection.diff),, len(intersection.down row.append(intersection) rows.append("\t".join(row)) table = "Table: Intersections\n~~~~~~~~~~~~~~~~~~~~~~\n" + "\n".join(rows) ctx = {'title': 'Intersections', 'table': table} return render(request, 'expressions/intersections_table.html', ctx)
def meta(request, ratio=2., pvalue=0.05, fold_change=None, exp=None, set=None, benjamini=None): """Common to all signature in a category.""" terms = False id = 'seq_id' if request.GET: if 'ratio' in request.GET and request.GET['ratio']: ratio = float(request.GET['ratio']) if 'pvalue' in request.GET and request.GET['pvalue']: pvalue = float(request.GET['pvalue']) if 'benjamini' in request.GET and request.GET['benjamini']: benjamini = float(request.GET['benjamini']) if 'fold_change' in request.GET and request.GET['fold_change']: fold_change = float(request.GET['fold_change']) if 'expression__exp' in request.GET and request.GET['expression__exp']: exp = float(request.GET['expression__exp']) if 'set' in request.GET and request.GET['set']: set = request.GET['set'] if 'terms' in request.GET and request.GET['terms']: terms = True if 'id' in request.GET['id'] and request.GET['id']: id = request.GET['id'] entry = get("Meta-Analysis") set = Set.objects.get(pk=set or 1) signatures = set.signatures.all() for signature in signatures: signature.differential(ratio, pvalue, fold_change, exp, benjamini, id) signatures = Signatures(signatures) filter = TranscriptFilterSet(request.GET, transcripts) if terms: table_up = functional_enrichment(terms, signatures.up, id) table_down = functional_enrichment(terms, signatures.down, id) #table_diff = functional_enrichment(terms, list(chain(signatures.up, signatures.down, id))) table_diff = None else: table_up = table_down = table_diff = None ctx = { 'title': 'Meta-Analysis', 'entry': entry, 'signatures': signatures, 'filter': filter, 'sets': Set.objects.all(), 'terms': terms, 'table_up': table_up, 'table_down': table_down, 'table_diff': table_diff, 'ids': IDs, 'id': id, } return render_to_response('expressions/meta.html', ctx, context_instance=RequestContext(request))
def transcript_list(request): f = TranscriptFilterSet(request.GET, queryset=Transcript.objects.all()) return render_to_response('expressions/transcripts.html', {'filter': f}, context_instance=RequestContext(request))
def transcripts(request): filterset = TranscriptFilterSet(request.GET or None) return render_to_response('expressions/transcripts.html', {'filterset': filterset}, context_instance=RequestContext(request))
def intersection(request, a, another, ratio=2., pvalue=0.05, fold_change=None, exp=None, benjamini=None): terms = None id = 'seq_id' a_signature = Signature.objects.get(pk=a) another_signature = Signature.objects.get(pk=another) if request.GET: if 'ratio' in request.GET and request.GET['ratio']: ratio = request.GET['ratio'] if 'pvalue' in request.GET and request.GET['pvalue']: pvalue = request.GET['pvalue'] if 'benjamini' in request.GET and request.GET['benjamini']: benjamini = request.GET['benjamini'] if 'fold_change' in request.GET and request.GET['fold_change']: fold_change = request.GET['fold_change'] print "get fold_change", fold_change if 'expression__exp' in request.GET and request.GET['expression__exp']: exp = float(request.GET['expression__exp']) if 'id' in request.GET and request.GET['id']: id = request.GET['id'] if 'terms' in request.GET and request.GET['terms']: terms = True else: terms = False filter = TranscriptFilterSet(request.GET, transcripts) if fold_change == 'None': fold_change = None #print fold_change a_signature.differential(float(ratio), float(pvalue), float(fold_change or 0), exp, benjamini, id) another_signature.differential(float(ratio), float(pvalue), float(fold_change or 0), exp, benjamini, id) intersection = Intersection(a_signature, another_signature) table_up = functional_enrichment(terms, intersection.upregulated(), id) table_down = functional_enrichment(terms, intersection.downregulated(), id) ctx = { 'title': '%s & %s' % (a_signature, another_signature), 'intersection': intersection, 'a_signature': a_signature, 'another_signature': another_signature, 'filer': filter, 'terms': terms, 'table_up': table_up, 'table_down': table_down, 'ids': IDs, 'id': id, } return render_to_response('expressions/intersection.html/', ctx, context_instance=RequestContext(request))
def intersections(request, ratio=2., pvalue=0.05, fold_change=None, exp=None, set=None, benjamini=None): entry = get("Intersections") id = 'seq_id' if request.GET: if 'ratio' in request.GET and request.GET['ratio']: ratio = float(request.GET['ratio']) if 'pvalue' in request.GET and request.GET['pvalue']: pvalue = float(request.GET['pvalue']) if 'fold_change' in request.GET and request.GET['fold_change']: fold_change = float(request.GET['fold_change']) if 'benjamini' in request.GET and request.GET['benjamini']: benjamini = float(request.GET['benjamini']) if 'expression__exp' in request.GET and request.GET['expression__exp']: exp = request.GET['expression__exp'] print "Expression exp" if 'set' in request.GET and request.GET['set']: set = request.GET['set'] if 'id' in request.GET and request.GET['id']: id = request.GET['id'] filter = TranscriptFilterSet(request.GET, transcripts) intersections = [] #signatures = Signature.objects.differential(ratio, pvalue) # if set: set = Set.objects.get(pk=set) signatures = set.signatures.all() else: set = Set.objects.get(pk=1) signatures = set.signatures.all() for signature in signatures: # signature.differential( ratio, pvalue, fold_change, exp, benjamini, id) # Might a good function for a custom manager. for a_signature in signatures: for another_signature in signatures[:len( signatures )]: # Prevents duplicated comparisions the other way around. if a_signature != another_signature: # Prevent comparision of the same signatures and intersection = Intersection(a_signature, another_signature) #print intersection.name, len(intersection.up), len(intersection.down) intersections.append(intersection) # Context: ctx = { 'title': 'Intersections', 'entry': entry, 'signatures': signatures, 'intersections': intersections, 'ratio': ratio, 'pvalue': pvalue, 'benjamini': benjamini, 'fold_change': fold_change, 'exp': exp, 'filter': filter, 'sets': Set.objects.all(), 'ids': IDs, 'id': id, 'set': set, } return render_to_response('expressions/intersections.html', ctx, context_instance=RequestContext(request))
def signature(request, pk=None, ratio=2., pvalue=0.05, fold_change=None, exp=None, benjamini=None, name=None): terms = False id = 'seq_id' term = "factors" if request.GET: if 'ratio' in request.GET and request.GET['ratio']: ratio = float(request.GET['ratio']) if 'pvalue' in request.GET and request.GET['pvalue']: pvalue = float(request.GET['pvalue']) if 'benjamini' in request.GET and request.GET['benjamini']: benjamini = float(request.GET and request.GET['benjamini']) if 'expression__exp' in request.GET and request.GET['expression__exp']: exp = float(request.GET['expression__exp']) if 'id' in request.GET and request.GET['id']: id = request.GET['id'] if 'terms' in request.GET and request.GET['terms']: terms = True else: terms = False if 'term' in request.GET and request.GET['term']: term = request.GET['term'] else: term = "factors" elif 'term' in request.POST: term = request.POST['term'] print(term) if pk: signature = Signature.objects.get(pk=pk) elif name: signature = Signature.objects.get(name=name) if not exp: transcripts = signature.transcripts.filter((Q(ratio__gt=ratio) | Q(ratio__lt=1. / ratio)) & Q(pvalue__lt=pvalue)) else: # What is about a gene that is only low expressed in one condition? #print "Filtering on expression:" transcripts = signature.transcripts.filter( (Q(ratio__gt=ratio) | Q(ratio__lt=1. / ratio)) & Q(pvalue__lt=pvalue #& Q(expression__exp__gt=exp) & Q(expression__ctr__gt=exp) )) transcripts = transcripts.filter(expression__exp__gt=exp, expression__ctr__gt=exp) if request.GET: if 'symbol' in request.GET and request.GET['symbol']: symbol = request.GET['symbol'] transcripts = transcripts.filter(symbol__icontains=symbol) if 'fold_change' in request.GET and request.GET['fold_change']: fold_change = float(request.GET['fold_change']) #print len(transcripts) transcripts = transcripts.filter( Q(fold_change__gt=fold_change) | Q(fold_change__lt=1. / fold_change)) #print "filtered transcripts", len(transcripts) if 'benjamini' in request.GET and request.GET['benjamini']: benjamini = float(request.GET['benjamini']) transcripts = transcripts.filter(benjamini__lt=benjamini) filter = TranscriptFilterSet(request.GET, transcripts) #print type(filter), vars(filter) #print len(filter.queryset) table = TranscriptTable(filter.queryset) RequestConfig(request).configure(table) transcripts_up = transcripts.filter( Q(ratio__gt=ratio) & Q(pvalue__lt=pvalue)) transcripts_down = transcripts.filter( Q(ratio__lt=1. / ratio) & Q(pvalue__lt=pvalue)) table_up = functional_enrichment(terms, transcripts_up) table_down = functional_enrichment(terms, transcripts_down) #table_diff = functional_enrichment(terms, list(chain(transcripts_up, transcripts_down))) table_diff = None #print id transcripts_up = set( [getattr(transcript, id) for transcript in transcripts_up]) if None in transcripts_up: transcripts_up.remove(None) transcripts_down = set( [getattr(transcript, id) for transcript in transcripts_down]) if None in transcripts_down: transcripts_down.remove(None) #transcripts_diff = set([getattr(transcripts, id) for transcript in transcripts_diff]) #if None in transcripts_diff: transcripts_diff.remove(None) ctx = { 'signature': signature, 'transcripts': transcripts, 'transcripts_up': transcripts_up, 'transcripts_down': transcripts_down, #'transcripts_diff': transcripts_diff, 'table': table, 'ratio': ratio, 'pvalue': pvalue, 'benjamini': benjamini, 'filter': filter, 'terms': terms, 'table_up': table_up, 'table_down': table_down, 'table_diff': table_diff, 'ids': IDs, 'id': id, 'term': term # GO term for go_filter } return render_to_response('expressions/signature.html', ctx, context_instance=RequestContext(request))