示例#1
0
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/')
示例#2
0
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")
示例#3
0
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")
示例#4
0
    def calculate_clusters_eden(collection):
        compounds = Compound.objects(compound_group=collection)

        # call EdenUtil Cluster
        clusters_file = EdenUtil.cluster(
            EdenUtil.sdf_to_gspan(
                CompoundCollection.generateTmpSDF(compounds,
                                                  'tmpcollection').name))

        clusters = []
        # create a cluster for each line
        for eden_cluster in clusters_file:
            eden_nodes = eden_cluster.split(" ")
            eden_nodes = [x for x in eden_nodes if (x != '' and x != '\n')]

            cluster = Cluster(
                title="Cluster_" + str(eden_nodes[0]),
                collection=collection,
                nodes=[compounds[int(node)] for node in eden_nodes],
                centriod=compounds[int(eden_nodes[0])],
                density=0.0,
                Color="#EF0000")
            clusters.append(cluster)
            cluster.save()
        clusters_file.close()
示例#5
0
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.
示例#6
0
def calc_new_mol(request) :
    if request.is_ajax():
        selected_compounds = request.GET["selected_compounds"]
        compounds = Compound.objects({"name__in":selected_compounds})
        new_comp = EdenUtil.molcule_design(EdenUtil.sdf_to_gspan(CompoundCollection.generateTmpSDF(compounds,"tmpsdf").name))
        response_data = {}
        response_data["new_comp" ] = new_comp
        response_data['result'] = 'Success'
        response_data['message'] = 'New Molcule Designed'

    return HttpResponse(json.dumps(response_data),  mimetype="application/json")
示例#7
0
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")
示例#8
0
    def distance_matrix_cluster_centriods(self) :
        
        #1- generat an sdf file of the clusters centriod
        tmpsdffile = CompoundCollection.generateTmpSDF(Cluster.get_clusters_centriod(self.compound_group),'centriods')
        pdb.set_trace()
        #2- calcualte distance matrix for the centriods
        sm_output = EdenUtil.similarty_matrix(EdenUtil.sdf_to_gspan(tmpsdffile.name))
        distances = 1 - sm_output

        #######################################
        self.data.new_file()
        self.data.write(distances.tostring())
        self.data.close()
        self.save(validate=False,cascade=False)
        self.calc_knn_eden()
示例#9
0
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")
示例#10
0
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))
示例#11
0
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")
示例#12
0
    def distance_matrix_cluster_centriods(self):

        #1- generat an sdf file of the clusters centriod
        tmpsdffile = CompoundCollection.generateTmpSDF(
            Cluster.get_clusters_centriod(self.compound_group), 'centriods')
        pdb.set_trace()
        #2- calcualte distance matrix for the centriods
        sm_output = EdenUtil.similarty_matrix(
            EdenUtil.sdf_to_gspan(tmpsdffile.name))
        distances = 1 - sm_output

        #######################################
        self.data.new_file()
        self.data.write(distances.tostring())
        self.data.close()
        self.save(validate=False, cascade=False)
        self.calc_knn_eden()
示例#13
0
    def PerformPCA(self, collection=None):
        # pdb.set_trace()
        use_sparse = True
        feature_vectors = []
        feature_dim = 15
        if collection is None:
            collection = [node for c in self.cluster for node in c.nodes]
        #Genrate gspan for Cluster nodes
        tmpsdffile = CompoundCollection.generateTmpSDF(collection, 'cluster')
        #Calculate Feature for cluster nodes
        feature_file = EdenUtil.feature(EdenUtil.sdf_to_gspan(tmpsdffile.name),
                                        feature_dim)
        i = 0
        if use_sparse:
            sparse_mtx = lil_matrix((len(collection), 2**feature_dim))
            row_indx = 0
            for row in feature_file:
                for fet in row.split(" ")[1:]:
                    sparse_mtx[row_indx, int(fet.split(":")[0])] = float(
                        fet.split(":")[1])
                    #compound_feature = Feature(compound=collection[i] , features = sparse_mtx.rows[row_indx])
                    #compound_feature.save()
                row_indx += 1
            pca = sklearn.decomposition.RandomizedPCA(n_components=3)
            pca_res = pca.fit_transform(sparse_mtx)
        else:
            for row in feature_file:
                feat_vect = [0] * (2**feature_dim)
                for fet in row.split(" ")[1:]:
                    feat_vect[int(fet.split(":")[0])] = float(
                        fet.split(":")[1])
                compound_feature = Feature(compound=collection[i],
                                           features=feat_vect)
                compound_feature.save()
                i += 1
                feature_vectors.append(feat_vect)

            #Perform PCA on Feature Vectors
            pca = sklearn.decomposition.PCA(n_components=3)
            pca_res = pca.fit_transform(feature_vectors)

        #store PCA Cluster Result

        #
        return pca_res
示例#14
0
    def calc_knn_eden(self):
        # Calculate and Store KNN
        print "\n Calculating KNN ... \n"
        compounds = Compound.objects(compound_group=self.compound_group)
        tmpsdffile = CompoundCollection.generateTmpSDF(compounds, "comps_eden")
        knn = EdenUtil.nearest_neighbor(EdenUtil.sdf_to_gspan(tmpsdffile.name))

        for knn_entry in knn:
            knn_item = KnnItem(
                simmatrix=self,
                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()
示例#15
0
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))
示例#16
0
    def calc_knn_eden(self) :
        # Calculate and Store KNN
        print "\n Calculating KNN ... \n"
        compounds = Compound.objects(compound_group=self.compound_group)
        tmpsdffile  = CompoundCollection.generateTmpSDF(compounds,"comps_eden")
        knn = EdenUtil.nearest_neighbor(EdenUtil.sdf_to_gspan(tmpsdffile.name))

        for knn_entry in knn :
            knn_item = KnnItem(
                    simmatrix = self
                    ,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()
示例#17
0
    def distance_matrix_eden(self) :
        comps = Compound.objects(compound_group=self.compound_group)
        n = comps.count()
        distances = numpy.ones((n, n), numpy.float64)
        # mmfile = self.compound_group.mol_file.read()
        # tmpsdffile = open('/tmp/'+self.compound_group.title+'.sdf', 'wb+')
        # tmpsdffile.write(mmfile)
        # tmpsdffile.close()

        tmpsdffile  = CompoundCollection.generateTmpSDF(comps,"comps_eden")
        # Calcuate Distances using EDeN
        sm_output = EdenUtil.similarty_matrix(EdenUtil.sdf_to_gspan(tmpsdffile.name))
        distances = 1 - sm_output

        #######################################
        self.data.new_file()
        self.data.write(distances.tostring())
        self.data.close()
        self.save(validate=False,cascade=False)
        self.calc_knn_eden()
示例#18
0
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.
示例#19
0
    def distance_matrix_eden(self):
        comps = Compound.objects(compound_group=self.compound_group)
        n = comps.count()
        distances = numpy.ones((n, n), numpy.float64)
        # mmfile = self.compound_group.mol_file.read()
        # tmpsdffile = open('/tmp/'+self.compound_group.title+'.sdf', 'wb+')
        # tmpsdffile.write(mmfile)
        # tmpsdffile.close()

        tmpsdffile = CompoundCollection.generateTmpSDF(comps, "comps_eden")
        # Calcuate Distances using EDeN
        sm_output = EdenUtil.similarty_matrix(
            EdenUtil.sdf_to_gspan(tmpsdffile.name))
        distances = 1 - sm_output

        #######################################
        self.data.new_file()
        self.data.write(distances.tostring())
        self.data.close()
        self.save(validate=False, cascade=False)
        self.calc_knn_eden()
示例#20
0
    def calculate_clusters_eden(collection) :
        compounds = Compound.objects(compound_group = collection)

        # call EdenUtil Cluster
        clusters_file  = EdenUtil.cluster(EdenUtil.sdf_to_gspan(CompoundCollection.generateTmpSDF(compounds,'tmpcollection').name))

        clusters = []
        # create a cluster for each line
        for eden_cluster in clusters_file :
            eden_nodes = eden_cluster.split(" ")
            eden_nodes = [x for x in eden_nodes if (x != '' and x != '\n')]

            cluster = Cluster(
                 title = "Cluster_" + str(eden_nodes[0])
                ,collection = collection
                ,nodes = [compounds[int(node)] for node in eden_nodes]
                ,centriod = compounds[int(eden_nodes[0])]
                ,density  = 0.0
                ,Color = "#EF0000"
            )
            clusters.append(cluster)
            cluster.save()
        clusters_file.close()
示例#21
0
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))
示例#22
0
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/')
示例#23
0
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))