Release version for Erange 4.0a
[erange.git] / test / testChksnp.py
1 '''
2 Created on Aug 25, 2010
3
4 @author: sau
5 '''
6 import unittest
7 import string
8 import os
9 from erange import chksnp
10
11 dbPath = "/Users/sau/work/snpdb/hg18"
12
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
19
20         Entry from altSnpDB not in sndDB
21         unknown|rs17160650|81751|81752
22     """
23
24     snpDB = "%s/dbSNP128.db" % dbPath
25     altSnpDB = "%s/snp129cDNA.db" % dbPath
26     inputFileName = "testChkSNP_input.txt"
27     outputFileName = "testChkSNP_output.txt"
28
29     def setUp(self):
30         pass
31
32
33     def tearDown(self):
34         pass
35
36
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)
44         infile.close()
45
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)
54         outfile.close()
55         os.remove(self.outputFileName)
56
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)
65         outfile.close()
66
67         os.remove(self.inputFileName)
68         os.remove(self.outputFileName)
69
70
71     def testMain(self):
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)
78         infile.close()
79
80         argv = ["chksnp", self.snpDB, self.inputFileName, self.outputFileName]
81         chksnp.main(argv)
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)
89         outfile.close()
90
91         os.remove(self.inputFileName)
92         os.remove(self.outputFileName)
93
94
95     def testChkSNP(self):
96         snpPropertiesList = []
97         dbList = [self.snpDB]
98         self.assertEquals({}, chksnp.chkSNP(dbList, snpPropertiesList))
99
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))
109
110
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)
118
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)
125
126
127     def testDoNotProcessLine(self):
128         self.assertTrue(chksnp.doNotProcessLine("#anything"))
129         self.assertFalse(chksnp.doNotProcessLine("line to process"))
130
131
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))
139
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))
146
147
148     def testAnnotateSNPFromDBList(self):
149         snpLocationList = []
150         snpDict = {}
151         dbList = [self.snpDB]
152         self.assertEquals({}, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
153
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))
159
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))
165
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))
171
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))
179
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))
187
188
189 def suite():
190     suite = unittest.TestSuite()
191     suite.addTest(unittest.makeSuite(TestChksnp))
192
193     return suite
194
195
196 if __name__ == "__main__":
197     #import sys;sys.argv = ['', 'Test.testName']
198     unittest.main()