File: gffutils-cli

package info (click to toggle)
python-gffutils 0.13-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,164 kB
  • sloc: python: 5,557; makefile: 57; sh: 13
file content (302 lines) | stat: -rwxr-xr-x 10,494 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/python3

"""
Command line interface for gffutils.

Developer notes
---------------

To implement or modify subcommands (e.g., "gffutils-cli fetch"), simply edit
the correspondingly-named functions.

Since the `argh` module is used to wrap functions into argparse.ArgumentParser
objects (and sub-parsers), just add an argument to the appropriate function and
you'll get the cli for free.
"""

import os
import gffutils
import gffutils.helpers as helpers
from gffutils.exceptions import FeatureNotFoundError
import gffutils.gffwriter as gffwriter
import argh
from argcomplete.completers import EnvironCompleter
from argh import arg
import sys

# D.R.Y.
db_help = '''Database to use.  If a GFF or GTF file is provided instead for
this argument, a database will be created for you.  This can take some time
(several minutes), so it's best to create one ahead of time.'''


def handle_relations_args(ids, limit, exclude):
    """
    Handle args for children or parents subcommands
    """
    if limit:
        raise NotImplementedError('sorry, still working on --limit')
    if ids is None:
        ids = gffdb.features_of_type('gene')
    else:
        ids = ids.split(',')
    exclude = (exclude or "").split(',')
    return ids, limit, exclude


# fetch subcommand ------------------------------------------------------------
@arg('db', help=db_help)
@arg('ids', help='Comma-separated list of IDs to fetch')
def fetch(db, ids):
    """
    Fetch IDs.
    """
    if not os.path.isfile(db):
        raise Exception("Cannot fetch: %s does not exist." % (db))
    gff_db = None
    if not helpers.is_gff_db(db):
        gff_db = helpers.get_gff_db(db)
    else:
        gff_db = db
    for i in ids.split(','):
        try:
            yield gff_db[i]
        except FeatureNotFoundError:
            print ("%s not found" % i, file=sys.stderr)


# children subcommand ---------------------------------------------------------
@arg('db', help=db_help)
@arg('ids', nargs='?', help='''Comma-separated list of IDs. These IDs, along
     with their children, will be returned (subject to --limit and --exclude).
     If none provided, use all genes in the database''')
@arg('--limit', help='''Feature type (string), or level (integer).  No children
     below this level will be returned''')
@arg('--exclude', help='''Comma-separated list of featuretypes to filter out
     (like grep -v)''')
@arg('--exclude-self', help='''Use this to suppress reporting the IDs you've
     provided.''')
def children(db, ids, limit=None, exclude=None, exclude_self=False):
    """
    Fetch children from the database according to ID.
    """
    db = gffutils.FeatureDB(db)
    ids, limit, exclude = handle_relations_args(ids, limit, exclude)

    for i in ids:
        f = db[i]
        if f.featuretype in exclude:
            continue
        if not exclude_self:
            yield f
        for child1 in db.children(i):
            if child1.featuretype in exclude:
                continue
            yield child1
            for child2 in db.children(child1):
                if child2.featuretype in exclude:
                    continue
                yield child2


# parents subcommand ----------------------------------------------------------
@arg('db', help=db_help)
@arg('ids', nargs='?', help='''Comma-separated list of IDs. These IDs, along
     with their parents, will be returned (subject to --limit and --exclude).
     If none provided, use all exons in the database''')
@arg('--limit', help='''Feature type (string), or level (integer).  No parents
     below this level will be returned''')
@arg('--exclude', help='''Comma-separated list of featuretypes to filter out
     (like grep -v)''')
@arg('--exclude-self', help='''Use this to suppress reporting the IDs you've
     provided.''')
def parents(db, ids, limit=None, exclude=None, exclude_self=False):
    """
    Fetch parents from the database according to ID.
    """
    db = gffutils.FeatureDB(db)
    ids, limit, exclude = handle_relations_args(ids, limit, exclude)
    for i in ids:
        f = db[i]
        if f.featuretype in exclude:
            continue
        if not exclude_self:
            yield f
        for parent1 in db.parents(i):
            if parent1.featuretype in exclude:
                continue
            yield parent1
            for parent2 in db.parents(parent1):
                if parent2.featuretype in exclude:
                    continue
                yield parent2


# region subcommand -----------------------------------------------------------
@arg('db', help=db_help)
@arg('region', help='Genomic coordinates of the form "chrom:start-stop"')
def region(db, region):
    """
    Returns features within provided genomic coordinates.
    """
    raise NotImplementedError('sorry, still working on "region"')


# common subcommand------------------------------------------------------------
@arg('db', help=db_help)
def common(db):
    """
    Identify child features in common (e.g., common exons across multiple
    isoforms)
    """
    raise NotImplementedError('sorry, still working on "common"')


# create subcommand------------------------------------------------------------
@arg('filename', help='GFF or GTF file to use')
@arg('--output', help='''Database to create.  Default is to append ".db" to the
     end of the input filename''')
@arg('--force', help='''Overwrite an existing database''')
@arg('--quiet', help='''Suppress the reporting of timing information when
     creating the database''')
@arg('--merge', help='''Merge strategy to be used if if duplicate IDs are
     found.''')
@arg('--disable-infer-genes', help='''Disable inferring of gene
     extents for GTF files. Use this if your GTF file already has "gene"
     featuretypes''')
@arg('--disable-infer-transcripts', help='''Disable inferring of transcript
     extents for GTF files. Use this if your GTF file already has "transcript"
     featuretypes''')
def create(filename, output=None, force=False, quiet=False, merge="merge",
           disable_infer_genes=False, disable_infer_transcripts=False):
    """
    Create a database.
    """
    verbose = not quiet
    if output is None:
        output = filename + '.db'
    gffutils.create_db(filename, output,
                       force=force,
                       verbose=verbose,
                       merge_strategy=merge,
                       disable_infer_genes=disable_infer_genes,
                       disable_infer_transcripts=disable_infer_transcripts)


# clean subcommand ------------------------------------------------------------
@arg('filename', help='''GFF or GTF file to use''')
def clean(filename):
    """
    Perform various QC operations to clean a GFF or GTF file.
    """
    raise NotImplementedError('sorry, still working on "clean"')


# sanitize subcommand ---------------------------------------------------------
@arg('filename', help='''GFF or GTF file to use (or GFF database.)''')
@arg('--in-memory', help='''Load GFF into memory for processing.''')
@arg('--in-place',
     help='''Sanitize file in-place: overwrites current file with sanitized
     version.''')
def sanitize(filename,
             in_memory=True,
             in_place=False):
    """
    Sanitize a GFF file. Might get merged with clean feature later.

    Cleans and adds useful annotations to a GFF file:

      - Ensures that start > end in all entries
      - Adds an entry id (eid) to each entry to make files grep-able

    Outputs result to stdout unless asked to sanitize in place.
    """
    print("Sanitizing GFF %s" % filename, file=sys.stderr)
    if in_memory:
        print("  - Loading GFF in memory", file=sys.stderr)
    if in_place:
        print("  - Sanitizing file in place (overwriting current file)",
              file=sys.stderr)
    helpers.sanitize_gff_file(filename,
                              in_memory=in_memory,
                              in_place=in_place)


@arg('filename', help='''GFF or GTF file to use.''')
@arg('--in-place', help='''Remove duplicates in place (overwrite current
     file.)''')
def rmdups(filename, in_place=False):
    """
    Remove duplicates from a GFF file.
    """
    print("Removing duplicates from: %s" % filename)
    merge_strategy = "merge"
    if not in_place:
        # Write to stdout
        gff_out = gffwriter.GFFWriter(sys.stdout)
    else:
        # Write to file in place
        gff_out = gffwriter.GFFWriter(filename, in_place=True)
    # Create database with merge
    db = gffutils.create_db(filename, ":memory:",
                            verbose=False,
                            merge_strategy=merge_strategy)
    # Write out the records
    for rec in db.all_features():
        gff_out.write_rec(rec)
    gff_out.close()


# annotate subcommand ---------------------------------------------------------
@arg('filename', help='''GFF or GTF file to use''')
@arg('genes-table', help='''Genes table in GFF or GTF format to use.''')
def annotate(filename, genes_table):
    """
    Annotate a GFF file with useful information. For now, add annotation
    of gene IDs based on an input GFF annotation of genes.

    Computes the most inclusive transcription start/end coordinates
    for each gene, and then uses pybedtools to intersect (in strand-specific
    manner) with the input annotation.
    """
    raise NotImplementedError('implementation in progress')


# convert subcommand ----------------------------------------------------------
@arg('filename', help='''GFF or GTF file to convert''')
def convert(filename):
    """
    Convert a GTF file to GFF or vice versa.
    """
    raise NotImplementedError('implementation in progress')


# search subcommand -----------------------------------------------------------
@arg('db', help=db_help)
@arg('text', help='''Text to search for. Case-insensitive; use sql LIKE
     syntax''')
@arg('--featuretype', help='''Restrict to a particular featuretype.  This can
     be faster than doing a grep on the output, since it restricts the search
     space in the database''')
def search(db, text, featuretype=None):
    """
    Search the attributes.
    """
    db = gffutils.FeatureDB(db)
    for item in db.attribute_search(text, featuretype):
        yield item


if __name__ == "__main__":
    argh.dispatch_commands([
        fetch,
        children,
        parents,
        region,
        create,
        common,
        clean,
        search,
        sanitize,
        rmdups
    ])