Example #1
0
 def prt_txt(prt, goea_results, prtfmt=None, **kws):
     """Print GOEA results in text format."""
     objprt = PrtFmt()
     if prtfmt is None:
         flds = ['GO', 'NS', 'p_uncorrected',
                 'ratio_in_study', 'ratio_in_pop', 'depth', 'name', 'study_items']
         prtfmt = objprt.get_prtfmt_str(flds)
     prtfmt = objprt.adjust_prtfmt(prtfmt)
     prt_flds = RPT.get_fmtflds(prtfmt)
     data_nts = MgrNtGOEAs(goea_results).get_goea_nts_prt(prt_flds, **kws)
     RPT.prt_txt(prt, data_nts, prtfmt, prt_flds, **kws)
     return data_nts
Example #2
0
 def prt_txt(self, prt, goea_results, prtfmt=None, **kws):
     """Print GOEA results in text format."""
     objprt = PrtFmt()
     if prtfmt is None:
         flds = ['GO', 'NS', 'p_uncorrected', 'ratio_in_study', 'ratio_in_pop', 'depth', 'name', 'study_items']
         prtfmt = objprt.get_prtfmt_str(flds)
         #### prtfmt = " ".join([objprt.default_fld2fmt[f] for f in flds])
         #### prtfmt = ("{GO} {NS} {p_uncorrected:5.2e} {ratio_in_study:>6} {ratio_in_pop:>9} "
         ####           "{depth:02} {name:40} {study_items}\n")
     prtfmt = objprt.adjust_prtfmt(prtfmt)
     prt_flds = RPT.get_fmtflds(prtfmt)
     data_nts = MgrNtGOEAs(goea_results).get_goea_nts_prt(prt_flds, **kws)
     RPT.prt_txt(prt, data_nts, prtfmt, prt_flds, **kws)
     return data_nts
Example #3
0
class GrpWr(object):
    """Write GO term GOEA information, grouped."""
    def __init__(self, sortobj, pval_fld, ver_list):
        self.sortobj = sortobj
        self.pval_fld = pval_fld
        self.ver_list = ver_list
        self.objprt = PrtFmt()
        self.flds_all = next(iter(self.sortobj.grprobj.go2nt.values()))._fields
        self.flds_cur = self._init_flds_cur()
        self.desc2nts = self.sortobj.get_desc2nts(hdrgo_prt=False)
        # print("nnnnnnnnnnnnnnnnnnnnnttttttttttttttttt", self.flds_all)

    def prt_outfiles_grouped(self, outfiles):
        """Write to outfiles."""
        for outfile in outfiles:
            if outfile.endswith(".xlsx"):
                self.wr_xlsx(outfile)
            elif outfile.endswith(".txt"):
                self.wr_txt(outfile)
            else:
                self.wr_tsv(outfile)

    def wr_xlsx(self, fout_xlsx):
        """Print grouped GOEA results into an xlsx file."""
        objwr = WrXlsxSortedGos("GOEA", self.sortobj)
        #### fld2fmt['ratio_in_study'] = '{:>8}'
        #### fld2fmt['ratio_in_pop'] = '{:>12}'
        #### ntfld2wbfmtdict = {
        # ntfld_wbfmt = {
        #     'ratio_in_study': {'align':'right'},
        #     'ratio_in_pop':{'align':'right'}}
        kws_xlsx = {
            'title': self.ver_list,
            'fld2fmt': {f: '{:8.2e}'
                        for f in self.flds_cur if f[:2] == 'p_'},
            #'ntfld_wbfmt': ntfld_wbfmt,
            #### 'ntval2wbfmtdict': ntval2wbfmtdict,
            #'hdrs': [],
            'prt_flds': self.flds_cur
        }
        objwr.wr_xlsx_nts(fout_xlsx, self.desc2nts, **kws_xlsx)

    def wr_tsv(self, fout_tsv):
        """Print grouped GOEA results into a tab-separated file."""
        with open(fout_tsv, 'w') as prt:
            kws_tsv = {
                'fld2fmt':
                {f: '{:8.2e}'
                 for f in self.flds_cur if f[:2] == 'p_'},
                'prt_flds': self.flds_cur
            }
            prt_tsv_sections(prt, self.desc2nts['sections'], **kws_tsv)
            print("  WROTE: {TSV}".format(TSV=fout_tsv))

    def wr_txt(self, fout_txt):
        """Write to a file GOEA results in an ASCII text format."""
        with open(fout_txt, 'w') as prt:
            for line in self.ver_list:
                prt.write("{LINE}\n".format(LINE=line))
            self.prt_txt(prt)
            print("  WROTE: {TXT}".format(TXT=fout_txt))

    def prt_tsv(self, prt=sys.stdout):
        """Print an ASCII text format."""
        prtfmt = self.objprt.get_prtfmt_str(self.flds_cur)
        prt.write("{FLDS}\n".format(FLDS=" ".join(self.flds_cur)))
        WrSectionsTxt.prt_sections(prt,
                                   self.desc2nts['sections'],
                                   prtfmt,
                                   secspc=True)

    def prt_txt(self, prt=sys.stdout):
        """Print an ASCII text format."""
        prtfmt = self.objprt.get_prtfmt_str(self.flds_cur)
        prt.write("{FLDS}\n".format(FLDS=" ".join(self.flds_cur)))
        WrSectionsTxt.prt_sections(prt,
                                   self.desc2nts['sections'],
                                   prtfmt,
                                   secspc=True)

    def _init_flds_cur(self):
        """Choose fields to print from a multitude of available fields."""
        flds = []
        # ('GO', 'NS', 'enrichment', 'name', 'ratio_in_study', 'ratio_in_pop', 'depth',
        # 'p_uncorrected', 'p_bonferroni', 'p_sidak', 'p_holm', 'p_fdr_bh',
        # 'pop_n', 'pop_count', 'pop_items'
        # 'study_n', 'study_count', 'study_items',
        # 'is_ratio_different', 'level', 'is_obsolete',
        # 'namespace', 'reldepth', 'alt_ids', 'format_txt', 'hdr_idx',
        # 'is_hdrgo', 'is_usrgo', 'num_usrgos', 'hdr1usr01', 'alt', 'GO_name',
        # 'dcnt', 'D1', 'tcnt', 'tfreq', 'tinfo', 'childcnt', 'REL',
        # 'REL_short', 'rel', 'id')
        flds0 = [
            'GO', 'NS', 'enrichment', self.pval_fld, 'dcnt', 'tinfo', 'depth',
            'ratio_in_study', 'ratio_in_pop', 'name'
        ]
        flds_p = [
            f for f in self.flds_all if f[:2] == 'p_' and f != self.pval_fld
        ]
        flds.extend(flds0)
        if flds_p:
            flds.extend(flds_p)
        flds.append('study_count')
        flds.append('study_items')
        return flds