#NEWLINE# def _update_relations( self ) : #NEWLINE# #INDENT# if ( self . disable_infer_genes and self . disable_infer_transcripts ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# c = self . conn . cursor ( ) #NEWLINE# c2 = self . conn . cursor ( ) #NEWLINE# logger . info ( 'Creating relations(parent) index' ) #NEWLINE# c . execute ( 'DROP INDEX IF EXISTS relationsparent' ) #NEWLINE# c . execute ( 'CREATE INDEX relationsparent ON relations (parent)' ) #NEWLINE# logger . info ( 'Creating relations(child) index' ) #NEWLINE# c . execute ( 'DROP INDEX IF EXISTS relationschild' ) #NEWLINE# c . execute ( 'CREATE INDEX relationschild ON relations (child)' ) #NEWLINE# if ( not ( self . disable_infer_genes or self . disable_infer_transcripts ) ) : #NEWLINE# #INDENT# msg = 'gene and transcript' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if self . disable_infer_transcripts : #NEWLINE# #INDENT# msg = 'gene' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if self . disable_infer_genes : #NEWLINE# #INDENT# msg = 'transcript' #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# logger . info ( ( 'Inferring %s extents and writing to tempfile' % msg ) ) #NEWLINE# if isinstance ( self . _keep_tempfiles , six . string_types ) : #NEWLINE# #INDENT# suffix = self . _keep_tempfiles #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# suffix = '.gffutils' #NEWLINE# #UNINDENT# tmp = tempfile . NamedTemporaryFile ( delete = False , suffix = suffix ) . name #NEWLINE# fout = open ( tmp , 'w' ) #NEWLINE# self . _tmpfile = tmp #NEWLINE# c . execute ( '\n            SELECT DISTINCT firstlevel.parent, relations.parent\n            FROM (\n                SELECT DISTINCT parent\n                FROM relations\n                JOIN features ON features.id = relations.child\n                WHERE features.featuretype = ?\n                AND relations.level = 1\n            )\n            AS firstlevel\n            JOIN relations ON firstlevel.parent = child\n            WHERE relations.level = 1\n            ORDER BY relations.parent\n            ' , ( self . subfeature , ) ) #NEWLINE# last_gene_id = None #NEWLINE# n_features = 0 #NEWLINE# for ( transcript_id , gene_id ) in c : #NEWLINE# #INDENT# if ( not self . disable_infer_transcripts ) : #NEWLINE# #INDENT# c2 . execute ( '\n                    SELECT MIN(start), MAX(end), strand, seqid\n                    FROM features\n                    JOIN relations ON\n                    features.id = relations.child\n                    WHERE parent = ? AND featuretype == ?\n                    ' , ( transcript_id , self . subfeature ) ) #NEWLINE# ( transcript_start , transcript_end , strand , seqid ) = c2 . fetchone ( ) #NEWLINE# transcript_attributes = { #NEWLINE# #INDENT# self . transcript_key : [ transcript_id ] , #NEWLINE# self . gene_key : [ gene_id ] , #NEWLINE# #UNINDENT# } #NEWLINE# transcript_bin = bins . bins ( transcript_start , transcript_end , one = True ) #NEWLINE# fout . write ( ( '\t' . join ( map ( str , [ transcript_id , seqid , transcript_start , transcript_end , strand , 'transcript' , transcript_bin , helpers . _jsonify ( transcript_attributes ) ] ) ) + '\n' ) ) #NEWLINE# n_features += 1 #NEWLINE# #UNINDENT# if ( not self . disable_infer_genes ) : #NEWLINE# #INDENT# if ( gene_id != last_gene_id ) : #NEWLINE# #INDENT# c2 . execute ( '\n                        SELECT MIN(start), MAX(end), strand, seqid\n                        FROM features\n                        JOIN relations ON\n                        features.id = relations.child\n                        WHERE parent = ? AND featuretype == ?\n                        ' , ( gene_id , self . subfeature ) ) #NEWLINE# ( gene_start , gene_end , strand , seqid ) = c2 . fetchone ( ) #NEWLINE# gene_attributes = { #NEWLINE# #INDENT# self . gene_key : [ gene_id ] , #NEWLINE# #UNINDENT# } #NEWLINE# gene_bin = bins . bins ( gene_start , gene_end , one = True ) #NEWLINE# fout . write ( ( '\t' . join ( map ( str , [ gene_id , seqid , transcript_attributes , gene_end , strand , 'gene' , gene_bin , helpers . _jsonify ( gene_attributes ) ] ) ) + '\n' ) ) #NEWLINE# #UNINDENT# last_gene_id = gene_id #NEWLINE# n_features += 1 #NEWLINE# #UNINDENT# #UNINDENT# fout . close ( ) #NEWLINE# def derived_feature_generator( ) : #NEWLINE# #INDENT# '\n            Generator of items from the file that was just created...\n            ' #NEWLINE# keys = [ 'parent' , 'seqid' , 'start' , 'end' , 'strand' , 'featuretype' , 'bin' , 'attributes' ] #NEWLINE# for line in open ( fout . name ) : #NEWLINE# #INDENT# d = dict ( list ( zip ( keys , line . strip ( ) . split ( '\t' ) ) ) ) #NEWLINE# d . pop ( 'parent' ) #NEWLINE# d [ 'score' ] = '.' #NEWLINE# d [ 'source' ] = 'gffutils_derived' #NEWLINE# d [ 'frame' ] = '.' #NEWLINE# d [ 'extra' ] = [ ] #NEWLINE# d [ 'attributes' ] = helpers . _unjsonify ( d [ 'attributes' ] ) #NEWLINE# f = feature . Feature ( ** d ) #NEWLINE# f . id = self . _id_handler ( f ) #NEWLINE# ( yield f ) #NEWLINE# #UNINDENT# #UNINDENT# c . execute ( 'DROP INDEX IF EXISTS relationsparent' ) #NEWLINE# c . execute ( 'DROP INDEX IF EXISTS relationschild' ) #NEWLINE# logger . info ( 'Importing inferred features into db' ) #NEWLINE# last_perc = None #NEWLINE# for ( i , f ) in enumerate ( derived_feature_generator ( ) ) : #NEWLINE# #INDENT# perc = int ( ( ( i / float ( n_features ) ) * 100 ) ) #NEWLINE# if ( perc != last_perc ) : #NEWLINE# #INDENT# sys . stderr . write ( ( '%s of %s (%s%%)\r' % ( i , n_features , perc ) ) ) #NEWLINE# sys . stderr . flush ( ) #NEWLINE# #UNINDENT# last_perc = perc #NEWLINE# try : #NEWLINE# #INDENT# self . _insert ( f , c ) #NEWLINE# #UNINDENT# except sqlite3 . IntegrityError : #NEWLINE# #INDENT# ( fixed , final_strategy ) = self . _do_merge ( f , 'merge' ) #NEWLINE# c . execute ( '\n                    UPDATE features SET attributes = ?\n                    WHERE id = ?\n                    ' , ( helpers . _jsonify ( fixed . attributes ) , fixed . id ) ) #NEWLINE# #UNINDENT# #UNINDENT# logger . info ( 'Committing changes' ) #NEWLINE# self . conn . commit ( ) #NEWLINE# if ( not self . _keep_tempfiles ) : #NEWLINE# #INDENT# os . unlink ( fout . name )