# 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/lib/multiprocessing_utils.py
# class ProcessSafeOPStream( object ):
#     def __init__( self, writeable_obj ):
#         self.writeable_obj = writeable_obj
#         self.lock = multiprocessing.Lock()
#         self.name = self.writeable_obj.name
#         return
#     
#     def write( self, data ):
#         self.lock.acquire()
#         self.writeable_obj.write( data )
#         self.writeable_obj.flush()
#         self.lock.release()
#         return
#     
#     def close( self ):
#         self.writeable_obj.close()


import sys, os
import grit.proteomics.ORF
    import argparse
from grit.files.gtf import load_gtf
from grit.lib.multiprocessing_utils import ProcessSafeOPStream
        '--only-longest-orf', default=False, action='store_true',
        help='If this is set, only report the longest ORF per transcript. ' )
    parser.add_argument(
        '--dont-include-stop-codon', default=False, action='store_true',
        help='If this is set, don\'t include the stop codon in the CDS region.')
    
    parser.add_argument(
        '--threads', '-t', type=int, default=1,
        help='Number of threads with which to find ORFs. ' +
        '(default: %(default)d)')
    
    parser.add_argument(
        '--output-filename', '-o',
        help='Output file. (default: stdout)')
    parser.add_argument(
        '--fasta-output-filename', '-f',
        help='Write protein sequences to --fasta-output-filename, if set.')

    parser.add_argument( 
        '--verbose', '-v', default=False, action='store_true',
        help='Whether or not to print status information.')
    args = parser.parse_args()
    
    if args.min_aas != None: 
        grit.proteomics.ORF.MIN_AAS_PER_ORF = args.min_aas
    
    # create default if no prefix provided or if same as gtf filename
    if args.output_filename == None:
        gtf_ofp = ProcessSafeOPStream( sys.stdout )
    else:

# 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/lib/multiprocessing_utils.py
# class ProcessSafeOPStream( object ):
#     def __init__( self, writeable_obj ):
#         self.writeable_obj = writeable_obj
#         self.lock = multiprocessing.Lock()
#         self.name = self.writeable_obj.name
#         return
#     
#     def write( self, data ):
#         self.lock.acquire()
#         self.writeable_obj.write( data )
#         self.writeable_obj.flush()
#         self.lock.release()
#         return
#     
#     def close( self ):
#         self.writeable_obj.close()


# Path: bin/find_orfs.py
import sys, os
import grit.proteomics.ORF
    import argparse
from grit.files.gtf import load_gtf
from grit.lib.multiprocessing_utils import ProcessSafeOPStream

        '--only-longest-orf', default=False, action='store_true',
        help='If this is set, only report the longest ORF per transcript. ' )
    parser.add_argument(
        '--dont-include-stop-codon', default=False, action='store_true',
        help='If this is set, don\'t include the stop codon in the CDS region.')
    
    parser.add_argument(
        '--threads', '-t', type=int, default=1,
        help='Number of threads with which to find ORFs. ' +
        '(default: %(default)d)')
    
    parser.add_argument(
        '--output-filename', '-o',
        help='Output file. (default: stdout)')
    parser.add_argument(
        '--fasta-output-filename', '-f',
        help='Write protein sequences to --fasta-output-filename, if set.')

    parser.add_argument( 
        '--verbose', '-v', default=False, action='store_true',
        help='Whether or not to print status information.')
    args = parser.parse_args()
    
    if args.min_aas != None: 
        grit.proteomics.ORF.MIN_AAS_PER_ORF = args.min_aas
    
    # create default if no prefix provided or if same as gtf filename
    if args.output_filename == None:
        gtf_ofp = ProcessSafeOPStream( sys.stdout )
    else:

        gtf_ofp = ProcessSafeOPStream( open( args.output_filename, 'w' ) )