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
35 def testChkSNPFile(self):
36 inputFileName = "testChkSNP_input.txt"
37 infile = open(inputFileName, "w")
38 infile.write("# header line\n")
39 snpEntry = string.join(["foo", "foo", "chr1", "691"], "\t")
40 infile.write("%s\n" % snpEntry)
41 snpEntry = string.join(["foo2", "foo2", "chr1", "81752"], "\t")
42 infile.write("%s\n" % snpEntry)
45 outputFileName = "testChkSNP_output.txt"
47 chksnp.chkSNPFile(self.snpDB, inputFileName, outputFileName)
48 outfile = open(outputFileName, "r")
49 line = outfile.readline()
50 result = "foo\tfoo\tchr1\t691\trs10218492\tunknown\n"
51 self.assertEquals(result, line)
52 result = "foo2\tfoo2\tchr1\t81752\tN\\A\tN\\A\n"
53 line = outfile.readline()
54 self.assertEquals(result, line)
56 os.remove(outputFileName)
58 chksnp.chkSNPFile(self.snpDB, inputFileName, outputFileName, snpDBList=[self.altSnpDB])
59 outfile = open(outputFileName, "r")
60 line = outfile.readline()
61 result = "foo\tfoo\tchr1\t691\trs10218492\tunknown\n"
62 self.assertEquals(result, line)
63 result = "foo2\tfoo2\tchr1\t81752\trs17160650\tunknown\n"
64 line = outfile.readline()
65 self.assertEquals(result, line)
68 os.remove(inputFileName)
69 os.remove(outputFileName)
73 inputFileName = "testChkSNP_input.txt"
74 infile = open(inputFileName, "w")
75 infile.write("# header line\n")
76 snpEntry = string.join(["foo", "foo", "chr1", "691"], "\t")
77 infile.write("%s\n" % snpEntry)
78 snpEntry = string.join(["foo2", "foo2", "chr1", "81752"], "\t")
79 infile.write("%s\n" % snpEntry)
82 outputFileName = "testChkSNP_output.txt"
84 argv = ["chksnp", self.snpDB, inputFileName, outputFileName]
86 outfile = open(outputFileName, "r")
87 line = outfile.readline()
88 result = "foo\tfoo\tchr1\t691\trs10218492\tunknown\n"
89 self.assertEquals(result, line)
90 result = "foo2\tfoo2\tchr1\t81752\tN\\A\tN\\A\n"
91 line = outfile.readline()
92 self.assertEquals(result, line)
94 os.remove(outputFileName)
97 snpPropertiesList = []
99 self.assertEquals({}, chksnp.chkSNP(dbList, snpPropertiesList))
101 snpPropertiesList = ["# header line"]
102 snpEntry = string.join(["foo", "foo", "chr1", "691"], "\t")
103 snpPropertiesList.append(snpEntry)
104 snpEntry = string.join(["foo2", "foo2", "chr1", "81752"], "\t")
105 snpPropertiesList.append(snpEntry)
106 dbList = [self.snpDB, self.altSnpDB]
107 result = {("1", 691): "foo\tfoo\tchr1\t691\trs10218492\tunknown",
108 ("1", 81752): "foo2\tfoo2\tchr1\t81752\trs17160650\tunknown"}
109 self.assertEquals(result, chksnp.chkSNP(dbList, snpPropertiesList))
112 def testGetSNPLocationInfo(self):
113 snpPropertiesList = []
114 snpEntry = string.join(["foo", "foo", "chr1", "20"], "\t")
115 snpPropertiesList.append(snpEntry)
116 snpLocationList, snpDict = chksnp.getSNPLocationInfo(snpPropertiesList)
117 self.assertEquals([("1", 20)], snpLocationList)
118 self.assertEquals({("1", 20): "foo\tfoo\tchr1\t20"}, snpDict)
120 snpPropertiesList = ["# header line"]
121 snpEntry = string.join(["foo", "foo", "chr1", "20"], "\t")
122 snpPropertiesList.append(snpEntry)
123 snpLocationList, snpDict = chksnp.getSNPLocationInfo(snpPropertiesList)
124 self.assertEquals([("1", 20)], snpLocationList)
125 self.assertEquals({("1", 20): "foo\tfoo\tchr1\t20"}, snpDict)
128 def testDoNotProcessLine(self):
129 self.assertTrue(chksnp.doNotProcessLine("#anything"))
130 self.assertFalse(chksnp.doNotProcessLine("line to process"))
133 def testAnnotateSNPFromDB(self):
134 snpLocationList = [("1", 691), ("1", 81752)]
135 snpDict = {("1", 691): "foo\tfoo\tchr1\t691",
136 ("1", 81752): "foo2\tfoo2\tchr1\t81752"}
137 result = {("1", 691): "foo\tfoo\tchr1\t691\trs10218492\tunknown",
138 ("1", 81752): "foo2\tfoo2\tchr1\t81752\tN\\A\tN\\A"}
139 self.assertEquals(result, chksnp.annotateSNPFromDB(snpLocationList, snpDict, self.snpDB))
141 snpLocationList = [("1", 691), ("1", 81752)]
142 snpDict = {("1", 691): "foo\tfoo\tchr1\t691",
143 ("1", 81752): "foo2\tfoo2\tchr1\t81752"}
144 result = {("1", 691): "foo\tfoo\tchr1\t691\tN\\A\tN\\A",
145 ("1", 81752): "foo2\tfoo2\tchr1\t81752\trs17160650\tunknown"}
146 self.assertEquals(result, chksnp.annotateSNPFromDB(snpLocationList, snpDict, self.altSnpDB))
149 def testAnnotateSNPFromDBList(self):
152 dbList = [self.snpDB]
153 self.assertEquals({}, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
155 snpLocationList = [("1", 21)]
156 snpDict = {("1", 21): "foo\tfoo\tchr1\t21"}
157 dbList = [self.snpDB]
158 result = {("1", 21): "foo\tfoo\tchr1\t21\tN\\A\tN\\A"}
159 self.assertEquals(result, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
161 snpLocationList = [("1", 21)]
162 snpDict = {("1", 21): "foo\tfoo\tchr1\t21"}
163 dbList = [self.snpDB]
164 result = {("1", 21): "foo\tfoo\tchr1\t21\tN\\A\tN\\A"}
165 self.assertEquals(result, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList, cachePages=10000))
167 snpLocationList = [("1", 691)]
168 snpDict = {("1", 691): "foo\tfoo\tchr1\t691"}
169 dbList = [self.snpDB]
170 result = {("1", 691): "foo\tfoo\tchr1\t691\trs10218492\tunknown"}
171 self.assertEquals(result, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
173 snpLocationList = [("1", 691), ("1", 81752)]
174 snpDict = {("1", 691): "foo\tfoo\tchr1\t691",
175 ("1", 81752): "foo2\tfoo2\tchr1\t81752"}
176 dbList = [self.snpDB]
177 result = {("1", 691): "foo\tfoo\tchr1\t691\trs10218492\tunknown",
178 ("1", 81752): "foo2\tfoo2\tchr1\t81752\tN\\A\tN\\A"}
179 self.assertEquals(result, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
181 snpLocationList = [("1", 691), ("1", 81752)]
182 snpDict = {("1", 691): "foo\tfoo\tchr1\t691",
183 ("1", 81752): "foo2\tfoo2\tchr1\t81752"}
184 dbList = [self.snpDB, self.altSnpDB]
185 result = {("1", 691): "foo\tfoo\tchr1\t691\trs10218492\tunknown",
186 ("1", 81752): "foo2\tfoo2\tchr1\t81752\trs17160650\tunknown"}
187 self.assertEquals(result, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
191 suite = unittest.TestSuite()
192 suite.addTest(unittest.makeSuite(TestChksnp))
197 if __name__ == "__main__":
198 #import sys;sys.argv = ['', 'Test.testName']