<s> import os <EOL> from os . path import join <EOL> import re <EOL> import tempfile <EOL> import pybamview <EOL> from flask import ( Blueprint , current_app , redirect , render_template , <EOL> request , Response , url_for ) <EOL> from . . utils import message <EOL> NUC_TO_COLOR = { <EOL> "<STR_LIT:A>" : "<STR_LIT>" , <EOL> '<STR_LIT:a>' : "<STR_LIT>" , <EOL> "<STR_LIT:C>" : "<STR_LIT>" , <EOL> "<STR_LIT:c>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:g>" : "<STR_LIT>" , <EOL> "<STR_LIT:T>" : "<STR_LIT>" , <EOL> "<STR_LIT:t>" : "<STR_LIT>" , <EOL> "<STR_LIT:N>" : "<STR_LIT>" , <EOL> "<STR_LIT:n>" : "<STR_LIT>" , <EOL> "<STR_LIT:->" : "<STR_LIT>" , <EOL> "<STR_LIT:.>" : "<STR_LIT>" , <EOL> } <EOL> blueprint = Blueprint ( "<STR_LIT>" , __name__ , template_folder = "<STR_LIT>" , <EOL> static_folder = "<STR_LIT>" ) <EOL> @ blueprint . route ( '<STR_LIT:/>' ) <EOL> def listsamples ( methods = [ '<STR_LIT:POST>' , '<STR_LIT:GET>' ] ) : <EOL> BAM = current_app . config [ '<STR_LIT>' ] <EOL> BAMDIR = current_app . config [ '<STR_LIT>' ] <EOL> if BAM : <EOL> if not os . path . exists ( BAM ) or not pybamview . CheckBam ( BAM ) : <EOL> return render_template ( "<STR_LIT>" , message = "<STR_LIT>" % BAM ) <EOL> try : <EOL> samplesToBam = pybamview . GetSamplesFromBamFiles ( [ os . path . abspath ( BAM ) ] ) <EOL> except ValueError , e : <EOL> return render_template ( "<STR_LIT>" , message = "<STR_LIT>" % e , title = "<STR_LIT>" % BAM ) <EOL> if not ( os . path . exists ( BAM + "<STR_LIT>" ) or os . path . exists ( BAM . replace ( "<STR_LIT>" , "<STR_LIT>" ) ) ) : <EOL> return render_template ( "<STR_LIT>" , message = "<STR_LIT>" % BAM ) <EOL> if len ( samplesToBam . keys ( ) ) == <NUM_LIT:0> : <EOL> return render_template ( "<STR_LIT>" , message = <EOL> "<STR_LIT>" , <EOL> title = "<STR_LIT>" % BAM ) <EOL> argstring = "<STR_LIT:&>" . join ( [ "<STR_LIT>" % ( sample , os . path . abspath ( BAM ) ) for sample in samplesToBam ] ) <EOL> return redirect ( url_for ( "<STR_LIT>" ) + "<STR_LIT:?>" + argstring ) <EOL> else : <EOL> bamfiles = request . args . getlist ( "<STR_LIT>" ) <EOL> samples = request . args . getlist ( "<STR_LIT>" ) <EOL> if len ( bamfiles ) > <NUM_LIT:0> and len ( samples ) > <NUM_LIT:0> : <EOL> return display_bam ( samples ) <EOL> try : <EOL> files = os . listdir ( BAMDIR ) <EOL> except OSError : files = [ ] <EOL> bamfiles = [ f for f in files if re . match ( "<STR_LIT>" , f ) is not None ] <EOL> bamfiles = [ f for f in bamfiles if ( ( f + "<STR_LIT>" ) in files or f . replace ( "<STR_LIT>" , "<STR_LIT>" ) in files ) ] <EOL> try : <EOL> samplesToBam = pybamview . GetSamplesFromBamFiles ( [ os . path . join ( os . path . abspath ( BAMDIR ) , b ) for b in bamfiles ] ) <EOL> except ValueError , e : <EOL> return render_template ( "<STR_LIT>" , message = "<STR_LIT>" % e , title = "<STR_LIT>" % BAMDIR ) <EOL> return render_template ( "<STR_LIT>" , samplesToBam = samplesToBam , title = "<STR_LIT>" % BAMDIR ) <EOL> @ blueprint . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' , '<STR_LIT:GET>' ] ) <EOL> def display_bam ( ) : <EOL> samplebams = request . args . getlist ( "<STR_LIT>" ) <EOL> zoomlevel = request . args . get ( "<STR_LIT>" ) <EOL> if len ( samplebams ) == <NUM_LIT:0> : <EOL> samples_toinclude = list ( set ( request . args . getlist ( "<STR_LIT>" ) ) ) <EOL> bamfiles_toinclude = list ( set ( request . args . getlist ( "<STR_LIT>" ) ) ) <EOL> else : <EOL> samples_toinclude = [ ] <EOL> bamfiles_toinclude = [ ] <EOL> for s in samplebams : <EOL> samples_toinclude . append ( s . split ( "<STR_LIT::>" ) [ <NUM_LIT:0> ] ) <EOL> for item in s . split ( "<STR_LIT::>" ) [ <NUM_LIT:1> : ] : <EOL> bamfiles_toinclude . extend ( item . split ( "<STR_LIT:U+002C>" ) ) <EOL> region = request . args . get ( "<STR_LIT>" , pybamview . GetDefaultLocation ( bamfiles_toinclude ) ) <EOL> return display_bam_region ( list ( set ( bamfiles_toinclude ) ) , samples_toinclude , region , zoomlevel ) <EOL> def display_bam_region ( bamfiles , samples , region , zoomlevel ) : <EOL> bamdir = current_app . config [ "<STR_LIT>" ] <EOL> reffile = current_app . config [ "<STR_LIT>" ] <EOL> settings = current_app . config [ "<STR_LIT>" ] <EOL> bamfile_to_bamview = current_app . config [ "<STR_LIT>" ] <EOL> if region == "<STR_LIT:error>" : <EOL> return render_template ( "<STR_LIT>" , message = "<STR_LIT>" ) <EOL> for bam in bamfiles : <EOL> if not os . path . exists ( join ( bamdir , bam ) ) : <EOL> message ( "<STR_LIT>" % join ( bamdir , bam ) , "<STR_LIT>" ) <EOL> if "<STR_LIT:;>" . join ( bamfiles ) not in bamfile_to_bamview : <EOL> bv = pybamview . BamView ( [ join ( bamdir , bam ) for bam in bamfiles ] , reffile ) <EOL> bamfile_to_bamview [ "<STR_LIT:;>" . join ( bamfiles ) ] = bv <EOL> else : <EOL> bv = bamfile_to_bamview [ "<STR_LIT:;>" . join ( bamfiles ) ] <EOL> try : <EOL> chrom , pos = region . split ( "<STR_LIT::>" ) <EOL> pos = int ( pos ) <EOL> except : <EOL> return render_template ( "<STR_LIT>" , message = "<STR_LIT>" % region ) <EOL> bv . LoadAlignmentGrid ( chrom , pos , _samples = samples , _settings = settings ) <EOL> positions = bv . GetPositions ( pos ) <EOL> region = "<STR_LIT>" % ( chrom , pos ) <EOL> return render_template ( "<STR_LIT>" , <EOL> title = "<STR_LIT>" % region , <EOL> BAM_FILES = bamfiles , <EOL> REF_FILE = reffile , <EOL> REGION = region , <EOL> SAMPLES = bv . GetSamples ( ) , <EOL> SAMPLE_HASHES = bv . GetSampleHashes ( ) , <EOL> BUFFER = settings [ "<STR_LIT>" ] , <EOL> MAXZOOM = settings [ "<STR_LIT>" ] , <EOL> ZOOMLEVEL = zoomlevel , <EOL> REFERENCE = bv . GetReferenceTrack ( pos ) , <EOL> ALIGNMENTS = bv . GetAlignmentTrack ( pos ) , <EOL> TARGETPOS = pos , <EOL> POSITIONS = positions , <EOL> NUC_TO_COLOR = NUC_TO_COLOR , <EOL> CHROM = chrom , <EOL> TARGET_LIST = current_app . config . get ( "<STR_LIT>" , [ ] ) <EOL> ) <EOL> @ blueprint . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' , '<STR_LIT:GET>' ] ) <EOL> def snapshot ( ) : <EOL> settings = current_app . config [ '<STR_LIT>' ] <EOL> samples = request . form . getlist ( "<STR_LIT>" ) <EOL> alignments_by_sample = { } <EOL> for s in samples : <EOL> alignments_by_sample [ s ] = request . form [ "<STR_LIT>" % s ] <EOL> region = request . form [ "<STR_LIT>" ] <EOL> region . replace ( "<STR_LIT>" , "<STR_LIT::>" ) <EOL> startpos = request . form [ "<STR_LIT>" ] <EOL> reference = request . form [ "<STR_LIT>" ] <EOL> zoom = request . form [ "<STR_LIT>" ] <EOL> zoomlevel = float ( zoom ) <EOL> if zoomlevel < <NUM_LIT:0> : <EOL> zoomlevel = - <NUM_LIT:1> / zoomlevel <EOL> chrom , start = region . split ( "<STR_LIT::>" ) <EOL> region = "<STR_LIT>" % ( max ( int ( int ( start ) - <NUM_LIT> / zoomlevel ) , <NUM_LIT:0> ) , int ( int ( start ) + <NUM_LIT:50> + <NUM_LIT> / zoomlevel ) ) <EOL> return render_template ( "<STR_LIT>" , title = "<STR_LIT>" , SAMPLES = samples , ZOOMLEVEL = zoom , CHROM = chrom , REGION = region , MINSTART = int ( start ) - settings [ "<STR_LIT>" ] / <NUM_LIT:2> , MAXEND = int ( start ) + settings [ "<STR_LIT>" ] / <NUM_LIT:2> , REFERENCE_TRACK = reference , ALIGN_BY_SAMPLE = alignments_by_sample , STARTPOS = startpos ) <EOL> @ blueprint . route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def export ( ) : <EOL> svg_xml = request . form . get ( "<STR_LIT:data>" , "<STR_LIT>" ) <EOL> input_file = tempfile . NamedTemporaryFile ( delete = False ) <EOL> input_file . write ( svg_xml ) <EOL> input_file . close ( ) <EOL> filename = request . form . get ( "<STR_LIT:filename>" , "<STR_LIT>" ) <EOL> output_file_name = os . path . join ( "<STR_LIT>" , filename ) <EOL> cmd = "<STR_LIT>" % ( output_file_name , input_file . name ) <EOL> retcode = os . system ( cmd ) <EOL> if retcode != <NUM_LIT:0> : <EOL> message ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if os . path . exists ( output_file_name ) : <EOL> pdf_data = open ( output_file_name , "<STR_LIT:r>" ) . read ( ) <EOL> else : pdf_data = "<STR_LIT>" <EOL> response = Response ( pdf_data , mimetype = "<STR_LIT>" ) <EOL> response . headers [ "<STR_LIT>" ] = "<STR_LIT>" % filename <EOL> return response </s>
