first pass cleanup of cistematic/genomes; change bamPreprocessing
[erange.git] / makeSNPtrack.py
1 #
2 #  makeSNPtrack.py
3 #  ENRAGE
4 #
5 # This script maps all the qualified SNC sites on to the genome browser 
6 # Output format: bed
7 # Written by: Wendy Lee
8 # Written on: August 18th, 2008
9 # Last Modified: December 14th, 2008 by Ali Mortazavi
10
11 import sys
12
13 def main(argv=None):
14     if not argv:
15         argv = sys.argv
16
17     print "makeSNPtrack: version 1.3"
18
19     if len(argv) < 4:
20         print "usage: python %s snpfile trackname trackoutfile" % argv[0]
21         sys.exit(1)
22
23     snpfile = argv[1]
24     track = argv[2]
25     outfile = argv[3]
26
27     makeSNPtrack(snpfile, track, outfile)
28
29
30 def makeSNPtrack(snpfilename, track, outfilename):
31
32     snpfile = open(snpfilename, "r")
33     writeSNPsBedfile(snpfile, track, outfilename)
34     snpfile.close()
35
36
37 def writeSNPsBedfile(snpPropertiesList, track, outfilename):
38
39     outfile = open(outfilename, "w")
40     header = getHeader(track)
41     outfile.write(header)
42
43     for line in snpPropertiesList:
44         if doNotProcessLine(line):
45             continue
46
47         fields = line.strip().split()
48         outline = getBedOutputLine(fields)
49         outfile.write(outline)
50
51     outfile.close()
52
53
54 def getHeader(track):
55     header = "track name=%s description=%s visibility=2 itemRgb=\"On\"\n" % (track, track)
56     return header
57
58
59 def doNotProcessLine(line):
60     return line[0] == "#"
61
62
63 def getBedOutputLine(snpPropertiesList):
64     chromosome = snpPropertiesList[2]
65     readStart = int(snpPropertiesList[3]) - 1
66     readStop = readStart + 1
67     readName = snpPropertiesList[7]
68     color = getSNPColor(readName)
69     score = "0"
70     sense = "+"
71     outline = "%s\t%d\t%d\t%s\t%s\t%s\t-\t-\t\t%s\n" % (chromosome, readStart, readStop, readName, score, sense, color)
72
73     return outline
74
75
76 def getSNPColor(readName):
77     baseColor = {"A": "200, 0, 255",
78                  "T": "200, 0, 255",
79                  "C": "200, 0, 255",
80                  "G": "200, 0, 255"
81     }
82
83     specialColors = {"A-G": "255, 0, 0",
84                      "T-C": "0, 0, 255"
85     }
86
87     if readName in specialColors.keys():
88         color = specialColors[readName]
89     else:
90         try:
91             color = baseColor[readName[-1]]
92         except (IndexError, KeyError):
93             color = "200, 0, 255"
94
95     return color
96
97
98 if __name__ == "__main__":
99     main(sys.argv)