def main(args=None):
if args is None:<EOL><INDENT>parser = get_argument_parser()<EOL>args = parser.parse_args()<EOL><DEDENT>stringtie_file = args.stringtie_file<EOL>gene_file = args.gene_file<EOL>no_novel_transcripts = args.no_novel_transcripts<EOL>output_file = args.output_file<EOL>log_file = args.log_file<EOL>quiet = args.quiet<EOL>verbose = args.verbose<EOL>logger = misc.get_logger(log_file=log_file, quiet=quiet, verbose=verbose)<EOL>logger.info('<STR_LIT>')<EOL>genes = misc.read_single(gene_file)<EOL>logger.info('<STR_LIT>')<EOL>logger.info('<STR_LIT>')<EOL>stringtie_genes = {}<EOL>with open(stringtie_file) as fh:<EOL><INDENT>reader = csv.reader(fh, dialect='<STR_LIT>')<EOL>for l in reader:<EOL><INDENT>if l[<NUM_LIT:0>][<NUM_LIT:0>] == '<STR_LIT:#>':<EOL><INDENT>continue<EOL><DEDENT>assert len(l) == <NUM_LIT:9><EOL>if l[<NUM_LIT:2>] != '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>attr = parse_attributes(l[<NUM_LIT:8>])<EOL>try:<EOL><INDENT>ref_gene = attr['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>continue<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>g = stringtie_genes[attr['<STR_LIT>']]<EOL><DEDENT>except KeyError:<EOL><INDENT>stringtie_genes[attr['<STR_LIT>']] = {ref_gene, }<EOL><DEDENT>else:<EOL><INDENT>g.add(ref_gene)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>logger.info('<STR_LIT>',<EOL>len(stringtie_genes))<EOL>gene_ids_ambiguous = [k for k, v in stringtie_genes.items()<EOL>if len(v) > <NUM_LIT:1>]<EOL>n = len(gene_ids_ambiguous)<EOL>logger.info('<STR_LIT>',<EOL>n, len(stringtie_genes), <NUM_LIT:100>*(n/float(len(stringtie_genes))))<EOL>n = len(genes)<EOL>fpkm = np.zeros(n, dtype=np.float64)<EOL>tpm = np.zeros(n, dtype=np.float64)<EOL>fpkm_novel_gene = <NUM_LIT:0><EOL>fpkm_unknown_gene_name = <NUM_LIT:0><EOL>fpkm_novel_trans = <NUM_LIT:0><EOL>fpkm_ambig = <NUM_LIT:0><EOL>with open(stringtie_file) as fh:<EOL><INDENT>reader = csv.reader(fh, dialect='<STR_LIT>')<EOL>for l in reader:<EOL><INDENT>if l[<NUM_LIT:0>][<NUM_LIT:0>] == '<STR_LIT:#>':<EOL><INDENT>continue<EOL><DEDENT>assert len(l) == <NUM_LIT:9><EOL>if l[<NUM_LIT:2>] != '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>attr = parse_attributes(l[<NUM_LIT:8>])<EOL>f = float(attr['<STR_LIT>'])<EOL>try:<EOL><INDENT>g = attr['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>if no_novel_transcripts:<EOL><INDENT>fpkm_novel_trans += f<EOL>continue<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>assoc = stringtie_genes[attr['<STR_LIT>']]<EOL><DEDENT>except KeyError:<EOL><INDENT>fpkm_novel_gene += f<EOL>continue<EOL><DEDENT>else:<EOL><INDENT>if len(assoc) > <NUM_LIT:1>:<EOL><INDENT>fpkm_ambig += f<EOL>continue<EOL><DEDENT>else:<EOL><INDENT>g = list(assoc)[<NUM_LIT:0>]<EOL><DEDENT><DEDENT><DEDENT><DEDENT>try:<EOL><INDENT>idx = misc.bisect_index(genes, g)<EOL><DEDENT>except ValueError:<EOL><INDENT>fpkm_unknown_gene_name += f<EOL>logger.warning('<STR_LIT>', g)<EOL>continue<EOL><DEDENT>t = float(attr['<STR_LIT>'])<EOL>fpkm[idx] += f<EOL>tpm[idx] += t<EOL><DEDENT><DEDENT>if no_novel_transcripts:<EOL><INDENT>ignored_fpkm = fpkm_novel_trans + fpkm_unknown_gene_name<EOL><DEDENT>else:<EOL><INDENT>ignored_fpkm = fpkm_novel_gene + fpkm_ambig + fpkm_unknown_gene_name<EOL><DEDENT>total_fpkm = np.sum(fpkm) + ignored_fpkm<EOL>logger.info('<STR_LIT>', ignored_fpkm,<EOL>total_fpkm, <NUM_LIT:100>*(ignored_fpkm/total_fpkm))<EOL>if no_novel_transcripts and fpkm_novel_trans > <NUM_LIT:0>:<EOL><INDENT>logger.info('<STR_LIT>',<EOL>fpkm_novel_trans, <NUM_LIT:100>*(fpkm_novel_trans/total_fpkm))<EOL><DEDENT>else:<EOL><INDENT>if fpkm_novel_gene > <NUM_LIT:0>:<EOL><INDENT>logger.info('<STR_LIT>'<EOL>'<STR_LIT>',<EOL>fpkm_novel_gene, <NUM_LIT:100>*(fpkm_novel_gene/total_fpkm))<EOL><DEDENT>if fpkm_ambig > <NUM_LIT:0>:<EOL><INDENT>logger.info('<STR_LIT>'<EOL>'<STR_LIT>',<EOL>fpkm_ambig, <NUM_LIT:100>*(fpkm_ambig/total_fpkm))<EOL><DEDENT><DEDENT>if fpkm_unknown_gene_name > <NUM_LIT:0>:<EOL><INDENT>logger.info('<STR_LIT>'<EOL>'<STR_LIT>',<EOL>fpkm_unknown_gene_name,<EOL><NUM_LIT:100>*(fpkm_unknown_gene_name/total_fpkm))<EOL><DEDENT>E = np.c_[fpkm, tpm]<EOL>with open(output_file, '<STR_LIT:w>') as ofh:<EOL><INDENT>writer = csv.writer(ofh, dialect='<STR_LIT>',<EOL>lineterminator=os.linesep,<EOL>quoting=csv.QUOTE_NONE)<EOL>for i, g in enumerate(genes):<EOL><INDENT>writer.writerow([g] + ['<STR_LIT>' % e for e in E[i, :]])<EOL><DEDENT><DEDENT>return <NUM_LIT:0><EOL>
Extracts gene-level expression data from StringTie output.

    Parameters
    ----------
    args: argparse.Namespace object, optional
        The argument values. If not specified, the values will be obtained by
        parsing the command line arguments using the `argparse` module.

    Returns
    -------
    int
        Exit code (0 if no error occurred).