<commit_before>import tangelo
import cherrypy
import json
import pymongo
import bson.objectid
import bson.json_util
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr

def fit_continuous_bm(analysis):
    tree_data = analysis["inputs"][0]["data"]
    table_data = analysis["inputs"][1]["data"]

    ape = importr("ape")
    geiger = importr("geiger")
    rjson = importr("rjson")

    r = robjects.r

    # anoleTree<-read.tree("anolis.phy")
    anoleTree = ape.read_tree(text=tree_data["content"])

    # anoleData<-read.csv("anolisDataAppended.csv", row.names=1)
    kwargs = {"text": table_data["content"], "row.names": robjects.Vector(1)}
    anoleData = r["read.csv"](**kwargs)

    # svl<-anoleData[,1]
    svl = anoleData.rx(True, 1)

    #names(svl) = r.rownames(anoleData)
    svl.names = r.rownames(anoleData)

    # bmOut<-fitContinuous(anoleTree, svl, model="BM")
    bmOut = geiger.fitContinuous(anoleTree, svl, model="BM")

    results = {
        "name": "results",
        "type": "table",
        "data": {
            "type": "json",
            "content": [json.loads(rjson.toJSON(bmOut.rx2("opt"))[0])]
        }
    }

    return [results]

def fit_continuous_ou(analysis):
    tree_data = analysis["inputs"][0]["data"]
    table_data = analysis["inputs"][1]["data"]

    ape = importr("ape")
    geiger = importr("geiger")
    rjson = importr("rjson")

    r = robjects.r

    # anoleTree<-read.tree("anolis.phy")
    anoleTree = ape.read_tree(text=tree_data["content"])

    # anoleData<-read.csv("anolisDataAppended.csv", row.names=1)
    kwargs = {"text": table_data["content"], "row.names": robjects.Vector(1)}
    anoleData = r["read.csv"](**kwargs)

    # svl<-anoleData[,1]
    svl = anoleData.rx(True, 1)

    #names(svl) = r.rownames(anoleData)
    svl.names = r.rownames(anoleData)

    # ouOut<-fitContinuous(anoleTree, svl, model="OU")
    ouOut = geiger.fitContinuous(anoleTree, svl, model="OU")

    results = {
        "name": "results",
        "type": "table",
        "data": {
            "type": "json",
            "content": [json.loads(rjson.toJSON(ouOut.rx2("opt"))[0])]
        }
    }

    #ouTree<-transform(anoleTree, "OU", ouOut$opt$alpha)
    ouTree = geiger.transform(anoleTree, "OU", ouOut.rx2("opt").rx2("alpha"))
    outTree = {
        "name": "transformed tree",
        "type": "tree",
        "data": {
            "type": "newick",
            "content": ape.write_tree(ouTree)[0]
        }
    }

    return [results, outTree]

def fit_continuous_eb(analysis):
    tree_data = analysis["inputs"][0]["data"]
    table_data = analysis["inputs"][1]["data"]

    ape = importr("ape")
    geiger = importr("geiger")
    rjson = importr("rjson")

    r = robjects.r

    # anoleTree<-read.tree("anolis.phy")
    anoleTree = ape.read_tree(text=tree_data["content"])

    # anoleData<-read.csv("anolisDataAppended.csv", row.names=1)
    kwargs = {"text": table_data["content"], "row.names": robjects.Vector(1)}
    anoleData = r["read.csv"](**kwargs)

    # svl<-anoleData[,1]
    svl = anoleData.rx(True, 1)

    #names(svl) = r.rownames(anoleData)
    svl.names = r.rownames(anoleData)

    # ebOut<-fitContinuous(anoleTree, svl, model="EB")
    ebOut = geiger.fitContinuous(anoleTree, svl, model="EB")

    results = {
        "name": "results",
        "type": "table",
        "data": {
            "type": "json",
            "content": [json.loads(rjson.toJSON(ebOut.rx2("opt"))[0])]
        }
    }

    #ebTree<-transform(anoleTree, "EB", ebOut$opt$a)
    ebTree = geiger.transform(anoleTree, "EB", ebOut.rx2("opt").rx2("a"))
    outTree = {
        "name": "transformed tree",
        "type": "tree",
        "data": {
            "type": "newick",
            "content": ape.write_tree(ebTree)[0]
        }
    }

    return [results, outTree]

def fit_continuous_model_comparison(analysis):
    models = [m["data"]["content"][0] for m in analysis["inputs"] if "data" in m]

    results = {
        "name": "model comparison",
        "type": "table",
        "data": {
            "type": "json",
            "content": models<commit_msg>Use parameter for specifying fit continuous character<commit_after>import tangelo
import cherrypy
import json
import pymongo
import bson.objectid
import bson.json_util
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr

def fit_continuous_bm(analysis):
    tree_data = analysis["inputs"][0]["data"]
    table_data = analysis["inputs"][1]["data"]

    ape = importr("ape")
    geiger = importr("geiger")
    rjson = importr("rjson")

    r = robjects.r

    # anoleTree<-read.tree("anolis.phy")
    anoleTree = ape.read_tree(text=tree_data["content"])

    # anoleData<-read.csv("anolisDataAppended.csv", row.names=1)
    kwargs = {"text": table_data["content"], "row.names": robjects.Vector(1)}
    anoleData = r["read.csv"](**kwargs)

    # svl<-anoleData[,1]
    svl = anoleData.rx2(analysis["parameters"][0]["current"])

    #names(svl) = r.rownames(anoleData)
    svl.names = r.rownames(anoleData)

    # bmOut<-fitContinuous(anoleTree, svl, model="BM")
    bmOut = geiger.fitContinuous(anoleTree, svl, model="BM")

    results = {
        "name": "results",
        "type": "table",
        "data": {
            "type": "json",
            "content": [json.loads(rjson.toJSON(bmOut.rx2("opt"))[0])]
        }
    }

    return [results]

def fit_continuous_ou(analysis):
    tree_data = analysis["inputs"][0]["data"]
    table_data = analysis["inputs"][1]["data"]

    ape = importr("ape")
    geiger = importr("geiger")
    rjson = importr("rjson")

    r = robjects.r

    # anoleTree<-read.tree("anolis.phy")
    anoleTree = ape.read_tree(text=tree_data["content"])

    # anoleData<-read.csv("anolisDataAppended.csv", row.names=1)
    kwargs = {"text": table_data["content"], "row.names": robjects.Vector(1)}
    anoleData = r["read.csv"](**kwargs)

    # svl<-anoleData[,1]
    svl = anoleData.rx2(analysis["parameters"][0]["current"])

    # names(svl) = r.rownames(anoleData)
    svl.names = r.rownames(anoleData)

    # ouOut<-fitContinuous(anoleTree, svl, model="OU")
    ouOut = geiger.fitContinuous(anoleTree, svl, model="OU")

    results = {
        "name": "results",
        "type": "table",
        "data": {
            "type": "json",
            "content": [json.loads(rjson.toJSON(ouOut.rx2("opt"))[0])]
        }
    }

    # ouTree<-transform(anoleTree, "OU", ouOut$opt$alpha)
    ouTree = geiger.transform(anoleTree, "OU", ouOut.rx2("opt").rx2("alpha"))
    outTree = {
        "name": "transformed tree",
        "type": "tree",
        "data": {
            "type": "newick",
            "content": ape.write_tree(ouTree)[0]
        }
    }

    return [results, outTree]

def fit_continuous_eb(analysis):
    tree_data = analysis["inputs"][0]["data"]
    table_data = analysis["inputs"][1]["data"]

    ape = importr("ape")
    geiger = importr("geiger")
    rjson = importr("rjson")

    r = robjects.r

    # anoleTree<-read.tree("anolis.phy")
    anoleTree = ape.read_tree(text=tree_data["content"])

    # anoleData<-read.csv("anolisDataAppended.csv", row.names=1)
    kwargs = {"text": table_data["content"], "row.names": robjects.Vector(1)}
    anoleData = r["read.csv"](**kwargs)

    # svl<-anoleData[,1]
    svl = anoleData.rx2(analysis["parameters"][0]["current"])

    # names(svl) = r.rownames(anoleData)
    svl.names = r.rownames(anoleData)

    # ebOut<-fitContinuous(anoleTree, svl, model="EB")
    ebOut = geiger.fitContinuous(anoleTree, svl, model="EB")

    results = {
        "name": "results",
        "type": "table",
        "data": {
            "type": "json",
            "content": [json.loads(rjson.toJSON(ebOut.rx2("opt"))[0])]
        }
    }

    # ebTree<-transform(anoleTree, "EB", ebOut$opt$a)
    ebTree = geiger.transform(anoleTree, "EB", ebOut.rx2("opt").rx2("a"))
    outTree = {
        "name": "transformed tree",
        "type": "tree",
        "data": {
            "type": "newick",
            "content": ape.write_tree(ebTree)[0]
        }
    }

    return [results, outTree]

def fit_continuous_model_comparison(analysis):
    models = [m["data"]["content"][0] for m in analysis["inputs"] if "data" in m]

    results = {
        "name": "model comparison",
        "type": "table",
        "data": {
            "type": "json",
            "content": models
{'fraction_non_alphanumeric': 0.1817206863679695, 'fraction_numerical': 0.008222116301239275, 'mean_word_length': 3.004293893129771, 'pattern_counts': {'":': 72, '<': 31, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1509307', 'n_tokens_mistral': 3136, 'n_tokens_neox': 3001, 'n_words': 482}