Skip to content

Commit 4dea593

Browse files
committed
Replace super(Class, self) => super()
1 parent a2b7e34 commit 4dea593

File tree

8 files changed

+249
-170
lines changed

8 files changed

+249
-170
lines changed

goatools/anno/gaf_reader.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"""
66

77
import sys
8-
from goatools.anno.annoreader_base import AnnoReaderBase
9-
from goatools.anno.init.reader_gaf import GafData
10-
from goatools.anno.init.reader_gaf import InitAssc
8+
9+
from .annoreader_base import AnnoReaderBase
10+
from .init.reader_gaf import GafData, InitAssc
1111

1212
__copyright__ = "Copyright (C) 2016-2019, DV Klopfenstein, H Tang. All rights reserved."
1313
__author__ = "DV Klopfenstein"
@@ -17,42 +17,52 @@
1717
class GafReader(AnnoReaderBase):
1818
"""Reads a Gene Annotation File (GAF). Returns a Python object."""
1919

20-
exp_kws = {'hdr_only', 'prt', 'namespaces', 'allow_missing_symbol', 'godag'}
20+
exp_kws = {"hdr_only", "prt", "namespaces", "allow_missing_symbol", "godag"}
2121

2222
def __init__(self, filename=None, **kws):
23-
super(GafReader, self).__init__(
24-
'gaf', filename,
25-
godag=kws.get('godag'),
26-
hdr_only=kws.get('hdr_only', False),
27-
prt=kws.get('prt', sys.stdout),
28-
namespaces=kws.get('namespaces'),
29-
allow_missing_symbol=kws.get('allow_missing_symbol', False))
23+
super().__init__(
24+
"gaf",
25+
filename,
26+
godag=kws.get("godag"),
27+
hdr_only=kws.get("hdr_only", False),
28+
prt=kws.get("prt", sys.stdout),
29+
namespaces=kws.get("namespaces"),
30+
allow_missing_symbol=kws.get("allow_missing_symbol", False),
31+
)
3032

31-
def read_gaf(self, namespace='BP', **kws):
33+
def read_gaf(self, namespace="BP", **kws):
3234
"""Read Gene Association File (GAF). Return associations."""
3335
return self.get_id2gos(namespace, **kws)
3436

3537
@staticmethod
3638
def wr_txt(fout_gaf, nts):
3739
"""Write namedtuples into a gaf format"""
3840
pat = (
39-
'{DB}\t{DB_ID}\t{DB_Symbol}\t{Qualifier}\t{GO_ID}\t{DB_Reference}\t'
40-
'{Evidence_Code}\t{With_From}\t{NS}\t{DB_Name}\t{DB_Synonym}\t{DB_Type}\t'
41-
'{Taxon}\t{Date}\t{Assigned_By}\t{Extension}\t{Gene_Product_Form_ID}\n')
42-
sets = {'Qualifier', 'DB_Reference', 'With_From', 'DB_Name', 'DB_Synonym', 'Gene_Product_Form_ID'}
43-
ns2a = {ns:p for p, ns in GafData.aspect2ns.items()}
44-
with open(fout_gaf, 'w') as prt:
45-
prt.write('!gaf-version: 2.1\n')
41+
"{DB}\t{DB_ID}\t{DB_Symbol}\t{Qualifier}\t{GO_ID}\t{DB_Reference}\t"
42+
"{Evidence_Code}\t{With_From}\t{NS}\t{DB_Name}\t{DB_Synonym}\t{DB_Type}\t"
43+
"{Taxon}\t{Date}\t{Assigned_By}\t{Extension}\t{Gene_Product_Form_ID}\n"
44+
)
45+
sets = {
46+
"Qualifier",
47+
"DB_Reference",
48+
"With_From",
49+
"DB_Name",
50+
"DB_Synonym",
51+
"Gene_Product_Form_ID",
52+
}
53+
ns2a = {ns: p for p, ns in GafData.aspect2ns.items()}
54+
with open(fout_gaf, "w") as prt:
55+
prt.write("!gaf-version: 2.1\n")
4656
for ntd in nts:
4757
dct = ntd._asdict()
4858
for fld in sets:
49-
dct[fld] = '|'.join(sorted(dct[fld]))
50-
dct['Taxon'] = '|'.join(['taxon:{T}'.format(T=t) for t in dct['Taxon']])
51-
dct['NS'] = ns2a[dct['NS']]
52-
dct['Date'] = dct['Date'].strftime('%Y%m%d')
59+
dct[fld] = "|".join(sorted(dct[fld]))
60+
dct["Taxon"] = "|".join(["taxon:{T}".format(T=t) for t in dct["Taxon"]])
61+
dct["NS"] = ns2a[dct["NS"]]
62+
dct["Date"] = dct["Date"].strftime("%Y%m%d")
5363
prt.write(pat.format(**dct))
54-
#prt.write('{NT}\n'.format(NT=ntd))
55-
print(' {N} annotations WROTE: {GAF}'.format(N=len(nts), GAF=fout_gaf))
64+
# prt.write('{NT}\n'.format(NT=ntd))
65+
print(" {N} annotations WROTE: {GAF}".format(N=len(nts), GAF=fout_gaf))
5666

5767
def chk_associations(self, fout_err="gaf.err"):
5868
"""Check that fields are legal in GAF"""
@@ -67,7 +77,9 @@ def has_ns(self):
6777
def _init_associations(self, fin_gaf, **kws):
6878
"""Read annotation file and store a list of namedtuples."""
6979
ini = InitAssc(fin_gaf)
70-
nts = ini.init_associations(kws['hdr_only'], kws['prt'], kws['namespaces'], kws['allow_missing_symbol'])
80+
nts = ini.init_associations(
81+
kws["hdr_only"], kws["prt"], kws["namespaces"], kws["allow_missing_symbol"]
82+
)
7183
self.hdr = ini.hdr
7284
return nts
7385

goatools/anno/genetogo_reader.py

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,39 @@
55
66
"""
77

8-
import sys
98
import collections as cx
9+
import sys
10+
1011
from itertools import chain
11-
from goatools.anno.init.reader_genetogo import InitAssc
12-
from goatools.anno.annoreader_base import AnnoReaderBase
13-
from goatools.anno.opts import AnnoOptions
1412

15-
__copyright__ = "Copyright (C) 2016-present, DV Klopfenstein, H Tang. All rights reserved."
13+
from .annoreader_base import AnnoReaderBase
14+
from .init.reader_genetogo import InitAssc
15+
from .opts import AnnoOptions
16+
17+
__copyright__ = (
18+
"Copyright (C) 2016-present, DV Klopfenstein, H Tang. All rights reserved."
19+
)
1620
__author__ = "DV Klopfenstein"
1721

1822

1923
# pylint: disable=broad-except,too-few-public-methods,line-too-long
2024
class Gene2GoReader(AnnoReaderBase):
2125
"""Reads a Gene Annotation File (GAF). Returns a Python object."""
2226

23-
exp_kws = {'taxids', 'taxid', 'namespaces', 'godag'}
27+
exp_kws = {"taxids", "taxid", "namespaces", "godag"}
2428

2529
def __init__(self, filename=None, **kws):
2630
# kws: taxids or taxid
27-
super(Gene2GoReader, self).__init__('gene2go', filename, **kws)
31+
super().__init__("gene2go", filename, **kws)
2832
# Each taxid has a list of namedtuples - one for each line in the annotations
2933
self.taxid2asscs = self._init_taxid2asscs()
3034

3135
def get_ns2assc(self, taxid=None, **kws):
3236
"""Return given associations into 3 (BP, MF, CC) dicts, id2gos"""
33-
return {ns:self._get_id2gos(nts, **kws) for ns, nts in self.get_ns2ntsanno(taxid).items()}
37+
return {
38+
ns: self._get_id2gos(nts, **kws)
39+
for ns, nts in self.get_ns2ntsanno(taxid).items()
40+
}
3441

3542
def get_ns2ntsanno(self, taxid=None):
3643
"""Return all associations in three (one for BP MF CC) dicts, id2gos"""
@@ -63,28 +70,33 @@ def get_associations(self, taxid=None):
6370
@staticmethod
6471
def _warning_taxid(taxid):
6572
"""Warn if an unexpected taxid"""
66-
pat = ('**WARNING: NO ASSOCIATIONS FOR taxid({TAXID}). '
67-
'Taxid MUST BE AN int, list of ints, OR bool')
73+
pat = (
74+
"**WARNING: NO ASSOCIATIONS FOR taxid({TAXID}). "
75+
"Taxid MUST BE AN int, list of ints, OR bool"
76+
)
6877
print(pat.format(TAXID=taxid))
6978
return {}
7079

7180
def get_id2gos_nss(self, **kws):
7281
"""Return all associations in a dict, id2gos, regardless of namespace"""
73-
taxids = self._get_taxids(kws.get('taxids'), kws.get('taxid'))
82+
taxids = self._get_taxids(kws.get("taxids"), kws.get("taxid"))
7483
assert taxids, "NO TAXIDS FOUND"
7584
assc = list(chain.from_iterable([self.taxid2asscs[t] for t in taxids]))
7685
return self._get_id2gos(assc, **kws)
7786

7887
def get_name(self):
7988
"""Get name using taxid"""
8089
if len(self.taxid2asscs) == 1:
81-
return '{BASE}_{TAXID}'.format(
82-
BASE=self.name, TAXID=next(iter(self.taxid2asscs.keys())))
83-
return '{BASE}_various'.format(BASE=self.name)
90+
return "{BASE}_{TAXID}".format(
91+
BASE=self.name, TAXID=next(iter(self.taxid2asscs.keys()))
92+
)
93+
return "{BASE}_various".format(BASE=self.name)
8494

8595
def get_taxid(self):
8696
"""Return taxid, if one was provided. Other wise return True representing all taxids"""
87-
return next(iter(self.taxid2asscs.keys())) if len(self.taxid2asscs) == 1 else True
97+
return (
98+
next(iter(self.taxid2asscs.keys())) if len(self.taxid2asscs) == 1 else True
99+
)
88100

89101
def has_ns(self):
90102
"""Return True if namespace field, NS exists on annotation namedtuples"""
@@ -96,56 +108,66 @@ def prt_counts(self, prt=sys.stdout):
96108
num_annos = sum(len(a) for a in self.taxid2asscs.values())
97109
# 792,891 annotations for 3 taxids stored: 10090 7227 9606
98110
cnts = self._get_counts(list(chain.from_iterable(self.taxid2asscs.values())))
99-
prt.write('{A:8,} annotations, {P:,} proteins/genes, {G:,} GO IDs, {N} taxids stored'.format(
100-
A=num_annos, N=num_taxids, G=cnts['GOs'], P=cnts['geneids']))
111+
prt.write(
112+
"{A:8,} annotations, {P:,} proteins/genes, {G:,} GO IDs, {N} taxids stored".format(
113+
A=num_annos, N=num_taxids, G=cnts["GOs"], P=cnts["geneids"]
114+
)
115+
)
101116
if num_taxids < 5:
102-
prt.write(': {Ts}'.format(Ts=' '.join(str(t) for t in sorted(self.taxid2asscs))))
103-
prt.write('\n')
117+
prt.write(
118+
": {Ts}".format(Ts=" ".join(str(t) for t in sorted(self.taxid2asscs)))
119+
)
120+
prt.write("\n")
104121
# 102,430 annotations for taxid 7227
105122
# 323,776 annotations for taxid 9606
106123
# 366,685 annotations for taxid 10090
107124
if num_taxids == 1:
108125
return
109126
for taxid, assc in self.taxid2asscs.items():
110127
cnts = self._get_counts(assc)
111-
prt.write('{A:8,} annotations, {P:,} proteins/genes, {G:,} GO IDs for taxid {T}\n'.format(
112-
A=len(assc), T=taxid, G=cnts['GOs'], P=cnts['geneids']))
128+
prt.write(
129+
"{A:8,} annotations, {P:,} proteins/genes, {G:,} GO IDs for taxid {T}\n".format(
130+
A=len(assc), T=taxid, G=cnts["GOs"], P=cnts["geneids"]
131+
)
132+
)
113133

114134
@staticmethod
115135
def _get_counts(nts):
116136
"""Return the count of GO IDs and genes/proteins in a set of annotation namedtuples"""
117137
sets = cx.defaultdict(set)
118138
for ntd in nts:
119-
sets['geneids'].add(ntd.DB_ID)
120-
sets['GOs'].add(ntd.GO_ID)
121-
return {'GOs':len(sets['GOs']), 'geneids':len(sets['geneids'])}
139+
sets["geneids"].add(ntd.DB_ID)
140+
sets["GOs"].add(ntd.GO_ID)
141+
return {"GOs": len(sets["GOs"]), "geneids": len(sets["geneids"])}
122142

123143
# -- taxids2asscs -------------------------------------------------------------------------
124144
def get_taxid2asscs(self, taxids=None, **kws):
125145
"""Read Gene Association File (GAF). Return data."""
126146
# WAS: get_annotations_taxid2dct
127-
taxid2asscs = cx.defaultdict(lambda: cx.defaultdict(lambda: cx.defaultdict(set)))
147+
taxid2asscs = cx.defaultdict(
148+
lambda: cx.defaultdict(lambda: cx.defaultdict(set))
149+
)
128150
options = AnnoOptions(self.evobj, **kws)
129151
for taxid in self._get_taxids(taxids):
130152
nts = self.taxid2asscs[taxid]
131153
assc = self.reduce_annotations(nts, options)
132-
taxid2asscs[taxid]['ID2GOs'] = self.get_dbid2goids(assc)
133-
taxid2asscs[taxid]['GO2IDs'] = self.get_goid2dbids(assc)
154+
taxid2asscs[taxid]["ID2GOs"] = self.get_dbid2goids(assc)
155+
taxid2asscs[taxid]["GO2IDs"] = self.get_goid2dbids(assc)
134156
return taxid2asscs
135157

136158
@staticmethod
137159
def fill_taxid2asscs(taxid2asscs_usr, taxid2asscs_ret):
138160
"""Fill user taxid2asscs for backward compatibility."""
139161
for taxid, ab_ret in taxid2asscs_ret.items():
140-
taxid2asscs_usr[taxid]['ID2GOs'] = ab_ret['ID2GOs']
141-
taxid2asscs_usr[taxid]['GO2IDs'] = ab_ret['GO2IDs']
162+
taxid2asscs_usr[taxid]["ID2GOs"] = ab_ret["ID2GOs"]
163+
taxid2asscs_usr[taxid]["GO2IDs"] = ab_ret["GO2IDs"]
142164

143165
@staticmethod
144166
def get_id2gos_all(taxid2asscs_a2b):
145167
"""Get associations for all stored species taxid2asscs[taxid][ID2GOs|GO2IDs]."""
146168
id2gos_all = {}
147169
for a2b in taxid2asscs_a2b.values():
148-
for geneid, gos in a2b['ID2GOs'].items():
170+
for geneid, gos in a2b["ID2GOs"].items():
149171
id2gos_all[geneid] = gos
150172
return id2gos_all
151173

goatools/anno/gpad_reader.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
"""
88

99
import collections as cx
10-
from goatools.anno.annoreader_base import AnnoReaderBase
11-
from goatools.anno.init.reader_gpad import InitAssc
10+
11+
from .annoreader_base import AnnoReaderBase
12+
from .init.reader_gpad import InitAssc
1213

1314
__copyright__ = "Copyright (C) 2016-2019, DV Klopfenstein, H Tang. All rights reserved."
1415
__author__ = "DV Klopfenstein"
@@ -17,13 +18,16 @@
1718
class GpadReader(AnnoReaderBase):
1819
"""dRead a Gene Product Association Data (GPAD) and store the data in a Python object."""
1920

20-
exp_kws = {'hdr_only', 'godag', 'namespaces'}
21+
exp_kws = {"hdr_only", "godag", "namespaces"}
2122

2223
def __init__(self, filename=None, **kws):
23-
super(GpadReader, self).__init__('gpad', filename,
24-
hdr_only=kws.get('hdr_only', False),
25-
godag=kws.get('godag'),
26-
namespaces=kws.get('namespaces'))
24+
super().__init__(
25+
"gpad",
26+
filename,
27+
hdr_only=kws.get("hdr_only", False),
28+
godag=kws.get("godag"),
29+
namespaces=kws.get("namespaces"),
30+
)
2731
self.qty = len(self.associations)
2832

2933
def get_relation_cnt(self):
@@ -36,8 +40,8 @@ def get_relation_cnt(self):
3640

3741
def _init_associations(self, fin_gpad, **kws):
3842
"""Read annotation file and store a list of namedtuples."""
39-
ini = InitAssc(fin_gpad, kws['godag'])
40-
nts = ini.init_associations(kws['hdr_only'], kws['namespaces'])
43+
ini = InitAssc(fin_gpad, kws["godag"])
44+
nts = ini.init_associations(kws["hdr_only"], kws["namespaces"])
4145
self.hdr = ini.hdr
4246
return nts
4347

goatools/anno/idtogos_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class IdToGosReader(AnnoReaderBase):
1717

1818
def __init__(self, filename=None, **kws):
1919
self.id2gos = None # ID to GO ID set as loaded from annotations file
20-
super(IdToGosReader, self).__init__(
20+
super().__init__(
2121
"id2gos",
2222
filename,
2323
godag=kws.get("godag"),

0 commit comments

Comments
 (0)