<commit_before>import bx.align.maf
import cStringIO
import os

from flask import Flask, request
ENVSETTINGSVAR = 'ALIGNMENTS_SETTINGS'
app = Flask(__name__)
app.config.from_object('regvar.webapps.alignments.DevelopmentConfig')
if ENVSETTINGSVAR in os.environ:
    app.config.from_envvar(ENVSETTINGSVAR)


def get_maf(genome, alignment, chrom):
    return os.path.join(
        app.config['UCSC_DIR'], 'goldenPath', genome,
        alignment, 'maf', '{0}.maf.bz2'.format(chrom))


@app.route('/alignment/<genome>/<alignment>/<chrom>/<int:start>/<int:end>')
def alignment(genome, alignment, chrom, start, end):
    chop = bool(int(request.headers.get('Chop', 0)))
    mincols = int(request.headers.get('Min-Cols', 0))
    src = '{0}.{1}'.format(genome, chrom)
    # app.logger.info(request.headers.get('User-Agent'))
    # app.logger.info(chop)
    index = bx.align.maf.MultiIndexed(
        [get_maf(genome, alignment, chrom)],
        keep_open=True,
        parse_e_rows=True,
        use_cache=True)
    # Write MAF into string
    output = cStringIO.StringIO()
    out = bx.align.maf.Writer(output)
    strand = None
    # Find overlap with reference component
    blocks = index.get(src, start, end)
    # Write each intersecting block
    if chop:
        for block in blocks:
            ref = block.get_component_by_src(src)
            slice_start = max(start, ref.get_forward_strand_start())
            slice_end = min(end, ref.get_forward_strand_end())
            sliced = block.slice_by_component(ref, slice_start, slice_end)
            # If the block is shorter than the minimum allowed size, stop
            if mincols and (sliced.text_size < mincols):
                continue
            # If the reference component is empty, don't write the block
            if sliced.get_component_by_src(src).size < 1:
                continue
            # Keep only components that are not empty
            sliced.components = [c for c in sliced.components if c.size > 0]
            # Reverse complement if needed
            if (strand is not None) and (ref.strand != strand):
                sliced = sliced.reverse_complement()
            # Write the block
            out.write(sliced)
    else:
        for block in blocks:
            out.write(block)
    result = output.getvalue()
    output.close()
    # Close output MAF
    index.close()
    out.close()
    return result

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9083)
<commit_msg>Add comment<commit_after>import bx.align.maf
import cStringIO
import os

from flask import Flask, request
ENVSETTINGSVAR = 'ALIGNMENTS_SETTINGS'
app = Flask(__name__)
#
# Change this to ProductionConfig when finished
app.config.from_object('regvar.webapps.alignments.DevelopmentConfig')
if ENVSETTINGSVAR in os.environ:
    app.config.from_envvar(ENVSETTINGSVAR)


def get_maf(genome, alignment, chrom):
    return os.path.join(
        app.config['UCSC_DIR'], 'goldenPath', genome,
        alignment, 'maf', '{0}.maf.bz2'.format(chrom))


@app.route('/alignment/<genome>/<alignment>/<chrom>/<int:start>/<int:end>')
def alignment(genome, alignment, chrom, start, end):
    chop = bool(int(request.headers.get('Chop', 0)))
    mincols = int(request.headers.get('Min-Cols', 0))
    src = '{0}.{1}'.format(genome, chrom)
    # app.logger.info(request.headers.get('User-Agent'))
    # app.logger.info(chop)
    index = bx.align.maf.MultiIndexed(
        [get_maf(genome, alignment, chrom)],
        keep_open=True,
        parse_e_rows=True,
        use_cache=True)
    # Write MAF into string
    output = cStringIO.StringIO()
    out = bx.align.maf.Writer(output)
    strand = None
    # Find overlap with reference component
    blocks = index.get(src, start, end)
    # Write each intersecting block
    if chop:
        for block in blocks:
            ref = block.get_component_by_src(src)
            slice_start = max(start, ref.get_forward_strand_start())
            slice_end = min(end, ref.get_forward_strand_end())
            sliced = block.slice_by_component(ref, slice_start, slice_end)
            # If the block is shorter than the minimum allowed size, stop
            if mincols and (sliced.text_size < mincols):
                continue
            # If the reference component is empty, don't write the block
            if sliced.get_component_by_src(src).size < 1:
                continue
            # Keep only components that are not empty
            sliced.components = [c for c in sliced.components if c.size > 0]
            # Reverse complement if needed
            if (strand is not None) and (ref.strand != strand):
                sliced = sliced.reverse_complement()
            # Write the block
            out.write(sliced)
    else:
        for block in blocks:
            out.write(block)
    result = output.getvalue()
    output.close()
    # Close output MAF
    index.close()
    out.close()
    return result

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9083)

{'fraction_non_alphanumeric': 0.11329063250600481, 'fraction_numerical': 0.006405124099279423, 'mean_word_length': 2.6474452554744525, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '126911', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1552, 'n_words': 379}