Skip to content

Commit ab07d24

Browse files
tanghaibaoAdamtaranto
authored andcommitted
Dev (#794)
* Add --human_chr to gff.format() * update gtf attributes * fix tests and deprecation warnings
1 parent bcf53b6 commit ab07d24

7 files changed

Lines changed: 42 additions & 24 deletions

File tree

src/jcvi/apps/phylo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def SH_raxml(reftree, querytree, phy_file, shout="SH_out.txt"):
481481
except Exception:
482482
print("SH test failed.", file=sys.stderr)
483483
else:
484-
pval = pval.strip().replace("\t", " ").replace("%", "\%")
484+
pval = pval.strip().replace("\t", " ").replace("%", r"\%")
485485
print("{0}\t{1}".format(op.basename(querytree), pval), file=shout)
486486
logger.debug("SH p-value appended to %s" % shout.name)
487487

src/jcvi/formats/gff.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/usr/bin/env python
22
# -*- coding: UTF-8 -*-
33

4-
from collections import defaultdict
54
import os
65
import os.path as op
76
import re
87
import sys
8+
9+
from collections import defaultdict
910
from urllib.parse import quote, unquote
1011

1112
from ..annotation.reformat import atg_name
@@ -67,7 +68,12 @@
6768
)
6869
multiple_gff_attributes = ("Parent", "Alias", "Dbxref", "Ontology_term")
6970
safechars = " /:?~#+!$'@()*[]|"
70-
VALID_HUMAN_CHROMOSMES = set([str(x) for x in range(1, 23)] + ["X", "Y"])
71+
VALID_HUMAN_CHROMOSOMES = {
72+
**{str(i): f"chr{i}" for i in range(1, 23)},
73+
"X": "chrX",
74+
"Y": "chrY",
75+
"MT": "chrM",
76+
}
7177

7278

7379
class GffLine(object):
@@ -209,6 +215,7 @@ def update_attributes(self, skipEmpty=True, gff3=True, gtf=None, urlquote=True):
209215
attributes = []
210216
if gtf:
211217
gff3 = None
218+
urlquote = False
212219
elif gff3 is None:
213220
gff3 = self.gff3
214221

@@ -1495,7 +1502,6 @@ def chain(args):
14951502
prev_gid = curr_gid
14961503

14971504
for gkey, v in sorted(gffdict.items()):
1498-
gseqid, key = gkey
14991505
seqid = v["seqid"]
15001506
source = v["source"]
15011507
type = v["type"]
@@ -1672,7 +1678,12 @@ def format(args):
16721678
action="store_true",
16731679
help="Store entire GFF file in memory during first iteration",
16741680
)
1675-
1681+
p.add_argument(
1682+
"--human_chr",
1683+
default=False,
1684+
action="store_true",
1685+
help="Only allow 1-22XY/MT, and add `chr` prefix to seqid",
1686+
)
16761687
p.set_outfile()
16771688
p.set_SO_opts()
16781689

@@ -1843,6 +1854,10 @@ def format(args):
18431854
so, _ = GODag_from_SO()
18441855
valid_soterm = {}
18451856

1857+
gtf = ".gtf" in gffile and not opts.gff3
1858+
if gtf:
1859+
logger.info("Output in GTF format")
1860+
18461861
fw = must_open(outfile, "w")
18471862
if not make_gff_store:
18481863
gff = Gff(gffile, keep_attr_order=(not opts.no_keep_attr_order), strict=strict)
@@ -2021,6 +2036,11 @@ def format(args):
20212036
_parent = [parent, "{0}-Protein".format(parent)]
20222037
g.set_attr("Parent", _parent)
20232038

2039+
if opts.human_chr:
2040+
if g.seqid not in VALID_HUMAN_CHROMOSOMES:
2041+
continue
2042+
g.seqid = VALID_HUMAN_CHROMOSOMES[g.seqid]
2043+
20242044
pp = g.get_attr("Parent", first=False)
20252045
if (
20262046
opts.multiparents == "split" and (pp and len(pp) > 1) and g.type != "CDS"
@@ -2034,9 +2054,8 @@ def format(args):
20342054
fix_gsac(g, notes)
20352055
print(g, file=fw)
20362056
else:
2037-
if g.gff3 and not opts.gff3:
2038-
opts.gff3 = True
2039-
g.update_attributes(gff3=opts.gff3)
2057+
gff3 = g.gff3 or opts.gff3
2058+
g.update_attributes(gff3=gff3, gtf=gtf)
20402059
if gsac:
20412060
fix_gsac(g, notes)
20422061
if duptype == g.type and skip[(g.seqid, g.idx, id, g.start, g.end)] == 1:
@@ -2576,7 +2595,6 @@ def gtf(args):
25762595
gff = Gff(gffile, strict=not opts.nostrict)
25772596
transcript_info = AutoVivification()
25782597
for g in gff:
2579-
transcript_id = None
25802598
if g.type.endswith(("RNA", "transcript")):
25812599
if "ID" in g.attributes and "Parent" in g.attributes:
25822600
transcript_id = g.get_attr("ID")
@@ -3024,7 +3042,7 @@ def bed(args):
30243042
"--human_chr",
30253043
default=False,
30263044
action="store_true",
3027-
help="Only allow 1-22XY, and add `chr` prefix to seqid",
3045+
help="Only allow 1-22XY/MT, and add `chr` prefix to seqid",
30283046
)
30293047
p.add_argument(
30303048
"--ensembl_cds",
@@ -3090,9 +3108,9 @@ def bed(args):
30903108
if span:
30913109
bl.score = bl.span
30923110
if human_chr:
3093-
if bl.seqid not in VALID_HUMAN_CHROMOSMES:
3111+
if bl.seqid not in VALID_HUMAN_CHROMOSOMES:
30943112
continue
3095-
bl.seqid = "chr" + bl.seqid
3113+
bl.seqid = VALID_HUMAN_CHROMOSOMES[bl.seqid]
30963114
if ensembl_cds:
30973115
if g.get_attr("gene_biotype") != "protein_coding":
30983116
continue

src/jcvi/formats/sam.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ def get_minibam_bed(bamfile, bedfile, minibam=None):
195195

196196
cmd = "cat {}".format(bedfile)
197197
cmd += " | perl -lane 'print \"$F[0]:$F[1]-$F[2]\"'"
198-
cmd += " | xargs -n1 -t -I \{\}"
198+
cmd += r" | xargs -n1 -t -I \{\}"
199199
cmd += " samtools view {}".format(bamfile)
200-
cmd += " \{\} >> " + minisamfile
200+
cmd += r" \{\} >> " + minisamfile
201201
sh(cmd)
202202

203203
cmd = "samtools view {} -b".format(minisamfile)

src/jcvi/projects/allmaps.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def estimategaps(args):
256256
root.text(
257257
sum(markers) / 2,
258258
ypos + pad,
259-
"Distance: 1.29cM $\Leftrightarrow$ 211,824bp (6.1 cM/Mb)",
259+
r"Distance: 1.29cM $\Leftrightarrow$ 211,824bp (6.1 cM/Mb)",
260260
**fontprop,
261261
)
262262

@@ -310,7 +310,7 @@ def lms(args):
310310
xdata = [x + randint(-3, 3) for x in range(10, 110, 10)]
311311
ydata = [x + randint(-3, 3) for x in range(10, 110, 10)]
312312
ydata[3:7] = ydata[3:7][::-1]
313-
xydata = zip(xdata, ydata)
313+
xydata = list(zip(xdata, ydata))
314314
lis = xydata[:3] + [xydata[4]] + xydata[7:]
315315
lds = xydata[3:7]
316316
xlis, ylis = zip(*lis)
@@ -425,7 +425,7 @@ def subplot_twinx(
425425
legend=None,
426426
loc="upper left",
427427
):
428-
columned_data = zip(*data)
428+
columned_data = list(zip(*data))
429429
x, yy = columned_data[0], columned_data[1:]
430430
assert len(ylabels) == 2
431431
assert len(yy) == 2
@@ -464,7 +464,7 @@ def subplot_twinx(
464464
def subplot(
465465
ax, data, xlabel, ylabel, xlim=None, ylim=1.1, xcast=float, ycast=float, legend=None
466466
):
467-
columned_data = zip(*data)
467+
columned_data = list(zip(*data))
468468
x, yy = columned_data[0], columned_data[1:]
469469
lines = []
470470
for y, m in zip(yy, "o^x"):

src/jcvi/projects/misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ def grabseeds(args):
9999

100100
fig = plt.figure(1, (iopts.w, iopts.h))
101101
ax = fig.add_subplot(1, 1, 1)
102-
ax.set_xlabel(f"Principal Component 1 ({pc1_var * 100:.0f}\%)", fontsize=15)
103-
ax.set_ylabel(f"Principal Component 2 ({pc2_var * 100:.0f}\%)", fontsize=15)
102+
ax.set_xlabel(f"Principal Component 1 ({pc1_var * 100:.0f}%)", fontsize=15)
103+
ax.set_ylabel(f"Principal Component 2 ({pc2_var * 100:.0f}%)", fontsize=15)
104104
ax.set_title("Sorghum kernels, PCA Plot", fontsize=20)
105105
ax.scatter("PC1", "PC2", s="ScatterSize", c="Color", data=final_df)
106106
set_helvetica_axis(ax)

src/jcvi/utils/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def db_defaults(connector="Sybase"):
3535
def get_profile(
3636
sqshrc="~/.sqshrc", connector="Sybase", hostname=None, username=None, password=None
3737
):
38-
"""
38+
r"""
3939
get database, username, password from .sqshrc file e.g.
4040
\set username="user"
4141
"""

tests/formats/test_obo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def test_oboreader():
1111
r4 = go["GO:0000010"]
1212
assert r1.item_id == "GO:0000001"
1313
assert r1.name == "mitochondrion inheritance"
14-
assert r2.item_id == "GO:0000002"
15-
assert r2.namespace == "biological_process"
16-
assert r3.item_id == "GO:0000006"
14+
assert r2.item_id == "GO:0000006"
15+
assert r2.namespace == "molecular_function"
16+
assert r3.item_id == "GO:0000007"
1717
assert tuple(sorted(r4.alt_ids)) == ("GO:0036422",)
1818

1919
if os.path.exists(obo_file):

0 commit comments

Comments
 (0)