first pass cleanup of cistematic/genomes; change bamPreprocessing
[erange.git] / makesitetrack.py
1 #
2 #  makesitetrack.py
3 #  ENRAGE
4 #
5
6 import sys
7 import string
8 import optparse
9 from commoncode import getConfigParser, getConfigOption, getConfigBoolOption
10
11 print "makesitetrack: version 2.2"
12
13
14 def main(argv=None):
15     if not argv:
16         argv = sys.argv
17
18     usage = "usage: python %prog sitefile outbedfile [--noheader] [--stype fieldID] [--color xx,yy,zz] [--append] [--exploded]"
19
20     parser = makeParser(usage)
21     (options, args) = parser.parse_args(argv[1:])
22
23     if len(args) < 2:
24         print usage
25         sys.exit(1)
26
27     infile = args[0]
28     outfileName = args[1]
29     
30     makesitetrack(infile, outfileName, options.stypeID, options.color, options.append, options.compact, options.noHeader)
31
32
33 def makeParser(usage=""):
34     parser = optparse.OptionParser(usage=usage)
35     parser.add_option("--noheader", action="store_true", dest="noHeader")
36     parser.add_option("--stype", type="int", dest="stypeID")
37     parser.add_option("--color", dest="color")
38     parser.add_option("--append", action="store_true", dest="append")
39     parser.add_option("--exploded", action="store_false", dest="compact")
40
41     configParser = getConfigParser()
42     section = "makesitetrack"
43     stypeID = getConfigOption(configParser, section, "stypeID", None)
44     color = getConfigOption(configParser, section, "color", "0,0,0")
45     append = getConfigBoolOption(configParser, section, "append", False)
46     compact = getConfigBoolOption(configParser, section, "compact", True)
47     noHeader = getConfigBoolOption(configParser, section, "noHeader", False)
48
49     parser.set_defaults(stypeID=stypeID, color=color, append=append, compact=compact, noHeader=noHeader)
50
51     return parser
52
53
54 def makesitetrack(infileName, outFileName, stypeID=None, color="0,0,0", append=False, compact=True, noHeader=False):
55     if stypeID is not None:
56         doStype = True
57     else:
58         doStype = False
59         stypeID = 4
60
61     infile = open(infileName)
62
63     if append:
64         outfile = open(outFileName, "a")
65     else:
66         outfile = open(outFileName, "w")
67
68     try:
69         (name, extension) = outFileName.split(".")
70     except ValueError:
71         name = outFileName.split(".")[:-1]
72         name = string.join(name, "_")
73     
74     if not noHeader:
75         outfile.write('track name="%s" visibility=4 itemRgb="On"\n' % name)
76
77     count = 1
78     for line in infile:
79         if line[0] == "#":
80             continue
81
82         fields = line.split()
83         if compact:
84             (chrom, loc) = fields[0].split(":")
85             (start, stop) = loc.split("-")
86             score = fields[1]
87         else:
88             chrom = fields[1]
89             start = fields[2]
90             stop = fields[3]
91             score = 1.
92
93         stype = "%s-%s" % (name, str(count))
94         if doStype:
95             try:
96                 stype = fields[stypeID]
97                 if stype == "11":
98                     stype = "can"
99                 elif stype == "0":
100                     stype = "half"
101                 else:
102                     stype = "NC" + stype
103             except IndexError:
104                 pass
105
106         sense = fields[-2].strip()
107         if sense not in ["+", "-"]:
108             sense = "+"
109
110         outfile.write("%s\t%s\t%d\t%s\t%s\t%s\t-\t-\t%s\n" % (chrom, start, int(stop) + 1, stype, score, sense, color))
111         count += 1
112
113     outfile.close()
114
115
116 if __name__ == "__main__":
117     main(sys.argv)