Skip to content

Commit a2d4491

Browse files
author
Haibao Tang
committed
update gff.gtf()
1 parent 08970a1 commit a2d4491

1 file changed

Lines changed: 28 additions & 17 deletions

File tree

src/jcvi/formats/gff.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ def __init__(
9595
if len(args) != 9:
9696
args = sline.split()
9797
if strict:
98-
assert len(args) == 9, "Malformed line ({0} columns != 9): {1}".format(
99-
len(args), args
98+
assert len(args) == 9, (
99+
f"Malformed line ({len(args)} columns != 9): {args}. Please use strict=False to skip this error."
100100
)
101101
self.seqid = args[0]
102102
self.source = args[1]
@@ -1443,9 +1443,9 @@ def chain(args):
14431443
id = g.accn
14441444
gid = id
14451445
if attrib_key:
1446-
assert (
1447-
attrib_key in g.attributes.keys()
1448-
), "Attribute `{0}` not present in GFF3".format(attrib_key)
1446+
assert attrib_key in g.attributes.keys(), (
1447+
"Attribute `{0}` not present in GFF3".format(attrib_key)
1448+
)
14491449
gid = g.get_attr(attrib_key)
14501450
curr_gid = gid
14511451
if break_chain:
@@ -1691,12 +1691,12 @@ def format(args):
16911691
remove_attrs = opts.remove_attrs.split(",") if opts.remove_attrs else None
16921692
process_ftype = opts.process_ftype.split(",") if opts.process_ftype else None
16931693
gsac = opts.gsac
1694-
assert not (
1695-
opts.unique and opts.duptype
1696-
), "Cannot use `--unique` and `--chaindup` together"
1697-
assert not (
1698-
opts.type and opts.duptype
1699-
), "Cannot use `--type` and `--chaindup` together"
1694+
assert not (opts.unique and opts.duptype), (
1695+
"Cannot use `--unique` and `--chaindup` together"
1696+
)
1697+
assert not (opts.type and opts.duptype), (
1698+
"Cannot use `--type` and `--chaindup` together"
1699+
)
17001700
unique = opts.unique
17011701
duptype = opts.duptype
17021702
fixphase = opts.fixphase
@@ -1711,9 +1711,9 @@ def format(args):
17111711
)
17121712
strict = False if opts.nostrict else True
17131713
make_gff_store = True if gffile in ("-", "stdin") else opts.make_gff_store
1714-
assert not (
1715-
opts.copy_id_attr_to_name and opts.invent_name_attr
1716-
), "Cannot use `--copy_id_attr_to_name` and `--invent_name_attr` together"
1714+
assert not (opts.copy_id_attr_to_name and opts.invent_name_attr), (
1715+
"Cannot use `--copy_id_attr_to_name` and `--invent_name_attr` together"
1716+
)
17171717
copy_id_attr_to_name = opts.copy_id_attr_to_name
17181718
invent_name_attr = opts.invent_name_attr
17191719
invent_protein_feat = opts.invent_protein_feat
@@ -2560,15 +2560,22 @@ def gtf(args):
25602560
specify "gene_id" and "transcript_id".
25612561
"""
25622562
p = OptionParser(gtf.__doc__)
2563+
p.add_argument(
2564+
"--nostrict",
2565+
default=False,
2566+
action="store_true",
2567+
help="Disable strict parsing of GFF file and/or mapping file",
2568+
)
25632569
opts, args = p.parse_args(args)
25642570

25652571
if len(args) != 1:
25662572
sys.exit(not p.print_help())
25672573

25682574
(gffile,) = args
2569-
gff = Gff(gffile)
2575+
gff = Gff(gffile, strict=not opts.nostrict)
25702576
transcript_info = AutoVivification()
25712577
for g in gff:
2578+
transcript_id = None
25722579
if g.type.endswith(("RNA", "transcript")):
25732580
if "ID" in g.attributes and "Parent" in g.attributes:
25742581
transcript_id = g.get_attr("ID")
@@ -2581,9 +2588,14 @@ def gtf(args):
25812588
gene_id = transcript_id
25822589
transcript_info[transcript_id]["gene_id"] = gene_id
25832590
transcript_info[transcript_id]["gene_type"] = g.type
2591+
g.attributes = OrderedDict(
2592+
[("gene_id", [gene_id]), ("transcript_id", [transcript_id])]
2593+
)
2594+
g.update_attributes(gtf=True, urlquote=False)
2595+
print(g)
25842596
continue
25852597

2586-
if g.type not in valid_gff_to_gtf_type.keys():
2598+
if g.type not in valid_gff_to_gtf_type:
25872599
continue
25882600

25892601
try:
@@ -2603,7 +2615,6 @@ def gtf(args):
26032615
[("gene_id", [gene_id]), ("transcript_id", [tid])]
26042616
)
26052617
g.update_attributes(gtf=True, urlquote=False)
2606-
26072618
print(g)
26082619

26092620

0 commit comments

Comments
 (0)