Esempio n. 1
0
def annotatedTree(accession, tree_method='nj'):
    """ Retrieve tree annotation-information from a family, based on bpg accession number,
        and return JSON-formatted representation of the needed fields. """
    _writer = json.JsonWriter()
    family_root_dir = '/clusterfs/ohana/bpg/pfacts/'
    family_dir = os.path.join(family_root_dir, accession[0:4], accession[0:7],
                              accession)
    tree_files = glob.glob('%s/%s.%s' % (family_dir, accession, tree_method))
    if len(tree_files) == 0:
        print "No tree file found: %s, %s, %s" \
              % (family_dir, accession, tree_method)
        return ''
    f = open(tree_files[0])
    nhxTree = f.read().translate(trivial_translation, string.whitespace)
    f.close()

    sequence_header_ids = [
        int(seqhdr_str) for seqhdr_str in seqhdr_re.findall(nhxTree)
    ]

    ret = {}
    ret = _annotate_nhx_element(accession, sequence_header_ids)
    nhxTree = nhxTree.replace('SEQHDR', '')
    ret['__tree__'] = nhxTree
    ret_string = _writer.write(ret)
    return ret_string.replace(unichr(1), ' ')
Esempio n. 2
0
    def jsonCmd(self, method, params={}, retry=0):
        if self.__nextdownload > datetime.datetime.now():
            diff = self.__nextdownload - datetime.datetime.now()
            secs = diff.microseconds / 1000000.0 + diff.seconds + diff.days * 24 * 3600
            time.sleep(secs)

        # We could use system.listMethods to check that cmd["method"]
        # is still recognised by the server?
        cmd = urllib.quote(json.JsonWriter().write({
            "id": self.__jsonid,
            "method": method,
            "params": params
        }))
        try:
            ret = json.read(urllib2.urlopen(self.__jsonurl, cmd).read())
            self.__nextdownload = datetime.datetime.now() + datetime.timedelta(
                seconds=self.config.getfloat("DEFAULT", "waitseconds"))
        except urllib2.URLError, e:
            # Retry on the following errors:
            # 104 - Connection reset
            # 110 - Connection timed out
            if e.reason[0] == 104 or e.reason[0] == 110:
                if retry < 5:
                    self.__output("%s - will retry in 5 seconds" % e.reason[1])
                    time.sleep(5)
                    return self.jsonCmd(method, params, retry + 1)
                else:
                    self.__output("%s - giving up" % e.reason[1])
            print >> sys.stderr, ("URL Error %d: %s (%s)" %
                                  (e.reason[0], e.reason[1], self.__jsonurl))
            sys.exit(3)
Esempio n. 3
0
    def ajax(self):
        postData = cherrypy.request.body.read()
        request = json.JsonReader().read(postData)
        print request
        if not 'rootPassword' in request:
            return json.JsonWriter().write({"error": "No password specified."})

        try:
            self._ensureConfigFileExists()

            self._validatePassword(request['rootPassword'])
            if 'adminUsername' in request:
                self._setAdminUser(request['adminUsername'],
                                   request['adminPassword'],
                                   request['adminConfirmPassword'])
            if 'adminRoot' in request:
                self._setAdminRoot(request['adminUsername'],
                                   request['adminRoot'])
        except ValueError, error:
            return json.JsonWriter().write({"error": str(error)})
Esempio n. 4
0
def superorthologousNodes(accession, tree_method='ml'):
    _writer = json.JsonWriter()
    querySet = TreeNode.objects.filter(
        duplication_distance__gte=0.0,
        greatest_duplication_distance_of_maximal_descendant=-1,
        tree__family__id__exact=int(accession[4:]),
        tree__method=tree_method)
    left_ids = [tree_node.left_id for tree_node in querySet]
    ret = {}
    ret['superorthologous_left_ids'] = left_ids
    return (_writer.write(ret))
def readTree(phy_output):
    #Retrieve tree from tree file and make a JSON object
    _writer = json.JsonWriter()

    #Making JSON for the SATCHMO tree in .phy file
    treeFile = phy_output
    f = open(treeFile)
    tree = ''.join([line.rstrip() for line in f.readlines()])
    f.close()
    tree = tree.replace(',', ':1.0,')
    tree = tree.replace(')', ':1.0)')
    #Changed this - removed ;
    tree = tree.replace(';', ':1.0')
    return tree
Esempio n. 6
0
def hyperNeighborhood(phog, ortholog_type, threshold, taxon_id):
    edges = set()
    hyper_neighbors = phog.get_hyper_neighbors(ortholog_type, threshold)
    if taxon_id:
        taxon = NCBITaxonomy.objects.get(id__exact=taxon_id)
    nodes = {}
    nodes[phog.get_accession(ortholog_type, threshold)] = {}
    nodes[phog.get_accession(ortholog_type, threshold)]['description'] \
        = phog.get_description(ortholog_type, threshold)
    if taxon_id:
        nodes[phog.get_accession(ortholog_type, threshold)]['genes_from_taxon'] \
            = remove_duplicates([leaf.sequence_header.identifier() for leaf in
                phog.get_contained_leaves_from_taxon(taxon, ortholog_type, threshold)])
    for type in hyper_neighbors.keys():
        for neighbor in hyper_neighbors[type]:
            nodes[neighbor.get_accession(ortholog_type, threshold)] = {}
            nodes[neighbor.get_accession(ortholog_type, threshold)]['description'] \
                = neighbor.get_description(ortholog_type, threshold)
            if taxon_id:
                nodes[neighbor.get_accession(ortholog_type,
                                              threshold)]['genes_from_taxon']\
                    = remove_duplicates([leaf.sequence_header.identifier() for leaf in
                        neighbor.get_contained_leaves_from_taxon(taxon, ortholog_type,
                                                                  threshold)])
    for type in hyper_neighbors.keys():
        for hyper_neighbor in hyper_neighbors[type]:
            edges.add(((phog.get_accession(ortholog_type, threshold),
                        hyper_neighbor.get_accession(ortholog_type,
                                                     threshold)), type))
            neighbors_of_hyper_neighbors \
              = hyper_neighbor.get_hyper_neighbors(ortholog_type, threshold)
            if type in neighbors_of_hyper_neighbors:
                relevant_neighbors \
                  = neighbors_of_hyper_neighbors[type] & hyper_neighbors[type]
                for neighbor in relevant_neighbors:
                    edges.add(
                        ((hyper_neighbor.get_accession(ortholog_type,
                                                       threshold),
                          neighbor.get_accession(ortholog_type,
                                                 threshold)), type))
    ret = {}
    ret['query'] = phog.get_accession(ortholog_type, threshold)
    ret['edges'] = list(edges)
    ret['nodes'] = nodes
    _writer = json.JsonWriter()
    # print ret
    return (_writer.write(ret))
Esempio n. 7
0
def annotatedSummaryTree(family_accession, tree_method='ml', threshold=0.0):
    _writer = json.JsonWriter()
    newick_tree = phog_summary_tree(family_accession,
                                    method=tree_method,
                                    threshold=threshold)
    ret = {}
    sequence_header_ids = [
        int(seqhdr_str) for seqhdr_str in seqhdr_re.findall(newick_tree)
    ]
    ret['SequenceHeaderAnnotations'] = _annotate_nhx_element(
        family_accession, sequence_header_ids)
    ret['PHOG_Annotations'] = _annotate_phog_element(family_accession,
                                                     sequence_header_ids,
                                                     tree_method, threshold)
    ret['__tree__'] = newick_tree
    ret_string = _writer.write(ret)
    return ret_string.replace(unichr(1), ' ')
Esempio n. 8
0
class rdiffSetupPage(page_main.rdiffPage):
    """Helps the user through initial rdiffWeb setup.
      This page is displayed with rdiffWeb is not yet configured.
      """
    @cherrypy.expose
    def index(self):
        page = rdw_helpers.compileTemplate("page_start.html",
                                           title='Set up rdiffWeb',
                                           rssLink='',
                                           rssTitle='')
        rootEnabled = False
        error = ""
        try:
            rootEnabled = self._rootAccountEnabled()
        except KeyError:
            error = "rdiffWeb setup must be run with root privileges."
        page += rdw_helpers.compileTemplate("setup.html",
                                            rootEnabled=rootEnabled,
                                            error=error)
        page += rdw_helpers.compileTemplate("page_end.html")
        return page

    @cherrypy.expose
    def ajax(self):
        postData = cherrypy.request.body.read()
        request = json.JsonReader().read(postData)
        print request
        if not 'rootPassword' in request:
            return json.JsonWriter().write({"error": "No password specified."})

        try:
            self._ensureConfigFileExists()

            self._validatePassword(request['rootPassword'])
            if 'adminUsername' in request:
                self._setAdminUser(request['adminUsername'],
                                   request['adminPassword'],
                                   request['adminConfirmPassword'])
            if 'adminRoot' in request:
                self._setAdminRoot(request['adminUsername'],
                                   request['adminRoot'])
        except ValueError, error:
            return json.JsonWriter().write({"error": str(error)})

        return json.JsonWriter().write({})
def makeJsonTree(phy_output):

    #Retrieve tree from tree file and make a JSON object
    _writer = json.JsonWriter()

    #Making JSON for the SATCHMO tree in .phy file
    treeFile = phy_output
    f = open(treeFile)
    tree = ''.join([line.rstrip() for line in f.readlines()])
    f.close()

    ret = {}
    tree = tree.replace(',', ':1.0,')
    tree = tree.replace(')', ':1.0)')
    tree = tree.replace(';', ':1.0;')
    ret['__tree__'] = tree
    ret['__seed_protein_sequence_id__'] = 1002

    ans = _writer.write(ret)
    return ans
Esempio n. 10
0
def getMRCA(taxid=[], withNonCellular=False):
    _writer = json.JsonWriter()
    _db = MySQLdb.connect(db=phylofacts_db_name, host=phylofacts_db_host, \
                      user=phylofacts_db_user, passwd=phylofacts_db_passwd)
    _cur = _db.cursor(MySQLdb.cursors.DictCursor)
    if 0 == len(taxid):
        return _writer.write(None)
    if isinstance(taxid, list):
        taxid = _uniq(taxid)
    if 1 == len(taxid):
        taxid = taxid[0]

    if isinstance(taxid, basestring):
        if _getlin(taxid)[-1]['id'] != 131567 and not withNonCellular:
            return _writer.write(None)
        else:
            sql = "SELECT id, scientific_name, common_name FROM ncbi_taxonomy WHERE id = %s" % taxid
    else:
        if withNonCellular:
            tree = [[taxon] for taxon in taxid]
        else:
            tree = []
            for taxon in taxid:
                lineage = _getlin(taxon)
                if lineage is not None and lineage[-1]['id'] == 131567:
                    tree.append([taxon])
        if len(tree) == 0:
            return _writer.write(None)
        elif len(tree) == 1:
            sql = "SELECT id, scientific_name, common_name FROM ncbi_taxonomy WHERE id = %s" % tree[
                0]
        else:
            parent = _recursiveGetParent(tree, _cur)
            sql = "SELECT id, scientific_name, common_name FROM ncbi_taxonomy WHERE id = %s" % parent
    numRows = _cur.execute(sql)
    if numRows > 0:
        row = _cur.fetchone()
        return _writer.write(row)
    else:
        return _writer.write(None)
Esempio n. 11
0
def _send(request):

    import socket, json

    dumps = None
    loads = None
    sock = None
    dist = None

    try:
        dist = __n__['platform']
    except:
        pass

    if dist == 'openwrt':
        dumps = json.JsonWriter().write
        loads = json.JsonReader().read
        sock = '/var/run/db.sock'
    else:
        dumps = json.dumps
        loads = json.loads
        sock = '/var/run/openvswitch/db.sock'

    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    s.connect(sock)

    pdu = dumps(request)
    s.send(pdu)
    response = s.recv(4096)

    #__n__['logger'].debug(_logstr('... JSON-RPC request ...', str(pdu), '... JSON-RPC response ...', response))
    #transaction = '--json-rpc:{"request":%s, "response":%s}' % (str(pdu),response)
    transaction = '{"request":%s, "response":%s}' % (str(pdu), response)
    __n__['logger2'].debug(transaction,
                           extra={'content_type': 'application/json'})

    return loads(response)
Esempio n. 12
0
"""

import os, socket, json

DIST = 'debian'

dumps = None
loads = None
sock = None

if DIST == 'debian':
    dumps = json.dumps
    loads = json.loads
    sock = '/var/run/openvswitch/db.sock'
elif DIST == 'openwrt':
    dumps = json.JsonWriter().write
    loads = json.JsonReader().read
    sock = '/var/run/db.sock'

s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(sock)

rpc = {
    "method": "transact",
    "params": ["Open_vSwitch", {
        "op": "insert",
        "table": "NLAN",
        "row": {}
    }],
    "id": 0
}
Esempio n. 13
0
def getLineage(taxid):
    _writer = json.JsonWriter()
    return _writer.write(_getlin(taxid))
Esempio n. 14
0
 def json_encode(data, indent=0):
     return json.JsonWriter().write(data)
def makeJsonAlign(smo_output, phy_output):
    _writer = json.JsonWriter()
    #Making JSON for root alignment in .smo file
    treeStr = readTree(phy_output)
    #Find left and right IDs using MPTT
    tree = Trees.Tree(rooted=True)
    (clades, vals) = tree._parse(treeStr)
    tree._add_subtree(0, clades)
    rootNode = tree.node(tree.root)
    nodeIdOfLeftId = {}
    leftIdOfSatchmoId = {}
    mptt(tree, 1, 0, nodeIdOfLeftId)
    #tree.display()
    #print(nodeIdOfLeftId)

    smoFile = smo_output
    #Read from .smo file
    f_smo = open(smoFile)
    smoString = ''.join([line for line in f_smo.readlines()])
    f_smo.close()
    #Find the number of nodes
    index1 = string.find(smoString, ' ')
    index2 = string.find(smoString, '\n')
    no_of_nodes = int(smoString[index1 + 1:index2])
    #Read sequence alignment at each node
    curr_index = 0
    i = 0
    ret = {}
    keys = {}
    no_of_leaves = {}
    while i < no_of_nodes:
        curr_index = string.find(smoString, '[', curr_index)
        leaf_index = string.find(smoString, "leaf", curr_index)
        internal_index = string.find(smoString, "internal", curr_index)

        if ((leaf_index < internal_index) or
            (internal_index == -1)) and (leaf_index != -1):
            nodeType = "leaf"
            #print("Its a leaf!")
        elif ((leaf_index > internal_index) or
              (leaf_index == -1)) and (internal_index != -1):
            nodeType = "internal"
            #print("Its an internal node!")
        else:
            print("Invalid node type!")

        if (nodeType == "leaf"):
            curr_index = string.find(smoString, "index", curr_index)
            newLine_index = string.find(smoString, '\n', curr_index)
            satchmo_id = int(smoString[curr_index + len("index") +
                                       1:newLine_index])
            curr_index = string.find(smoString, '>', curr_index)
            newLine_index = string.find(smoString, '\n', curr_index)
            seq_name = smoString[curr_index + 1:newLine_index]
            for id in nodeIdOfLeftId.keys():
                if (tree.is_terminal(
                        nodeIdOfLeftId[id])) and (seq_name in tree.get_taxa(
                            nodeIdOfLeftId[id])):
                    leftIdOfSatchmoId[satchmo_id] = id
                    populate_ret(ret, id, curr_index, keys, no_of_leaves,
                                 smoString)

        elif (nodeType == "internal"):
            curr_index = string.find(smoString, "index", curr_index)
            newLine_index = string.find(smoString, '\n', curr_index)
            satchmo_id = int(smoString[curr_index + len("index") +
                                       1:newLine_index])
            target_index = string.find(smoString, "target", curr_index)
            newLine_index = string.find(smoString, '\n', target_index)
            target = int(smoString[target_index + len("target") +
                                   1:newLine_index])
            target = nodeIdOfLeftId[leftIdOfSatchmoId[target]]
            #print("Target = ." + str(target) + ".");
            template_index = string.find(smoString, "template", curr_index)
            newLine_index = string.find(smoString, '\n', template_index)
            template = int(smoString[template_index + len("template") +
                                     1:newLine_index])
            template = nodeIdOfLeftId[leftIdOfSatchmoId[template]]
            #print("Template = ." + str(template) + ".")
            #print(tree.node(nodeIdOfLeftId[id]).get_succ())
            for id in nodeIdOfLeftId.keys():
                #print(tree.node(nodeIdOfLeftId[id]).get_succ())
                if(not(tree.is_terminal(nodeIdOfLeftId[id]))) \
                and (template in tree.node(nodeIdOfLeftId[id]).get_succ()) \
                and (target in tree.node(nodeIdOfLeftId[id]).get_succ()) \
                and (len(tree.node(nodeIdOfLeftId[id]).get_succ()) == 2):
                    #print("Template = ." + str(template) + ".")
                    #print(tree.node(nodeIdOfLeftId[id]).get_succ())
                    #print("Adding to id = " + str(id))
                    leftIdOfSatchmoId[satchmo_id] = id
                    curr_index = string.find(smoString, '>', curr_index)
                    populate_ret(ret, id, curr_index, keys, no_of_leaves,
                                 smoString)

        i += 1
        #print(leftIdOfSatchmoId)
        #print(ret)
    for id in nodeIdOfLeftId.keys():
        addColorString(ret, keys, no_of_leaves, id)
        #print(ret)
    ans = _writer.write(ret)
    return ans