def calc_pca_consistent(request) : if request.is_ajax() : # pdb.set_trace() response_data = {} pca_res = [] comps = [] # for cluster_id in [request.GET["cluster_ids"]] : cg = CompoundCollection.objects().with_id(str(request.POST['collection_id'])) centriods = Cluster.get_clusters_centriod(compound_group=cg) clusters = Cluster.objects(id__in = [request.GET["cluster_ids"]]) comps.extend(centriods) comps.extend([c.nodes for c in clusters]) pca = PCARes(collection=comps , title="PCA_on_clusters") response_data['clusters'] = [{"nodes" : [node.name for node in cluster.nodes] ,"id":str(cluster.id) ,"centriod" : cluster.centriod.name ,"density" :len(cluster.nodes)} for cluster in Clusters] response_data['pca_res'] = pca.PerformPCA().tolist() response_data['nodes'] = [{"name" : comp.name} for c in clusters for comp in c.nodes ] pca.save() #returen PCA Results response_data['result'] = 'Success' response_data['message'] = 'PCA Results' return HttpResponse(json.dumps(response_data), mimetype="application/json")
def get_collection_mdsres(request): if request.is_ajax(): collection = CompoundCollection.objects().with_id(request.GET["collection_id"]) response_data = {} response_data['result'] = 'Success' response_data['message'] = 'Collection Mds Results' #response_data['field_names'] = [] #for f in field_names : # response_data['field_names'].append(f) sim_matrix = SimilarityMatrix.objects(compound_group = collection) mds_res_list = [] response_data['mdsresults'] = [] response_data['pcaresults'] = [] for sm in sim_matrix : mds_res = MDSRes.objects(simmatrix = sm) for res in mds_res : mds_res_list.append({"title" : res.title , "id" : str(res.id)}) # add PCA Pos Res : pca_res_list =[] for pca_res in PCARes.objects(compound_group = collection) : pca_res_list.append({"title":pca_res.title, "id" : str(pca_res.id)}) response_data['mdsresults'] = mds_res_list response_data['pcaresults'] = pca_res_list return HttpResponse(json.dumps(response_data), mimetype="application/json")
def calc_mdsres_2d(request): if request.method == 'POST': # Calculate The Similarity Matrix #Calculate Finger Prints simmatrix_method = str(request.POST["simmatrix_method"]) cg = CompoundCollection.objects().with_id(str(request.POST['collection_id'])) if simmatrix_method == "FP" : print "\n Calculating Simmatrix (FingerPrints) ... \n " sm = SimilarityMatrix(compound_group = cg , method=simmatrix_method) sm.distance_matrix(0.0) elif simmatrix_method == "eden" : print "\n Calculating Simmatrix (EDeN) ...\n " sm = SimilarityMatrix(compound_group = cg,method=simmatrix_method) sm.distance_matrix_eden() # Calculate MDS print "\n Calculating MDS ... \n " res = MDSRes(title = str(request.POST['mds_title']),simmatrix = sm,max_iter =300,eps = 1e-6) ; data = numpy.fromstring(sm.data.read()) d = math.sqrt(len(data)) datamd = numpy.reshape(data, (d,d)) res.data.new_file() res.data.write(res.runMDS(simmatrix = datamd,n_comp=2).tostring()) res.data.close() res.save() response_data = {} response_data['result'] = 'Success' response_data['message'] = 'Calculate Mds Results' return HttpResponseRedirect('/space_explorer/')
def calc_clusters(request): if request.method == 'POST': form = CalcClusterForm(request.POST) if form.is_valid(): compound_group = CompoundCollection.objects().with_id( str(request.POST["collection_id"])) compounds = Compound.objects( compound_group=str(request.POST["collection_id"])) #input_text = "<(" #for compound in compounds : # input_text += compound.mol + "\n" #input_text += ")" input_text = "/home/mjs/Test/approved.gspan.gz" Cluster.calcualte_clusters(compound_group, input_text, "EDEN") # handle_uploaded_file(request.FILES['file']) return HttpResponseRedirect('/cluster/') else: form = CalcClusterForm() return render_to_response( 'calc_clusters.html', { "view_titel": "Calculate Clusters", "form": form }, context_instance=RequestContext(request)) # Create your views here.
def get_coordinate(request): if request.is_ajax(): # pdb.set_trace() mdsres = MDSRes.objects().with_id(request.session.get("mds_id")) Clusters = [] compound_group = CompoundCollection.objects().with_id(request.session.get("collection_id")) if(mdsres is not None) : data = numpy.fromstring(mdsres.data.read()) d = len(data) datamd = numpy.reshape(data, (d/3,3)) compounds = Compound.objects(compound_group = request.session.get("collection_id")) if mdsres.simmatrix.method == "FP" : arena = chemfp.load_fingerprints(mdsres.simmatrix.compound_group.fp_file) compounds = arena.ids elif mdsres.simmatrix.method == "eden" : compounds = [compound.name for compound in compounds] elif mdsres.simmatrix.method == "pre_cluster_eden" or mdsres.simmatrix.method == "pre_cluster_eden_pca" : Clusters = Cluster.objects(collection = request.session.get("collection_id")) Clusters = [{"nodes" : [node.name for node in cluster.nodes] ,"id":str(cluster.id) ,"centriod" : cluster.centriod.name ,"density" :len(cluster.nodes)} for cluster in Clusters] compounds = Cluster.get_clusters_centriod(compound_group) compounds = [compound.name for compound in compounds] elif(mdsres is None) : pcares = PCARes.objects().with_id(request.session.get("mds_id")) data = numpy.fromstring(pcares.data.read()) d = len(data) datamd = numpy.reshape(data, (d/3,3)) Clusters = Cluster.objects(collection = request.session.get("collection_id")) Clusters = [{"nodes" : [node.name for node in cluster.nodes] ,"id":str(cluster.id) ,"centriod" : cluster.centriod.name ,"density" :len(cluster.nodes)} for cluster in Clusters] compounds = Cluster.get_clusters_centriod(compound_group) compounds = [compound.name for compound in compounds] # the order of the compounds is not correct !!! make sure to fix (arena.ids[idx] from the fingerprint file) response_data = {} response_data['result'] = 'Success' response_data['message'] = 'Compounds Coordinate' response_data['coord'] = datamd.tolist() response_data["clusters"] = Clusters response_data['comps'] = [] for compound in compounds : compitem = { "name" :compound } response_data['comps'].append(compitem) return HttpResponse(json.dumps(response_data), mimetype="application/json") return HttpResponse({}, mimetype="application/json")
def view_space(request): if request.method == 'POST': return HttpResponseRedirect('/space_explorer/') cc = CompoundCollection.objects() cc_list = [ {"name":collection.title,"id":collection.id} for collection in cc ] c = { "view_titel":"Space Explorer" , "collections":cc_list , "current_collction" :request.session.get("collection_id") , "current_mds" :request.session.get("mds_id") } c.update(csrf(request)) return render_to_response('explorer.html',c,context_instance=RequestContext(request))
def add_meta(request): if request.method == 'POST': comp_collec = CompoundCollection.objects().with_id(request.session["collection_id"]) comp_collec.add_meta_info(request.FILES["metainfo_file"],int(request.POST["key_col"])) response_data = {} response_data['result'] = 'Success' response_data['message'] = 'Compounds Fields' return redirect('/space_explorer/') return HttpResponse(json.dumps(""), mimetype="application/json")
def calc_pca(request) : if request.is_ajax() : if(str(request.GET["consistent"]) == 'true'): # pdb.set_trace() response_data = {} pca_res = [] comps = [] clusters_nodes = [] # for cluster_id in [request.GET["cluster_ids"]] : cg = CompoundCollection.objects().with_id(str(request.session['collection_id'])) centriods = Cluster.get_clusters_centriod(compound_group=cg) clusters = Cluster.objects(id__in = request.GET.getlist('embededClusters[]')) clusters_nodes = [comp for c in clusters for comp in c.nodes ] comps.extend(centriods) comps.extend(node for node in clusters_nodes if node not in centriods) pca = PCARes(cluster=clusters , title="PCA_on_clusters") response_data['pca_res'] = pca.PerformPCA(collection=comps).tolist() response_data['nodes'] = [{"name" : c.name} for c in comps] # response_data['nodes'] = [{"name" : comp.name} for c in clusters for comp in c.nodes ] pca.save() #returen PCA Results response_data['result'] = 'Success' response_data['message'] = 'PCA Results' else : response_data = {} pca_res = [] # for cluster_id in [request.GET["cluster_ids"]] : cluster = Cluster.objects(id__in = [request.GET["cluster_ids"]]) pca = PCARes(cluster =cluster, title="PCA_on_clusters") response_data['pca_res'] = pca.PerformPCA().tolist() pca.save() response_data['nodes'] = [{"name" : comp.name} for c in cluster for comp in c.nodes ] #returen PCA Results response_data['result'] = 'Success' response_data['message'] = 'PCA Results' return HttpResponse(json.dumps(response_data), mimetype="application/json")
def calc_similarity(request): if request.method == 'POST': form = CalcSimForm(request.POST, request.FILES) if form.is_valid(): f = request.FILES['fingerprint_file'] cg = CompoundCollection.objects().with_id(str(request.POST['compound_group'])) sm = SimilarityMatrix(compound_group = cg ,fp = f) arrstr = sm.distance_matrix(f,0.0).tostring() sm.data.new_file() sm.data.write(arrstr) sm.data.close() sm.save(validate=False,cascade=False) return HttpResponseRedirect('/similarity/') else: form = CalcSimForm() return render_to_response('calc_similarity.html',{"view_titel":"Calculate SimilartyMatrix","form":form},context_instance=RequestContext(request))
def calc_clusters(request): if request.method == 'POST': form = CalcClusterForm(request.POST) if form.is_valid(): compound_group = CompoundCollection.objects().with_id(str(request.POST["collection_id"])) compounds = Compound.objects(compound_group = str(request.POST["collection_id"])) #input_text = "<(" #for compound in compounds : # input_text += compound.mol + "\n" #input_text += ")" input_text = "/home/mjs/Test/approved.gspan.gz" Cluster.calcualte_clusters(compound_group,input_text,"EDEN") # handle_uploaded_file(request.FILES['file']) return HttpResponseRedirect('/cluster/') else: form = CalcClusterForm() return render_to_response('calc_clusters.html',{"view_titel":"Calculate Clusters","form":form},context_instance=RequestContext(request)) # Create your views here.
def calc_similarity(request): if request.method == 'POST': form = CalcSimForm(request.POST, request.FILES) if form.is_valid(): f = request.FILES['fingerprint_file'] cg = CompoundCollection.objects().with_id( str(request.POST['compound_group'])) sm = SimilarityMatrix(compound_group=cg, fp=f) arrstr = sm.distance_matrix(f, 0.0).tostring() sm.data.new_file() sm.data.write(arrstr) sm.data.close() sm.save(validate=False, cascade=False) return HttpResponseRedirect('/similarity/') else: form = CalcSimForm() return render_to_response('calc_similarity.html', { "view_titel": "Calculate SimilartyMatrix", "form": form }, context_instance=RequestContext(request))
def calc_pos(request): if request.method == 'POST': # Calculate The Similarity Matrix #Calculate Finger Prints calc_method = str(request.POST["method"]) cg = CompoundCollection.objects().with_id(str(request.POST['collection_id'])) if calc_method == "FP" : print "\n Calculating Simmatrix (FingerPrints) ... \n " sm = SimilarityMatrix(compound_group = cg , method=calc_method) sm.distance_matrix(0.0) elif calc_method == "eden" : print "\n Calculating Simmatrix (EDeN) ...\n " sm = SimilarityMatrix(compound_group = cg,method=calc_method) sm.distance_matrix_eden() elif calc_method == "pre_cluster_eden" : Cluster.calculate_clusters_eden(cg) sm = SimilarityMatrix(compound_group = cg,method=calc_method) sm.distance_matrix_cluster_centriods() if calc_method == "pre_cluster_eden_pca" : # Calculate PCA if(len(Cluster.objects(collection = cg))<1): print "\n Calculating Clusters ... \n " Cluster.calculate_clusters_eden(cg) # Calculate and Store KNN compounds = Compound.objects(compound_group=cg) tmpsdffile = CompoundCollection.generateTmpSDF(compounds,"comps_eden") print "\n Calculating KNN ... \n" knn = EdenUtil.nearest_neighbor(EdenUtil.sdf_to_gspan(tmpsdffile.name)) for knn_entry in knn : knn_item = KnnItem( compound_group = cg ,mol_id = str(compounds[int(knn_entry['indxs'].pop(0))].name) ,neighbors = [ { "mol_id":str(compounds[int(x)].name) ,"val":float(knn_entry['knn_vals'][knn_entry['indxs'].index(x)]) } for x in knn_entry['indxs'] ] ) knn_item.save() print "\n Calculating PCA ... \n " pca = PCARes(compound_group=cg, title=str(request.POST['pos_title'])) res = pca.PerformPCA(collection=Cluster.get_clusters_centriod(compound_group=cg)).tostring() pca.data.new_file() pca.data.write(res) pca.data.close() pca.save() else : # Calculate MDS print "\n Calculating MDS ... \n " res = MDSRes(title = str(request.POST['pos_title']),simmatrix = sm,max_iter =300,eps = 1e-6) ; data = numpy.fromstring(sm.data.read()) d = math.sqrt(len(data)) datamd = numpy.reshape(data, (d,d)) res.data.new_file() res.data.write(res.runMDS(simmatrix = datamd).tostring()) res.data.close() res.save() response_data = {} response_data['result'] = 'Success' response_data['message'] = 'Calculate Position Results' return HttpResponseRedirect('/space_explorer/')
def test_add_meta(request): CC = CompoundCollection.objects().with_id(request.session["collection_id"]) CC.add_meta_info() compounds = Compound.objects() return render_to_response('view_file.html',{"view_titel":"Home",'compounds':compounds},context_instance=RequestContext(request))