-
Notifications
You must be signed in to change notification settings - Fork 0
/
gff_to_db.py
62 lines (46 loc) · 1.94 KB
/
gff_to_db.py
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
#!/usr/bin/python
# Script: gff_to_db.py
# Author: Daniel Desiro'
"""
Description:
Convert a gff file into a new database or add it to a existing database.
Usage:
gff_to_db.py -g <gff_file> -i <sqlite_file>
Source:
https://github.com/desiro/gffDB/blob/master/gff_to_db.py
Reference:
http://pythonhosted.org/gffutils/contents.html
"""
import os
import gffutils
import argparse
import sys
def main(gffFile, inputDB, newDB):
# creating a new database or adding to an existing one
if inputDB:
addToDB(gffFile,inputDB)
else:
createDB(gffFile,newDB)
def addToDB(gffFile, inputDB):
# opening the database
try:
db = gffutils.FeatureDB(dbfn=inputDB)
except:
print("can't open input database")
try:
db.update(gffFile, make_backup=False, id_spec=':source:', merge_strategy='create_unique')
except:
print("can't add files to database; source key is already in use; please change the source description with the \'gff_source_editing\' tool")
def createDB(gffFile, newDB):
# creating a new database
gffutils.create_db(gffFile, dbfn=newDB, id_spec=':source:', merge_strategy='create_unique')
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog = 'gff_to_db.py', description='Create a database from a gff file or add to an excisting database.', prefix_chars='-+', epilog="")
parser.add_argument('--version', action='version', version='%(prog)s 0.1')
parser.add_argument('--gff', '-g', dest='gffFile', required=True, help='input GFF file')
parser.add_argument('--input_database', '-i', dest='inputDB', help='write to existing database')
parser.add_argument('--new_database', '-n', dest='newDB', help='create new database')
options = parser.parse_args()
if not (options.inputDB or options.newDB):
parser.error("One of --input_database or --new_database must be given")
main(options.gffFile, options.inputDB, options.newDB)