<s> import logging <EOL> from optparse import make_option <EOL> import os <EOL> import gzip <EOL> from django . conf import settings <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from annotation_server import models <EOL> from annotation_server import utils <EOL> from file_store . models import _mkdir <EOL> """<STR_LIT>""" <EOL> logger = logging . getLogger ( __name__ ) <EOL> def parse_wig_attribute ( wig ) : <EOL> """<STR_LIT>""" <EOL> ret_dict = { } <EOL> t2 = wig . strip ( ) . split ( "<STR_LIT:U+0020>" ) <EOL> for item in t2 : <EOL> t3 = item . split ( "<STR_LIT:=>" ) <EOL> if len ( t3 ) == <NUM_LIT:2> : <EOL> ret_dict [ t3 [ <NUM_LIT:0> ] ] = t3 [ <NUM_LIT:1> ] . strip ( '<STR_LIT:">' ) <EOL> return ret_dict <EOL> def parse_gff_attribute ( attr ) : <EOL> """<STR_LIT>""" <EOL> t2 = attr . strip ( ) . split ( "<STR_LIT:;>" ) <EOL> ret_dict = { } <EOL> k = None <EOL> for item in t2 : <EOL> t3 = item . strip ( ) . split ( "<STR_LIT:=>" ) <EOL> if len ( t3 ) != <NUM_LIT:2> : <EOL> t3 = item . strip ( ) . split ( "<STR_LIT:U+0020>" ) <EOL> if len ( t3 ) == <NUM_LIT:2> : <EOL> k = t3 [ <NUM_LIT:0> ] <EOL> v = t3 [ <NUM_LIT:1> ] . strip ( '<STR_LIT:">' ) . strip ( "<STR_LIT:'>" ) <EOL> ret_dict [ k ] = v <EOL> return ret_dict <EOL> def parse_db_string ( attr , sym ) : <EOL> """<STR_LIT>""" <EOL> ret_string = '<STR_LIT>' <EOL> for var in sym : <EOL> if var in attr : <EOL> if ret_string : <EOL> ret_string += '<STR_LIT>' % ( var , attr [ var ] ) <EOL> else : <EOL> ret_string += '<STR_LIT>' % ( var , attr [ var ] ) <EOL> return ret_string <EOL> def getFileHandle ( file_in ) : <EOL> """<STR_LIT>""" <EOL> if file_in . endswith ( '<STR_LIT>' ) : <EOL> handle = gzip . open ( file_in ) <EOL> else : <EOL> handle = open ( file_in , '<STR_LIT:r>' ) <EOL> return handle <EOL> class Command ( BaseCommand ) : <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> can_import_settings = True <EOL> GENOME_BUILD = None <EOL> ANNOTATION_DIR = '<STR_LIT>' <EOL> ANNOTATION_BASE_DIR = os . path . join ( settings . MEDIA_ROOT , ANNOTATION_DIR ) <EOL> if not os . path . isdir ( ANNOTATION_BASE_DIR ) : <EOL> _mkdir ( ANNOTATION_BASE_DIR ) <EOL> def handle ( self , * args , ** options ) : <EOL> if options [ '<STR_LIT>' ] : <EOL> self . GENOME_BUILD = options [ '<STR_LIT>' ] . strip ( ) <EOL> if self . GENOME_BUILD in utils . SUPPORTED_GENOMES : <EOL> self . ANNOTATION_TEMP_DIR = os . path . join ( <EOL> self . ANNOTATION_BASE_DIR , self . GENOME_BUILD ) <EOL> if not os . path . isdir ( self . ANNOTATION_TEMP_DIR ) : <EOL> _mkdir ( self . ANNOTATION_TEMP_DIR ) <EOL> if options [ '<STR_LIT>' ] : <EOL> self . geneAnnotation ( options [ '<STR_LIT>' ] ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> self . mappabilityEmpirical ( options [ '<STR_LIT>' ] ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> self . mappabilityTheoretical ( options [ '<STR_LIT>' ] ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> self . gapRegions ( options [ '<STR_LIT>' ] ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> self . gcContent ( options [ '<STR_LIT>' ] ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> self . conservation ( options [ '<STR_LIT>' ] ) <EOL> else : <EOL> raise CommandError ( <EOL> '<STR_LIT>' ) <EOL> else : <EOL> raise CommandError ( <EOL> '<STR_LIT>' ) <EOL> def gcContent ( self , wig_file ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , self . GENOME_BUILD , wig_file ) <EOL> db_table = self . GENOME_BUILD + "<STR_LIT>" <EOL> self . addWigAnnotation ( wig_file , db_table , "<STR_LIT>" ) <EOL> def conservation ( self , wig_file ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , self . GENOME_BUILD , wig_file ) <EOL> db_table = self . GENOME_BUILD + "<STR_LIT>" <EOL> self . addWigAnnotation ( wig_file , db_table , "<STR_LIT>" ) <EOL> def addWigAnnotation ( self , wig_file , db_model , annot_type ) : <EOL> """<STR_LIT>""" <EOL> current_table = eval ( db_model ) <EOL> current_table . objects . all ( ) . delete ( ) <EOL> handle = getFileHandle ( wig_file ) <EOL> for line in handle : <EOL> if line [ <NUM_LIT:0> ] != '<STR_LIT:#>' : <EOL> t1 = line . strip ( ) . split ( '<STR_LIT:U+0020>' ) <EOL> if t1 [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> try : <EOL> item = models . WigDescription . objects . get ( <EOL> genome_build = self . GENOME_BUILD , <EOL> annotation_type = annot_type <EOL> ) <EOL> item . delete ( ) <EOL> except ObjectDoesNotExist : <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> self . GENOME_BUILD , annot_type ) <EOL> ret_attr = parse_wig_attribute ( line ) <EOL> table_vals = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:type>' , '<STR_LIT:description>' ] <EOL> db_string = "<STR_LIT>" "<STR_LIT>" <EOL> db_string = db_string % ( <EOL> self . GENOME_BUILD , annot_type , parse_db_string ( <EOL> ret_attr , table_vals ) <EOL> ) <EOL> item = eval ( db_string ) <EOL> item . save ( ) <EOL> elif t1 [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> attr = parse_wig_attribute ( line ) <EOL> chrom = attr [ '<STR_LIT>' ] <EOL> start = int ( attr [ '<STR_LIT:start>' ] ) <EOL> step = int ( attr [ '<STR_LIT>' ] ) <EOL> curr_pos = start <EOL> else : <EOL> curr_pos += step <EOL> wigItem = current_table ( annot = item , chrom = chrom , <EOL> position = curr_pos , value = t1 [ <NUM_LIT:0> ] ) <EOL> wigItem . save ( ) <EOL> def mappabilityTheoretical ( self , bed_file ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , self . GENOME_BUILD , bed_file ) <EOL> if self . GENOME_BUILD == '<STR_LIT>' : <EOL> db_table = self . GENOME_BUILD + "<STR_LIT>" <EOL> self . addBedAnnotation ( bed_file , db_table ) <EOL> else : <EOL> raise CommandError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> def mappabilityEmpirical ( self , bed_file ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , self . GENOME_BUILD , bed_file ) <EOL> db_table = self . GENOME_BUILD + "<STR_LIT>" <EOL> if ( self . GENOME_BUILD == '<STR_LIT>' or <EOL> self . GENOME_BUILD == '<STR_LIT>' or <EOL> self . GENOME_BUILD == '<STR_LIT>' ) : <EOL> self . addBedAnnotation ( bed_file , db_table ) <EOL> else : <EOL> raise CommandError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> def gapRegions ( self , bed_file ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , self . GENOME_BUILD , bed_file ) <EOL> db_table = self . GENOME_BUILD + "<STR_LIT>" <EOL> if self . GENOME_BUILD == '<STR_LIT>' or self . GENOME_BUILD == '<STR_LIT>' : <EOL> self . addGapRegions ( bed_file , db_table ) <EOL> else : <EOL> raise CommandError ( <EOL> '<STR_LIT>' ) <EOL> def geneAnnotation ( self , gff_file ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , self . GENOME_BUILD , gff_file ) <EOL> if self . GENOME_BUILD == '<STR_LIT>' : <EOL> db_table = self . GENOME_BUILD + "<STR_LIT>" <EOL> db_vars = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> self . addGeneAnnotation ( gff_file , db_table , db_vars ) <EOL> elif self . GENOME_BUILD == '<STR_LIT>' : <EOL> db_table = self . GENOME_BUILD + "<STR_LIT>" <EOL> db_vars = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:Name>' ] <EOL> self . addGeneAnnotation ( gff_file , db_table , db_vars ) <EOL> elif self . GENOME_BUILD == '<STR_LIT>' : <EOL> db_table = self . GENOME_BUILD + "<STR_LIT>" <EOL> db_vars = [ '<STR_LIT>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . addGeneAnnotation ( gff_file , db_table , db_vars ) <EOL> def addGeneAnnotation ( self , gff_file , db_model , table_vals ) : <EOL> """<STR_LIT>""" <EOL> current_table = eval ( db_model ) <EOL> current_table . objects . all ( ) . delete ( ) <EOL> handle = getFileHandle ( gff_file ) <EOL> for line in handle : <EOL> if line [ <NUM_LIT:0> ] != '<STR_LIT:#>' : <EOL> t1 = line . strip ( ) . split ( '<STR_LIT:\t>' ) <EOL> attrib = parse_gff_attribute ( t1 [ <NUM_LIT:8> ] ) <EOL> db_string = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> parse_db_string ( attrib , table_vals ) <EOL> db_string = db_string % ( parse_db_string ( attrib , table_vals ) ) <EOL> item = eval ( db_string ) <EOL> item . save ( ) <EOL> def addGapRegions ( self , bed_file , db_model ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> self . GENOME_BUILD , bed_file , db_model ) <EOL> current_table = eval ( db_model ) <EOL> current_table . objects . all ( ) . delete ( ) <EOL> handle = getFileHandle ( bed_file ) <EOL> for line in handle : <EOL> if line [ <NUM_LIT:0> ] != '<STR_LIT:#>' : <EOL> t1 = line . strip ( ) . split ( '<STR_LIT:\t>' ) <EOL> item = current_table ( bin = t1 [ <NUM_LIT:0> ] , chrom = t1 [ <NUM_LIT:1> ] , chromStart = t1 [ <NUM_LIT:2> ] , <EOL> chromEnd = t1 [ <NUM_LIT:3> ] , ix = t1 [ <NUM_LIT:4> ] , n = t1 [ <NUM_LIT:5> ] , <EOL> size = t1 [ <NUM_LIT:6> ] , type = t1 [ <NUM_LIT:7> ] , bridge = t1 [ <NUM_LIT:8> ] ) <EOL> item . save ( ) <EOL> def addBedAnnotation ( self , bed_file , db_model ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> self . GENOME_BUILD , bed_file , db_model ) <EOL> current_table = eval ( db_model ) <EOL> current_table . objects . all ( ) . delete ( ) <EOL> handle = getFileHandle ( bed_file ) <EOL> for line in handle : <EOL> if line [ <NUM_LIT:0> ] != '<STR_LIT:#>' : <EOL> t1 = line . strip ( ) . split ( '<STR_LIT:\t>' ) <EOL> item = current_table ( <EOL> chrom = t1 [ <NUM_LIT:0> ] , chromStart = t1 [ <NUM_LIT:1> ] , chromEnd = t1 [ <NUM_LIT:2> ] ) <EOL> item . save ( ) </s>
