2 Created on Aug 25, 2010
9 from erange import chksnp
11 dbPath = "/Users/sau/work/snpdb/hg18"
13 class TestChksnp(unittest.TestCase):
14 """ First entries from snpDB using select func, name, start, stop from snp where chrom="1" limit 4;
15 unknown|rs10218492|690|691
16 unknown|rs10218493|766|767
17 unknown|rs10218527|789|790
18 unknown|rs28853987|800|801
20 Entry from altSnpDB not in sndDB
21 unknown|rs17160650|81751|81752
24 snpDB = "%s/dbSNP128.db" % dbPath
25 altSnpDB = "%s/snp129cDNA.db" % dbPath
26 inputFileName = "testChkSNP_input.txt"
27 outputFileName = "testChkSNP_output.txt"
37 def testChkSNPFile(self):
38 infile = open(self.inputFileName, "w")
39 infile.write("# header line\n")
40 snpEntry = string.join(["foo", "foo", "chr1", "691"], "\t")
41 infile.write("%s\n" % snpEntry)
42 snpEntry = string.join(["foo2", "foo2", "chr1", "81752"], "\t")
43 infile.write("%s\n" % snpEntry)
46 chksnp.chkSNPFile(self.snpDB, self.inputFileName, self.outputFileName)
47 outfile = open(self.outputFileName, "r")
48 line = outfile.readline()
49 result = "foo\tfoo\tchr1\t691\trs10218492\tunknown\n"
50 self.assertEquals(result, line)
51 result = "foo2\tfoo2\tchr1\t81752\tN\\A\tN\\A\n"
52 line = outfile.readline()
53 self.assertEquals(result, line)
55 os.remove(self.outputFileName)
57 chksnp.chkSNPFile(self.snpDB, self.inputFileName, self.outputFileName, snpDBList=[self.altSnpDB])
58 outfile = open(self.outputFileName, "r")
59 line = outfile.readline()
60 result = "foo\tfoo\tchr1\t691\trs10218492\tunknown\n"
61 self.assertEquals(result, line)
62 result = "foo2\tfoo2\tchr1\t81752\trs17160650\tunknown\n"
63 line = outfile.readline()
64 self.assertEquals(result, line)
67 os.remove(self.inputFileName)
68 os.remove(self.outputFileName)
72 infile = open(self.inputFileName, "w")
73 infile.write("# header line\n")
74 snpEntry = string.join(["foo", "foo", "chr1", "691"], "\t")
75 infile.write("%s\n" % snpEntry)
76 snpEntry = string.join(["foo2", "foo2", "chr1", "81752"], "\t")
77 infile.write("%s\n" % snpEntry)
80 argv = ["chksnp", self.snpDB, self.inputFileName, self.outputFileName]
82 outfile = open(self.outputFileName, "r")
83 line = outfile.readline()
84 result = "foo\tfoo\tchr1\t691\trs10218492\tunknown\n"
85 self.assertEquals(result, line)
86 result = "foo2\tfoo2\tchr1\t81752\tN\\A\tN\\A\n"
87 line = outfile.readline()
88 self.assertEquals(result, line)
91 os.remove(self.inputFileName)
92 os.remove(self.outputFileName)
96 snpPropertiesList = []
98 self.assertEquals({}, chksnp.chkSNP(dbList, snpPropertiesList))
100 snpPropertiesList = ["# header line"]
101 snpEntry = string.join(["foo", "foo", "chr1", "691"], "\t")
102 snpPropertiesList.append(snpEntry)
103 snpEntry = string.join(["foo2", "foo2", "chr1", "81752"], "\t")
104 snpPropertiesList.append(snpEntry)
105 dbList = [self.snpDB, self.altSnpDB]
106 result = {("1", 691): "foo\tfoo\tchr1\t691\trs10218492\tunknown",
107 ("1", 81752): "foo2\tfoo2\tchr1\t81752\trs17160650\tunknown"}
108 self.assertEquals(result, chksnp.chkSNP(dbList, snpPropertiesList))
111 def testGetSNPLocationInfo(self):
112 snpPropertiesList = []
113 snpEntry = string.join(["foo", "foo", "chr1", "20"], "\t")
114 snpPropertiesList.append(snpEntry)
115 snpLocationList, snpDict = chksnp.getSNPLocationInfo(snpPropertiesList)
116 self.assertEquals([("1", 20)], snpLocationList)
117 self.assertEquals({("1", 20): "foo\tfoo\tchr1\t20"}, snpDict)
119 snpPropertiesList = ["# header line"]
120 snpEntry = string.join(["foo", "foo", "chr1", "20"], "\t")
121 snpPropertiesList.append(snpEntry)
122 snpLocationList, snpDict = chksnp.getSNPLocationInfo(snpPropertiesList)
123 self.assertEquals([("1", 20)], snpLocationList)
124 self.assertEquals({("1", 20): "foo\tfoo\tchr1\t20"}, snpDict)
127 def testDoNotProcessLine(self):
128 self.assertTrue(chksnp.doNotProcessLine("#anything"))
129 self.assertFalse(chksnp.doNotProcessLine("line to process"))
132 def testAnnotateSNPFromDB(self):
133 snpLocationList = [("1", 691), ("1", 81752)]
134 snpDict = {("1", 691): "foo\tfoo\tchr1\t691",
135 ("1", 81752): "foo2\tfoo2\tchr1\t81752"}
136 result = {("1", 691): "foo\tfoo\tchr1\t691\trs10218492\tunknown",
137 ("1", 81752): "foo2\tfoo2\tchr1\t81752\tN\\A\tN\\A"}
138 self.assertEquals(result, chksnp.annotateSNPFromDB(snpLocationList, snpDict, self.snpDB))
140 snpLocationList = [("1", 691), ("1", 81752)]
141 snpDict = {("1", 691): "foo\tfoo\tchr1\t691",
142 ("1", 81752): "foo2\tfoo2\tchr1\t81752"}
143 result = {("1", 691): "foo\tfoo\tchr1\t691\tN\\A\tN\\A",
144 ("1", 81752): "foo2\tfoo2\tchr1\t81752\trs17160650\tunknown"}
145 self.assertEquals(result, chksnp.annotateSNPFromDB(snpLocationList, snpDict, self.altSnpDB))
148 def testAnnotateSNPFromDBList(self):
151 dbList = [self.snpDB]
152 self.assertEquals({}, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
154 snpLocationList = [("1", 21)]
155 snpDict = {("1", 21): "foo\tfoo\tchr1\t21"}
156 dbList = [self.snpDB]
157 result = {("1", 21): "foo\tfoo\tchr1\t21\tN\\A\tN\\A"}
158 self.assertEquals(result, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
160 snpLocationList = [("1", 21)]
161 snpDict = {("1", 21): "foo\tfoo\tchr1\t21"}
162 dbList = [self.snpDB]
163 result = {("1", 21): "foo\tfoo\tchr1\t21\tN\\A\tN\\A"}
164 self.assertEquals(result, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList, cachePages=10000))
166 snpLocationList = [("1", 691)]
167 snpDict = {("1", 691): "foo\tfoo\tchr1\t691"}
168 dbList = [self.snpDB]
169 result = {("1", 691): "foo\tfoo\tchr1\t691\trs10218492\tunknown"}
170 self.assertEquals(result, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
172 snpLocationList = [("1", 691), ("1", 81752)]
173 snpDict = {("1", 691): "foo\tfoo\tchr1\t691",
174 ("1", 81752): "foo2\tfoo2\tchr1\t81752"}
175 dbList = [self.snpDB]
176 result = {("1", 691): "foo\tfoo\tchr1\t691\trs10218492\tunknown",
177 ("1", 81752): "foo2\tfoo2\tchr1\t81752\tN\\A\tN\\A"}
178 self.assertEquals(result, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
180 snpLocationList = [("1", 691), ("1", 81752)]
181 snpDict = {("1", 691): "foo\tfoo\tchr1\t691",
182 ("1", 81752): "foo2\tfoo2\tchr1\t81752"}
183 dbList = [self.snpDB, self.altSnpDB]
184 result = {("1", 691): "foo\tfoo\tchr1\t691\trs10218492\tunknown",
185 ("1", 81752): "foo2\tfoo2\tchr1\t81752\trs17160650\tunknown"}
186 self.assertEquals(result, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
190 suite = unittest.TestSuite()
191 suite.addTest(unittest.makeSuite(TestChksnp))
196 if __name__ == "__main__":
197 #import sys;sys.argv = ['', 'Test.testName']