erange version 4.0a dev release
[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
27     def setUp(self):
28         pass
29
30
31     def tearDown(self):
32         pass
33
34
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)
43         infile.close()
44
45         outputFileName = "testChkSNP_output.txt"
46
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)
55         outfile.close()
56         os.remove(outputFileName)
57
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)
66         outfile.close()
67
68         os.remove(inputFileName)
69         os.remove(outputFileName)
70
71
72     def testMain(self):
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)
80         infile.close()
81
82         outputFileName = "testChkSNP_output.txt"
83
84         argv = ["chksnp", self.snpDB, inputFileName, outputFileName]
85         chksnp.main(argv)
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)
93         outfile.close()
94         os.remove(outputFileName)
95
96     def testChkSNP(self):
97         snpPropertiesList = []
98         dbList = [self.snpDB]
99         self.assertEquals({}, chksnp.chkSNP(dbList, snpPropertiesList))
100
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))
110
111
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)
119
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)
126
127
128     def testDoNotProcessLine(self):
129         self.assertTrue(chksnp.doNotProcessLine("#anything"))
130         self.assertFalse(chksnp.doNotProcessLine("line to process"))
131
132
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))
140
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))
147
148
149     def testAnnotateSNPFromDBList(self):
150         snpLocationList = []
151         snpDict = {}
152         dbList = [self.snpDB]
153         self.assertEquals({}, chksnp.annotateSNPFromDBList(snpLocationList, snpDict, dbList))
154
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))
160
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))
166
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))
172
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))
180
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))
188
189
190 def suite():
191     suite = unittest.TestSuite()
192     suite.addTest(unittest.makeSuite(TestChksnp))
193
194     return suite
195
196
197 if __name__ == "__main__":
198     #import sys;sys.argv = ['', 'Test.testName']
199     unittest.main()