# Path: grit/files/gtf.py
# def load_gtf(fname_or_fp, contig=None, strand=None):
#     if isinstance( fname_or_fp, str ):
#         fp = open( fname_or_fp )
#     else:
#         assert isinstance( fname_or_fp, (file, gzip.GzipFile) )
#         fp = fname_or_fp
#     
#     gene_lines = defaultdict(lambda: ( defaultdict(list), [] ))
#     for line in fp:
#         data = parse_gtf_line(line, fix_chrm=True )
#         # skip unparseable lines, or lines without gene ids
#         if None == data: continue
#         if data.gene_id == "": continue
#         if contig != None and data.region.chr != contig: continue
#         if strand != None and data.region.strand != strand: continue
#         # add gene lines directly to the gene object
#         if data.feature == 'gene': 
#             gene_lines[data.gene_id][1].append( data )
#         else:
#             gene_lines[data.gene_id][0][data.trans_id].append(data)
#     
#     genes = Annotation()
#     for gene_id, ( transcripts_data, gene_lines ) in gene_lines.iteritems():
#         try:
#             gene = _load_gene_from_gtf_lines(
#                 gene_id, gene_lines, transcripts_data)
#         except Exception, inst:
#             log_statement( 
#                 "ERROR : Could not load '%s': %s" % (gene_id, inst), log=True)
#             #log_statement( traceback.format_exc(), log=True )
#             if DEBUG: raise
#             gene = None
#         
#         if gene == None: continue
#         genes.append( gene )
#     
#     if isinstance( fname_or_fp, str ):
#         fp.close()
#     
#     return genes


import sys, os
    import argparse
        import cProfile
from collections import defaultdict
from itertools import izip
from grit.files.gtf import load_gtf
    from merge_transcripts import build_gtf_lines
def make_class_cnts_string( class_counts, ref_fname, gtf_fname ):
    # if suppress_maps_stats then class_counts will be None and 
    # so should not be output
    if all( item == None for item in class_counts ):
        return ''
    
    r_class_cnts, t_class_cnts = class_counts
    
    class_lines = []
    
    # add class count here
    class_lines.append( "\nClass Code Counts" )    
    
    h_cts = "".join(char.ljust(12) for char in "=cju")
    class_lines.append( "".ljust(25) + h_cts )
    
    r_cts = "".join(str(r_class_cnts[char]).ljust(12) for char in "=cju")
    class_lines.append( "Reference".ljust(25) + r_cts )
    
    n_cts = "".join(str(t_class_cnts[char]).ljust(12) for char in "=cju")
    class_lines.append( "Novel Annotation".ljust(25) + n_cts )
    
    return '\n'.join( class_lines )

def compare( ref_fname, gtf_fname, build_maps, build_maps_stats, 
             out_prefix, num_threads=1 ):
    """Compare refernce to another 'gtf' annotation by element types
    """
    # load the gtf files
    ref_genes = load_gtf(ref_fname)

# Path: grit/files/gtf.py
# def load_gtf(fname_or_fp, contig=None, strand=None):
#     if isinstance( fname_or_fp, str ):
#         fp = open( fname_or_fp )
#     else:
#         assert isinstance( fname_or_fp, (file, gzip.GzipFile) )
#         fp = fname_or_fp
#     
#     gene_lines = defaultdict(lambda: ( defaultdict(list), [] ))
#     for line in fp:
#         data = parse_gtf_line(line, fix_chrm=True )
#         # skip unparseable lines, or lines without gene ids
#         if None == data: continue
#         if data.gene_id == "": continue
#         if contig != None and data.region.chr != contig: continue
#         if strand != None and data.region.strand != strand: continue
#         # add gene lines directly to the gene object
#         if data.feature == 'gene': 
#             gene_lines[data.gene_id][1].append( data )
#         else:
#             gene_lines[data.gene_id][0][data.trans_id].append(data)
#     
#     genes = Annotation()
#     for gene_id, ( transcripts_data, gene_lines ) in gene_lines.iteritems():
#         try:
#             gene = _load_gene_from_gtf_lines(
#                 gene_id, gene_lines, transcripts_data)
#         except Exception, inst:
#             log_statement( 
#                 "ERROR : Could not load '%s': %s" % (gene_id, inst), log=True)
#             #log_statement( traceback.format_exc(), log=True )
#             if DEBUG: raise
#             gene = None
#         
#         if gene == None: continue
#         genes.append( gene )
#     
#     if isinstance( fname_or_fp, str ):
#         fp.close()
#     
#     return genes


# Path: grit/analyze/compare_annotations.py
import sys, os
    import argparse
        import cProfile
from collections import defaultdict
from itertools import izip
from grit.files.gtf import load_gtf
    from merge_transcripts import build_gtf_lines

def make_class_cnts_string( class_counts, ref_fname, gtf_fname ):
    # if suppress_maps_stats then class_counts will be None and 
    # so should not be output
    if all( item == None for item in class_counts ):
        return ''
    
    r_class_cnts, t_class_cnts = class_counts
    
    class_lines = []
    
    # add class count here
    class_lines.append( "\nClass Code Counts" )    
    
    h_cts = "".join(char.ljust(12) for char in "=cju")
    class_lines.append( "".ljust(25) + h_cts )
    
    r_cts = "".join(str(r_class_cnts[char]).ljust(12) for char in "=cju")
    class_lines.append( "Reference".ljust(25) + r_cts )
    
    n_cts = "".join(str(t_class_cnts[char]).ljust(12) for char in "=cju")
    class_lines.append( "Novel Annotation".ljust(25) + n_cts )
    
    return '\n'.join( class_lines )

def compare( ref_fname, gtf_fname, build_maps, build_maps_stats, 
             out_prefix, num_threads=1 ):
    """Compare refernce to another 'gtf' annotation by element types
    """
    # load the gtf files
    ref_genes = load_gtf(ref_fname)

    t_genes = load_gtf(gtf_fname)