Example #1
0
def get_filter_result(request):
    
    if request.is_ajax():
        term_count = int(request.POST["term_count"])
        
        filter_terms = []
        response_data = {}

        for i in range(1,term_count+1):
            ttype  = str(request.POST["filter_type_"+str(i)])
            tkey   = str(request.POST["filter_key_"+str(i)])
            topp    = str(request.POST["filter_terms_op_"+str(i)])
            tvalue = [] 
            if ttype == "range" :
                tvalue.append(str(request.POST["filter_value_from_"+str(i)]))
                tvalue.append(str(request.POST["filter_value_to_"+str(i)]))           
            elif ttype == "multichoice" :
                 tvalue.append(request.POST.getlist("filter_multi_value_"+str(i))) 
            elif ttype == "text_search" :
                tvalue.append(str(request.POST["filter_value_"+str(i)]))
                    
            term = FilterTerm(
                        term_type  = ttype ,
                        term_key   = tkey ,
                        term_value = tvalue
                    )
            filter_terms.append(term)
        
        etype = str(request.POST["effect_type"])
        
        if etype == "color" :
            evalue =   str(request.POST["effect_color"])
        elif etype == "scale" :
            evalue =  str(request.POST["effect_scale"])
        elif etype == "hide" :
            evalue =  ""
        effect = VisualEffect( effect_type = etype   , effect_value = evalue)
        
        filter_rule = FilterRule(terms = filter_terms ,terms_op = topp ,vis_effect = effect)
      
        compounds = Compound.objects(compound_group = request.session.get("collection_id")).filter(filter_rule.get_query())

        Clusters = Cluster.objects(collection = request.session.get("collection_id") , nodes__in=compounds)

        if(len(Clusters)>0) :
            response_data['cluster_percent'] = [{"id":str(c.id) , "percent" : float(c.filtered_percentage(compounds))} for c in Clusters]


        response_data['result'] = 'Success'
        response_data['message'] = 'Compounds Fields'
        #response_data['field_names'] = []
        #for f in field_names :
        #    response_data['field_names'].append(f)  
        response_data['filterd_compound'] = [ c.name for c in compounds ]
        
        
        
        return HttpResponse(json.dumps(response_data),  mimetype="application/json")
                   
    return HttpResponse({}, mimetype="application/json")    
Example #2
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()
Example #3
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.
Example #4
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")
Example #5
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")
Example #6
0
def get_comp_field_values(request) :
    
    if request.is_ajax():
        
        compounds = Compound.objects(compound_group = request.session.get("collection_id")).item_frequencies(request.GET["field"])
        response_data = {}
        response_data['result'] = 'Success'
        response_data['message'] = 'Compounds Fields'
        response_data['field_values'] = list(compounds)

        pprint.pprint(compounds)

        return HttpResponse(json.dumps(response_data),  mimetype="application/json")
    return HttpResponse({}, mimetype="application/json")    
Example #7
0
def get_comp_field_names(request) :
    if request.is_ajax():
        
        comp = Compound.objects(compound_group = request.session.get("collection_id"))
        response_data = {}
        response_data['result'] = 'Success'
        response_data['message'] = 'Compounds Fields'
        response_data['field_names'] =  []
        response_data['dfield_names'] =  []
        if comp :
            response_data['field_names'] = comp[0].get_model_fields() + comp[0].get_model_dfields()
            response_data['dfield_names'] =  comp[0].get_model_dfields()

        return HttpResponse(json.dumps(response_data),  mimetype="application/json")
    return HttpResponse({}, mimetype="application/json")    
Example #8
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()
Example #9
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()
Example #10
0
def get_comp_details(request) :
    if request.is_ajax():
        mdsres = MDSRes.objects().with_id(request.session.get("mds_id"))
        compound = Compound.objects(compound_group = request.session.get("collection_id"),name = str(request.GET["name"]))

        if(mdsres) :
            knn_items = KnnItem.objects(simmatrix = mdsres.simmatrix ,mol_id = compound[0].name)
        else :
            knn_items = KnnItem.objects(compound_group = request.session.get("collection_id") ,mol_id = compound[0].name)

        #arena = chemfp.load_fingerprints(mdsres.simmatrix.fp)
        #knearest = arena.knearest_tanimoto_search_fp(arena[arena.ids.index(compound[0].name)][1],k=int(request.GET["k"]),threshold=float(request.GET["t"])/100.00)

        threshold= float(request.GET["t"])/100.00
        k=int(request.GET["k"]) if (int(request.GET["k"])>0) else 30

        if len(knn_items) > 0 :
            neighbors = [ {"mol_id":str(ki.get("mol_id")),"val":str(ki.get("val"))} for ki in knn_items[0].neighbors if float(ki.get("val") >= threshold )]
        else :
            neighbors = []
        compitem = {
                "name" :compound[0].name ,
                "generic_name" :compound[0].generic_name ,
                "mol_weight" : compound[0].mol_weight , 
                "formula" : compound[0].formula ,   
                "inchi" : compound[0].inchi ,
                "exact_mass" : compound[0].exact_mass ,
                "smiles" : compound[0].smiles ,
                "ring_count" :  compound[0].ring_count ,
                "mol" :  compound[0].mol ,
                "neighbors" : neighbors[:k]
            }
        
        
        response_data = {}
        response_data['result'] = 'Success'
        response_data['message'] = 'Compounds Coordinate'
        response_data['compound'] = compitem
        #response_data['neighbors'] = knearest.get_ids_and_scores()
        
        return HttpResponse(json.dumps(response_data),  mimetype="application/json")
    return HttpResponse({}, mimetype="application/json")
Example #11
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()
Example #12
0
def detailed_view(request) :
    if request.method == 'GET':
        mdsres = MDSRes.objects().with_id(request.session.get("mds_id"))
        compound = Compound.objects(compound_group = request.session.get("collection_id"),name = str(request.GET["name"]))
        threshold= 0/100.00
        k=int(10)

        neighbors = []
        if(mdsres) :
            knnitems = KnnItem.objects(simmatrix = mdsres.simmatrix ,mol_id = compound[0].name)
        else :
            knnitems = KnnItem.objects(compound_group = request.session.get("collection_id") ,mol_id = compound[0].name)
        if(len(knnitems)>0) :
            neighbors = [ {"mol_id":str(ki.get("mol_id")),"val":str(ki.get("val"))} for ki in knnitems[0].neighbors if float(ki.get("val") >= threshold )]


        print(compound[0]._data)


        compitem = {
                "name" :compound[0].name ,
                "generic_name" :compound[0].generic_name ,
                "mol_weight" : compound[0].mol_weight , 
                "formula" : compound[0].formula ,   
                "inchi" : compound[0].inchi ,
                "exact_mass" : compound[0].exact_mass ,
                "smiles" : compound[0].smiles ,
                "ring_count" :  compound[0].ring_count ,
                "mol" :  compound[0].mol ,
                "neighbors" : neighbors[:k]
            }
        response_data = {}

        response_data['dfield_values'] = [ {"field":f ,"val":compound[0]._data[f]} for f in compound[0].get_model_dfields()]
        print response_data['dfield_values']
        response_data['result'] = 'Success'
        response_data['message'] = 'Compounds Coordinate'
        response_data['compound'] = compitem

        return render_to_response('detailed_view.html',{"view_titel":"Detailed View","response":response_data},context_instance=RequestContext(request))
    return render_to_response('detailed_view.html',{"view_titel":"Detailed View"},context_instance=RequestContext(request))
Example #13
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.
Example #14
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()
Example #15
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()
Example #16
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/')
Example #17
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))