Exemple #1
0
 def read_external_file_handler(self, data):
     data = convert_to_utf8(json.loads(data))
     print "DATA", data
     try:
         self.wfile.write("".join(open(data["path"],"r").readlines()))
     except:
         self.wfile.write('KO')
def get_best_clustering(results_file):
    """
    Loads and returns the best clustering from a results file.
    """
    results = convert_to_utf8(json.loads(open(results_file).read()))
    best_clustering_id =results["best_clustering"]
    best_clustering_dic = results["selected"][best_clustering_id]
    return Clustering.from_dic(best_clustering_dic["clustering"])
Exemple #3
0
def get_best_clustering(results_file):
    """
    Loads and returns the best clustering from a results file.
    """
    results = convert_to_utf8(json.loads(open(results_file).read()))
    best_clustering_id = results["best_clustering"]
    best_clustering_dic = results["selected"][best_clustering_id]
    return Clustering.from_dic(best_clustering_dic["clustering"])
Exemple #4
0
 def create_directory(self,data):
     data = convert_to_utf8(json.loads(data))
     print data
     try:
         success = create_directory(data['location'], ensure_writability = True)
         self.wfile.write(json.dumps({"done":success}))
     except:
         self.wfile.write(json.dumps({"done":False}))
Exemple #5
0
 def save_params_handler(self, data):
     data = convert_to_utf8(json.loads(data))
     create_directory("wizard/scripts")
     my_hash = hashlib.sha1()
     my_hash.update(str(time.time()))
     path = os.path.join("wizard","scripts",my_hash.hexdigest()[:10]+".ppc")
     script_handler = open(path,"w")
     script_handler.write(json.dumps(data, sort_keys=False, indent=4, separators=(',', ': ')))
     script_handler.close()
     self.wfile.write('{"file_url":"'+path+'"}')
Exemple #6
0
 def get_sources_from_file_list(cls, list_file):
     """
     Gets the sources from a ".lst" file. Each line of this file describes a source using:
         - A single string: the source path.
         - A json object with at least a "source" keyword with the source path.
     
     :param list_file: The path of the list file.
     
     :return: An array with the contents of the file (strings and dictionaries) 
     """
     return [convert_to_utf8(json.loads(line)) for line in open(list_file,"r")]
Exemple #7
0
 def show_results_handler(self, data):
     try:
         data = convert_to_utf8(json.loads(data))
         print "show_results_handler", data
         create_directory("results/tmp")
         results = data["results"] if "results" in data else "results"
         results_path = os.path.join(data["base"],results,"results.json")
         shutil.copyfile(results_path,os.path.join("results","tmp","data.json"))
         webbrowser.open("http://"+IP+":"+str(PORT)+"/results.html", new = 0, autoraise=True)
         self.wfile.write("OK")
     except IOError:
         self.wfile.write("KO")
Exemple #8
0
    def save_frame_handler(self, data):
        data = convert_to_utf8(json.loads(data))
#             print "DATA", data
        path = os.path.join(data["paths"]["results"], "representatives.pdb")

#             try:
#             print "PRIM PATH",path
        file_handler_in = open(path,"r")
        file_handler_out = open("results/tmp/prototype.pdb","w")
        grab_existing_frame_from_trajectory(file_handler_in, file_handler_out, data["frame"])
        file_handler_in.close()
        file_handler_out.close()
        self.wfile.write('{"path":"results/tmp/prototype.pdb"}')
Exemple #9
0
 def get_sources_from_file_list(cls, list_file):
     """
     Gets the sources from a ".lst" file. Each line of this file describes a source using:
         - A single string: the source path.
         - A json object with at least a "source" keyword with the source path.
     
     :param list_file: The path of the list file.
     
     :return: An array with the contents of the file (strings and dictionaries) 
     """
     return [
         convert_to_utf8(json.loads(line)) for line in open(list_file, "r")
     ]
Exemple #10
0
    def save_cluster_handler(self, data):
        data = convert_to_utf8(json.loads(data))
#             print "DATA", data
        path = os.path.join(data["paths"]["tmp"], "tmp_merged_trajectory.pdb")

#             try:
#             print "PRIM PATH",path
        file_handler_in = open(path,"r")
        file_handler_out = open("results/tmp/cluster.pdb","w")
        extract_frames_from_trajectory_sequentially(file_handler_in,
                                                    get_number_of_frames(path),
                                                    file_handler_out,
                                                    data["elements"],
                                                    keep_header=True,
                                                    write_frame_number_instead_of_correlative_model_number=True)
        file_handler_in.close()
        file_handler_out.close()
        self.wfile.write('{"path":"results/tmp/cluster.pdb"}')
Exemple #11
0
    def run_handler(self, data):
        json_script = convert_to_utf8(json.loads(data))
        print json_script
        parameters = None
        try:
            parameters = ProtocolParameters(json_script)
        except ValueError:
            self.wfile.write(json.dumps({"exit_status":"Malformed json script."}))

        observer = Observer()

        global executor
        global ongoing_clustering

        if ongoing_clustering == False:
            executor = ExecutionThread(observer, parameters)
            executor.start()
            self.wfile.write("OK")
        else:
            self.wfile.write("KO")
Exemple #12
0
        parameters = ProtocolParameters.get_params_from_json(script_str)
        # And change another hypothesis stuff
        parameters["clustering"]["evaluation"]["maximum_noise"] = data.noise[dataset_name]
        parameters["clustering"]["evaluation"]["minimum_cluster_size"] = data.minsize[dataset_name]
        parameters["clustering"]["evaluation"]["minimum_clusters"] = data.num_cluster_ranges[dataset_name][0]
        parameters["clustering"]["evaluation"]["maximum_clusters"] = data.num_cluster_ranges[dataset_name][1]
        print parameters["clustering"]["evaluation"]["minimum_clusters"], parameters["clustering"]["evaluation"]["maximum_clusters"]
        if dataset_name in data.criteria:
            parameters["clustering"]["evaluation"]["evaluation_criteria"] = data.criteria[dataset_name]
        else:
            parameters["clustering"]["evaluation"]["evaluation_criteria"] = data.criteria["default"]
        Driver(Observer()).run(parameters)

    for dataset_name in ['concentric_circles']: #data.all_datasets:
        results_file = os.path.join(os.path.abspath("./tmp/%s"%dataset_name),"results/results.json")
        results = convert_to_utf8(json.loads(open(results_file).read()))
        best = results["best_clustering"]
        clustering = Clustering.from_dic(results["selected"][best]["clustering"])
        vtools.show_2D_dataset_clusters(all_observations[dataset_name],
                                        clustering,
                                        scale = 20,
                                        margin = 20).save("clustering_images/%s.jpg"%dataset_name,
                                                 "JPEG")
        print dataset_name,results["selected"][best]["type"],results["selected"][best]["clustering"]["number_of_clusters"], results["selected"][best]["evaluation"]["Noise level"],#results["selected"][best]["parameters"]
        # look for the best criteria
        criteria_scores = []
        for criteria in results["scores"]:
            criteria_scores.append((results["scores"][criteria][best],criteria))
        print criteria_scores

    print "\nDone"
Exemple #13
0
ordered_keys =["Defined Clusters",
               "Diffuse Clusters (defaults)",
               #"Diffuse Clusters (1)",
               "Diffuse Clusters (2)",
               "Diffuse Clusters (3)"]

def calculate_max_min(displacements):
    all_displacements = []
    for cluster_id in displacements:
        if cluster_id != "global":
            all_displacements.append(displacements[cluster_id])
    np_displacements = numpy.array(all_displacements)
    return numpy.max(np_displacements,0),numpy.min(np_displacements,0)

if __name__ == '__main__':
    fig = plt.figure(figsize=(9, 9))
    fig.subplots_adjust(wspace = 0.25, hspace = 0.20, top = 0.85, bottom = 0.05)

    for i, file_id in enumerate(ordered_keys):
        print i, file_id
        displacements = convert_to_utf8(json.loads(open(displacement_files[file_id]).read()))
        ax = fig.add_subplot(len(displacement_files), 1, i)
        ax.set_ylim(-1,max(displacements["global"])+5)
        ax.plot(displacements["global"], color='k', linewidth =2)
        max_displacements, min_displacements = calculate_max_min(displacements)
        ax.plot(max_displacements, color='r', linewidth = 1)
        ax.plot(min_displacements, color='b', linewidth = 1)
        ax.set_title(file_id, weight='bold', size='medium', #position=(0.5, 1.1),
                         horizontalalignment='center', verticalalignment='center')
    plt.show()
Exemple #14
0
 def get_params_from_json(cls, json_string):
     return ProtocolParameters(convert_to_utf8(json.loads(json_string)));
Exemple #15
0
def load_dic_in_json(filename):
    return convert_to_utf8(json.loads("".join(open(filename,"r").readlines())))
    data = {}
    data["column names"] = [str(x) for x in range(N)]
    for i in range(0, N):
        data[str(i)] = []
        for j in range(0, N):
            if (i, j) in cluster.percents:
                data[str(i)].append(cluster.percents[(i, j)])
            else:
                data[str(i)].append(0)

    return data


if __name__ == '__main__':
    results = convert_to_utf8(json.loads(open(sys.argv[1]).read()))
    best_clustering_id = results["best_clustering"]
    best_clustering_dic = results["selected"][best_clustering_id]
    num_clusters = best_clustering_dic["clustering"]["number_of_clusters"]
    clustering = Clustering.from_dic(best_clustering_dic["clustering"])
    file_frames = int(sys.argv[2])

    # generate a map element -> interpolation
    index_to_interpolation = {}
    acc = 0
    for i in range(0, file_frames - 1):
        for j in range(i + 1, file_frames):
            for k in range(20):
                index_to_interpolation[acc] = (i, j)
                acc += 1
Exemple #17
0
 def get_params_from_json(cls, json_string):
     return ProtocolParameters(convert_to_utf8(json.loads(json_string)))
Exemple #18
0
 def file_exists_handler(self, data):
     data = convert_to_utf8(json.loads(data))
     print data
     self.wfile.write(json.dumps({"exists":os.path.exists(data['location']),
                                  "isfile":os.path.isfile(data['location']),
                                  "isdir":os.path.isdir(data['location'])}))
Exemple #19
0
 def do_selection(self,data):
     data = convert_to_utf8(json.loads(data))
     print data
     self.wfile.write(get_pdb_selection(data))
    data = {}
    data["column names"] = [str(x) for x in range(N)]
    for i in range(0,N):
        data[str(i)] = []
        for j in range(0,N):
            if (i,j) in cluster.percents:
                data[str(i)].append( cluster.percents[(i,j)])
            else:
                data[str(i)].append(0)

    return data



if __name__ == '__main__':
    results = convert_to_utf8(json.loads(open(sys.argv[1]).read()))
    best_clustering_id =results["best_clustering"]
    best_clustering_dic = results["selected"][best_clustering_id]
    num_clusters = best_clustering_dic["clustering"]["number_of_clusters"]
    clustering = Clustering.from_dic(best_clustering_dic["clustering"])
    file_frames = int(sys.argv[2])

    # generate a map element -> interpolation
    index_to_interpolation = {}
    acc = 0
    for i in range(0, file_frames-1):
        for j in range(i+1, file_frames):
            for k in range(20):
                index_to_interpolation[acc] = (i,j)
                acc += 1
Exemple #21
0
 def normalize_path_handler(self, data):
     data = convert_to_utf8(json.loads(data))
     print "DATA", data
     print "PATH", data["path"].replace("%2F","/")
     print "NORM PATH", os.path.abspath(data["path"].replace("%2F","/"))
     self.wfile.write('{"path":"'+os.path.abspath(data["path"].replace("%2F","/"))+'"}')
Exemple #22
0
def calculate_max_min(displacements):
    all_displacements = []
    for cluster_id in displacements:
        if cluster_id != "global":
            all_displacements.append(displacements[cluster_id])
    np_displacements = numpy.array(all_displacements)
    return numpy.max(np_displacements, 0), numpy.min(np_displacements, 0)


if __name__ == '__main__':
    fig = plt.figure(figsize=(9, 9))
    fig.subplots_adjust(wspace=0.25, hspace=0.20, top=0.85, bottom=0.05)

    for i, file_id in enumerate(ordered_keys):
        print i, file_id
        displacements = convert_to_utf8(
            json.loads(open(displacement_files[file_id]).read()))
        ax = fig.add_subplot(len(displacement_files), 1, i)
        ax.set_ylim(-1, max(displacements["global"]) + 5)
        ax.plot(displacements["global"], color='k', linewidth=2)
        max_displacements, min_displacements = calculate_max_min(displacements)
        ax.plot(max_displacements, color='r', linewidth=1)
        ax.plot(min_displacements, color='b', linewidth=1)
        ax.set_title(
            file_id,
            weight='bold',
            size='medium',  #position=(0.5, 1.1),
            horizontalalignment='center',
            verticalalignment='center')
    plt.show()
    print results[selection_class][clustering_id]["parameters"]

if __name__ == '__main__':
    parser = optparse.OptionParser(usage='%prog -m <arg> -c <arglist> [-o <arg>]', version='1.0')

    parser.add_option('-l', action="store_true", dest = "print_list", help="Print a list of generated clusters and some properties",  metavar = "1")
    parser.add_option("-s", action="store_true", dest="show_protein", help="Shows the protein backbone.")
    parser.add_option('-r', action="store", dest = "results_file", help="", metavar = "results.json")
    parser.add_option('-p', action="store", dest = "parameters_file", help="",metavar = "parameters.json")
    parser.add_option('-c', action="store", dest = "clustering_to_see", help="",metavar = "clustering_0001")
    parser.add_option('--all', action="store_true", dest = "all_clusterings", help="",metavar = "")
    parser.add_option('--stride', type = "int", action="store", dest = "stride", help="",metavar = "5")
    options, args = parser.parse_args()

    params = convert_to_utf8(json.loads(open(options.parameters_file).read()))
    if params["data"]["matrix"]["method"] == "distance":
        results = convert_to_utf8(json.loads(open(options.results_file).read()))
    else:
        print "ERROR: Only 'distance' clusterings can be plotted."

    if options.print_list:
        print "SELECTED"
        print "========"
        for selected_cluster in results["selected"]:
            print_cluster_info("selected",selected_cluster,results)
        if options.all_clusterings:
            print "NOT SELECTED"
            print "============"
            for not_selected_cluster in results["not_selected"]:
                print_cluster_info("not_selected",not_selected_cluster,results)
Exemple #24
0
            "maximum_clusters"] = data.num_cluster_ranges[dataset_name][1]
        print parameters["clustering"]["evaluation"][
            "minimum_clusters"], parameters["clustering"]["evaluation"][
                "maximum_clusters"]
        if dataset_name in data.criteria:
            parameters["clustering"]["evaluation"][
                "evaluation_criteria"] = data.criteria[dataset_name]
        else:
            parameters["clustering"]["evaluation"][
                "evaluation_criteria"] = data.criteria["default"]
        Driver(Observer()).run(parameters)

    for dataset_name in ['concentric_circles']:  #data.all_datasets:
        results_file = os.path.join(os.path.abspath("./tmp/%s" % dataset_name),
                                    "results/results.json")
        results = convert_to_utf8(json.loads(open(results_file).read()))
        best = results["best_clustering"]
        clustering = Clustering.from_dic(
            results["selected"][best]["clustering"])
        vtools.show_2D_dataset_clusters(
            all_observations[dataset_name], clustering, scale=20,
            margin=20).save("clustering_images/%s.jpg" % dataset_name, "JPEG")
        print dataset_name, results["selected"][best]["type"], results[
            "selected"][best]["clustering"]["number_of_clusters"], results[
                "selected"][best]["evaluation"][
                    "Noise level"],  #results["selected"][best]["parameters"]
        # look for the best criteria
        criteria_scores = []
        for criteria in results["scores"]:
            criteria_scores.append(
                (results["scores"][criteria][best], criteria))
Exemple #25
0
                      help="",
                      metavar="clustering_0001")
    parser.add_option('--all',
                      action="store_true",
                      dest="all_clusterings",
                      help="",
                      metavar="")
    parser.add_option('--stride',
                      type="int",
                      action="store",
                      dest="stride",
                      help="",
                      metavar="5")
    options, args = parser.parse_args()

    params = convert_to_utf8(json.loads(open(options.parameters_file).read()))
    if params["data"]["matrix"]["method"] == "distance":
        results = convert_to_utf8(json.loads(
            open(options.results_file).read()))
    else:
        print "ERROR: Only 'distance' clusterings can be plotted."

    if options.print_list:
        print "SELECTED"
        print "========"
        for selected_cluster in results["selected"]:
            print_cluster_info("selected", selected_cluster, results)
        if options.all_clusterings:
            print "NOT SELECTED"
            print "============"
            for not_selected_cluster in results["not_selected"]: