예제 #1
0
def sizes(request):
    """Return chromosome sizes.
    Retrieves the chromSiyes.tsv and either retrieves it as is or converts it
    to a JSON format.
    Args:
        request: HTTP GET request object. The request can feature the following
            queries:
            id: id of the stored chromSizes [e.g.: hg19 or mm9]
            type: return data format [tsv or json]
            cum: return cumulative size or offset [0 or 1]
    Returns:
        A HTTP text or JSON response depending on the GET request.
        A text response looks like this:
        ```
        chr1    1
        chr2    2
        ...
        ```
        A JSON response looks like this:
        ```
        {
            chr1: {
                size: 1,
                offset: 0
            }
            chr2: {
                size: 2,
                offset: 1
            },
            ...
        }
        ```
    """
    uuid = request.GET.get("id", False)
    res_type = request.GET.get("type", "tsv")
    incl_cum = request.GET.get("cum", False)

    response = HttpResponse
    is_json = False

    if res_type == "json":
        is_json = True
        response = JsonResponse

    if res_type != "json" and incl_cum:
        return response(
            "Sorry buddy. Cumulative sizes not yet supported for non-JSON "
            "file types. 😞",
            status=501,
        )

    # Try to find the db entry
    try:
        chrom_sizes = tm.Tileset.objects.get(uuid=uuid)
    except Exception as e:
        logger.exception(e)
        err_msg = "Oh lord! ChromSizes for %s not found. 😬" % uuid
        err_status = 404

        if is_json:
            return response({"error": err_msg}, status=err_status)

        return response(err_msg, status=err_status)

    # Try to load the chromosome sizes and return them as a list of
    # (name, size) tuples
    try:
        if tgt.get_tileset_filetype(chrom_sizes) == "bigwig":
            data = hgbi.chromsizes(chrom_sizes.datafile.path)
        elif tgt.get_tileset_filetype(chrom_sizes) == "bigbed":
            data = hgbb.chromsizes(chrom_sizes.datafile.path)
        elif tgt.get_tileset_filetype(chrom_sizes) == "cooler":
            data = tcs.get_cooler_chromsizes(chrom_sizes.datafile.path)
        elif tgt.get_tileset_filetype(chrom_sizes) == "chromsizes-tsv":
            data = tcs.get_tsv_chromsizes(chrom_sizes.datafile.path)
        elif tgt.get_tileset_filetype(chrom_sizes) == "multivec":
            data = tcs.get_multivec_chromsizes(chrom_sizes.datafile.path)
        else:
            data = ""

    except Exception as ex:
        logger.exception(ex)
        err_msg = str(ex)
        err_status = 500

        if is_json:
            return response({"error": err_msg}, status=err_status)

        return response(err_msg, status=err_status)

    # Convert the stuff if needed
    try:
        # data should be a list of (name, size) tuples coming
        # coming and converted to a more appropriate data type
        # going out
        if res_type == "tsv":
            lines = []
            for (name, size) in data:
                lines += ["{}\t{}\n".format(name, size)]
                data = lines

        if res_type == "json" and not incl_cum:
            json_out = {}

            for row in data:
                json_out[row[0]] = {"size": int(row[1])}

            data = json_out

        if res_type == "json" and incl_cum:
            json_out = {}
            cum = 0

            for row in data:
                size = int(row[1])

                json_out[row[0]] = {"size": size, "offset": cum}
                cum += size

            data = json_out
    except Exception as e:
        logger.exception(e)
        err_msg = "THIS IS AN OUTRAGE!!!1! Something failed. 😡"
        err_status = 500

        if is_json:
            return response({"error": err_msg}, status=err_status)

        return response(err_msg, status=err_status)

    return response(data)
예제 #2
0
파일: bigbed.py 프로젝트: sergpolly/clodius
def chromsizes(filename):
    return hgbw.chromsizes(filename)