forked from roryk/junkdrawer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditAttributes.py
More file actions
90 lines (76 loc) · 3.48 KB
/
editAttributes.py
File metadata and controls
90 lines (76 loc) · 3.48 KB
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
from gtfUtils import GTFtoDict, outputGTF, swapAttributes, delAttributes
from gtfUtils import addAttribute, filterAttributes, reorderAttributes
from argparse import ArgumentParser
import os
import logging
def main():
logging.basicConfig(format='%(levelname)s: %(asctime)s %(message)s',
level=logging.INFO)
description = "Replaces the attributes in -r with the ones from -s. " \
"Deletes the attributes in -d."
parser = ArgumentParser(description=description)
parser.add_argument("-g", "--gtf", dest="gtf", default=False,
type=str, required=True,
help="combined.gtf file from Cuffcompare")
parser.add_argument("-s", "--source", nargs="*", dest="source",
default=False,
help="attributes to replace the -r attributes")
parser.add_argument("-r", "--replace", nargs="*", dest="replace",
default=False,
help="attributes to be replaced")
parser.add_argument("-d", "--delete", nargs="*", dest="delete",
default=False,
help="attributes to be deleted")
parser.add_argument("-a", "--add", dest="add", default=False,
type=str,
help="file of attributes to be added")
parser.add_argument("-f", "--filter", dest="filter", default=False,
type=str,
help="file of attribute to filter on")
parser.add_argument("-o", "--output", dest="output", default=False,
type=str, required=True,
help="output filename")
parser.add_argument("-m", "--move", dest="move", default=False,
help="list of attributes to move to the front")
args = parser.parse_args()
if not os.path.isfile(args.gtf):
logging.error("%s cannot be found." %(args.gtf))
parser.print_help()
exit(-1)
if os.path.isfile(args.output):
logging.error("%s already exists." %(args.output))
parser.print_help()
exit(-1)
# check to make sure arguments make sense
if args.source or args.replace:
if len(args.source) != len(args.replace):
logging.error("Source and replacement lengths must be the same.")
parser.print_help()
exit(-1)
if not (args.source or args.replace or args.delete or
args.add or args.filter or args.move):
logging.error("Must provide at least one action.")
parser.print_help()
exit(-1)
gtflines = GTFtoDict(args.gtf)
if args.source:
logging.info("Swapping attributes.")
gtflines = swapAttributes(gtflines, args.source, args.replace)
if args.add:
if not os.path.isfile(args.add):
logging.error("%s cannot be found." %(args.add))
exit(-1)
logging.info("Adding attributes.")
gtflines = addAttribute(gtflines, args.add)
if args.delete:
logging.info("Deleting attributes.")
gtflines = delAttributes(gtflines, args.delete)
if args.filter:
logging.info("Filtering by attribute in file %s." %(args.filter))
gtflines = filterAttributes(gtflines, args.filter)
if args.move:
logging.info("Moving %s to the front of the attributes." %(args.move))
gtflines = reorderAttributes(gtflines, args.move)
outputGTF(gtflines, args.output)
if __name__ == "__main__":
main()