Esempio n. 1
0
def tileset_info(request):
    """Get information about a tileset

    Tilesets have information critical to their display
    such as the maximum number of dimensions and well as
    their width. This needs to be relayed to the client
    in order for it to know which tiles to request.

    Args:
        request (django.http.HTTPRequest): The request object
            containing tileset_ids in the 'd' parameter.
    Return:
        django.http.JsonResponse: A JSON object containing
            the tileset meta-information
    """
    queryset = tm.Tileset.objects.all()
    tileset_uuids = request.GET.getlist("d")
    tileset_infos = {}

    chromsizes_error = None

    if "cs" in request.GET:
        # we need to call a different server to get the tiles
        if not "ci" in request.GET.getlist:
            chromsizes_error = "cs param present without ci"

        # call the request server and get the chromsizes
        pass
    else:
        if "ci" in request.GET:
            try:
                chromsizes = tm.Tileset.objects.get(uuid=request.GET["ci"])
                data = tcs.chromsizes_array_to_series(
                    tcs.get_tsv_chromsizes(chromsizes.datafile.path)
                )
            except Exception as ex:
                pass

    for tileset_uuid in tileset_uuids:
        tileset_object = queryset.filter(uuid=tileset_uuid).first()

        if tileset_uuid == "osm-image":
            tileset_infos[tileset_uuid] = {
                "min_x": 0,
                "max_height": 134217728,
                "min_y": 0,
                "max_y": 134217728,
                "max_zoom": 19,
                "tile_size": 256,
            }
            continue

        if tileset_object is None:
            tileset_infos[tileset_uuid] = {
                "error": "No such tileset with uid: {}".format(tileset_uuid)
            }
            continue

        if tileset_object.private and request.user != tileset_object.owner:
            # dataset is not public
            tileset_infos[tileset_uuid] = {"error": "Forbidden"}
            continue

        if tileset_object.requiresAuthentication and not request.user.is_authenticated:
            # dataset is not public
            tileset_infos[tileset_uuid] = {
                "error": "This request required authentication"
            }
            continue
        
        #print(tileset_uuid,"accessibleTilesets" in request.session)
        if tileset_object.requiresAuthentication and (
            ("accessibleTilesets" not in request.session)
            or (tileset_uuid not in request.session["accessibleTilesets"])
        ):
            # dataset is not accessible for this user
            tileset_infos[tileset_uuid] = {
                "error": "You don't have access to this tileset"
            }
            continue

        if tileset_object.filetype == "hitile" or tileset_object.filetype == "hibed":
            tileset_info = hdft.get_tileset_info(
                h5py.File(tileset_object.datafile.path, "r")
            )
            tileset_infos[tileset_uuid] = {
                "min_pos": [int(tileset_info["min_pos"])],
                "max_pos": [int(tileset_info["max_pos"])],
                "max_width": 2
                ** math.ceil(
                    math.log(tileset_info["max_pos"] - tileset_info["min_pos"])
                    / math.log(2)
                ),
                "tile_size": int(tileset_info["tile_size"]),
                "max_zoom": int(tileset_info["max_zoom"]),
            }
        elif tileset_object.filetype == "bigwig":
            chromsizes = tgt.get_chromsizes(tileset_object)
            tsinfo = hgbi.tileset_info(tileset_object.datafile.path, chromsizes)
            # print('tsinfo:', tsinfo)
            if "chromsizes" in tsinfo:
                tsinfo["chromsizes"] = [(c, int(s)) for c, s in tsinfo["chromsizes"]]
            tileset_infos[tileset_uuid] = tsinfo
        elif tileset_object.filetype == "bigbed":
            chromsizes = tgt.get_chromsizes(tileset_object)
            tsinfo = hgbi.tileset_info(tileset_object.datafile.path, chromsizes)
            # print('tsinfo:', tsinfo)
            if "chromsizes" in tsinfo:
                tsinfo["chromsizes"] = [(c, int(s)) for c, s in tsinfo["chromsizes"]]
            tileset_infos[tileset_uuid] = tsinfo
        elif tileset_object.filetype == "multivec":
            tileset_infos[tileset_uuid] = hgmu.tileset_info(
                tileset_object.datafile.path
            )
        elif tileset_object.filetype == "elastic_search":
            response = urllib.urlopen(tileset_object.datafile + "/tileset_info")
            tileset_infos[tileset_uuid] = json.loads(response.read())
        elif tileset_object.filetype == "beddb":
            tileset_infos[tileset_uuid] = cdt.get_tileset_info(
                tileset_object.datafile.path
            )
        elif tileset_object.filetype == "bed2ddb":
            tileset_infos[tileset_uuid] = cdt.get_2d_tileset_info(
                tileset_object.datafile.path
            )
        elif tileset_object.filetype == "cooler":
            tileset_infos[tileset_uuid] = hgco.tileset_info(
                tileset_object.datafile.path
            )
        elif tileset_object.filetype == "time-interval-json":
            tileset_infos[tileset_uuid] = hgti.tileset_info(
                tileset_object.datafile.path
            )
        elif (
            tileset_object.filetype == "2dannodb"
            or tileset_object.filetype == "imtiles"
        ):
            tileset_infos[tileset_uuid] = hgim.get_tileset_info(
                tileset_object.datafile.path
            )
        elif tileset_object.filetype == "geodb":
            tileset_infos[tileset_uuid] = hggo.tileset_info(
                tileset_object.datafile.path
            )
        elif tileset_object.filetype == "bam":
            tileset_infos[tileset_uuid] = ctb.tileset_info(tileset_object.datafile.path)
            tileset_infos[tileset_uuid]["max_tile_width"] = hss.MAX_BAM_TILE_WIDTH
        else:
            # Unknown filetype
            tileset_infos[tileset_uuid] = {
                "error": "Unknown filetype " + tileset_object.filetype
            }

        tileset_infos[tileset_uuid]["name"] = tileset_object.name
        tileset_infos[tileset_uuid]["datatype"] = tileset_object.datatype
        tileset_infos[tileset_uuid]["coordSystem"] = tileset_object.coordSystem
        tileset_infos[tileset_uuid]["coordSystem2"] = tileset_object.coordSystem2

    return JsonResponse(tileset_infos)
Esempio n. 2
0
def tileset_info(request):
    ''' Get information about a tileset

    Tilesets have information critical to their display
    such as the maximum number of dimensions and well as
    their width. This needs to be relayed to the client
    in order for it to know which tiles to request.

    Args:
        request (django.http.HTTPRequest): The request object
            containing tileset_ids in the 'd' parameter.
    Return:
        django.http.JsonResponse: A JSON object containing
            the tileset meta-information
    '''
    queryset = tm.Tileset.objects.all()
    tileset_uuids = request.GET.getlist("d")
    tileset_infos = {}

    chromsizes_error = None

    if 'cs' in request.GET:
        # we need to call a different server to get the tiles
        if not 'ci' in request.GET.getlist:
            chromsizes_error = 'cs param present without ci'

        # call the request server and get the chromsizes
        pass
    else:
        if 'ci' in request.GET:
            try:
                chromsizes = tm.Tileset.objects.get(uuid=request.GET['ci'])
                data = tcs.chromsizes_array_to_series(
                        tcs.get_tsv_chromsizes(chromsizes.datafile.path))
            except Exception as ex:
                pass

    for tileset_uuid in tileset_uuids:
        tileset_object = queryset.filter(uuid=tileset_uuid).first()

        if tileset_uuid == 'osm-image':
            tileset_infos[tileset_uuid] = {
                'min_x': 0,
                'max_height': 134217728,
                'min_y': 0,
                'max_y': 134217728,
                'max_zoom': 19,
                'tile_size': 256
            }
            continue

        if tileset_object is None:
            tileset_infos[tileset_uuid] = {
                'error': 'No such tileset with uid: {}'.format(tileset_uuid)
            }
            continue

        if tileset_object.private and request.user != tileset_object.owner:
            # dataset is not public
            tileset_infos[tileset_uuid] = {'error': "Forbidden"}
            continue

        if (
            tileset_object.filetype == 'hitile' or
            tileset_object.filetype == 'hibed'
        ):
            tileset_info = hdft.get_tileset_info(
                h5py.File(tileset_object.datafile.path, 'r'))
            tileset_infos[tileset_uuid] = {
                "min_pos": [int(tileset_info['min_pos'])],
                "max_pos": [int(tileset_info['max_pos'])],
                "max_width": 2 ** math.ceil(
                    math.log(
                        tileset_info['max_pos'] - tileset_info['min_pos']
                    ) / math.log(2)
                ),
                "tile_size": int(tileset_info['tile_size']),
                "max_zoom": int(tileset_info['max_zoom'])
            }
        elif tileset_object.filetype == 'bigwig':
            chromsizes = tgt.get_chromsizes(tileset_object)
            tsinfo = hgbi.tileset_info(
                    tileset_object.datafile.path,
                    chromsizes
                )
            #print('tsinfo:', tsinfo)
            if 'chromsizes' in tsinfo:
                tsinfo['chromsizes'] = [(c, int(s)) for c,s in tsinfo['chromsizes']]
            tileset_infos[tileset_uuid] = tsinfo
        elif tileset_object.filetype == 'bigbed':
            chromsizes = tgt.get_chromsizes(tileset_object)
            tsinfo = hgbi.tileset_info(
                    tileset_object.datafile.path,
                    chromsizes
                )
            #print('tsinfo:', tsinfo)
            if 'chromsizes' in tsinfo:
                tsinfo['chromsizes'] = [(c, int(s)) for c,s in tsinfo['chromsizes']]
            tileset_infos[tileset_uuid] = tsinfo
        elif tileset_object.filetype == 'multivec':
            tileset_infos[tileset_uuid] = hgmu.tileset_info(
                    tileset_object.datafile.path)
        elif tileset_object.filetype == "elastic_search":
            response = urllib.urlopen(
                tileset_object.datafile + "/tileset_info")
            tileset_infos[tileset_uuid] = json.loads(response.read())
        elif tileset_object.filetype == 'beddb':
            tileset_infos[tileset_uuid] = cdt.get_tileset_info(
                tileset_object.datafile.path
            )
        elif tileset_object.filetype == 'bed2ddb':
            tileset_infos[tileset_uuid] = cdt.get_2d_tileset_info(
                tileset_object.datafile.path
            )
        elif tileset_object.filetype == 'cooler':
            tileset_infos[tileset_uuid] = hgco.tileset_info(
                    tileset_object.datafile.path
            )
        elif tileset_object.filetype == 'time-interval-json':
            tileset_infos[tileset_uuid] = hgti.tileset_info(
                    tileset_object.datafile.path
            )
        elif (
            tileset_object.filetype == '2dannodb' or
            tileset_object.filetype == 'imtiles'
        ):
            tileset_infos[tileset_uuid] = hgim.get_tileset_info(
                tileset_object.datafile.path
            )
        elif tileset_object.filetype == 'geodb':
            tileset_infos[tileset_uuid] = hggo.tileset_info(
                tileset_object.datafile.path
            )
        elif tileset_object.filetype == 'bam':
            tileset_infos[tileset_uuid] = ctb.tileset_info(
                tileset_object.datafile.path
            )
            tileset_infos[tileset_uuid]['max_tile_width'] = hss.MAX_BAM_TILE_WIDTH
        else:
            # Unknown filetype
            tileset_infos[tileset_uuid] = {
                'error': 'Unknown filetype ' + tileset_object.filetype
            }

        tileset_infos[tileset_uuid]['name'] = tileset_object.name
        tileset_infos[tileset_uuid]['datatype'] = tileset_object.datatype
        tileset_infos[tileset_uuid]['coordSystem'] = tileset_object.coordSystem
        tileset_infos[tileset_uuid]['coordSystem2'] =\
            tileset_object.coordSystem2

    return JsonResponse(tileset_infos)