18
|
1 import unittest
|
|
2 import os
|
|
3 from optparse import OptionParser
|
|
4 from SMART.Java.Python.structure.Transcript import Transcript
|
|
5 from commons.core.parsing.ParserChooser import ParserChooser
|
|
6 from commons.core.parsing.GffParser import GffParser
|
|
7 from commons.core.writer.Gff3Writer import Gff3Writer
|
|
8 from SMART.Java.Python.CombineTags import CombineTags
|
|
9
|
|
10 class Test_F_CombineTags(unittest.TestCase):
|
|
11
|
|
12 def setUp(self):
|
|
13 self._inputFileName = "inputFileTest.gff3"
|
|
14 self._outputFileName = "outputFileTest.gff3"
|
|
15 self._tag1 = "tag1"
|
|
16 self._tag2 = "tag2"
|
|
17 self._outputTag = "outputTag"
|
|
18
|
|
19 def tearDown(self):
|
|
20 os.remove(self._inputFileName)
|
|
21 os.remove(self._outputFileName)
|
|
22
|
|
23 def test_plus(self):
|
|
24 transcript = self._createDummyTranscript()
|
|
25 transcript.setTagValue(self._tag1, 1)
|
|
26 transcript.setTagValue(self._tag2, 2)
|
|
27 self._writeTranscript(transcript)
|
|
28 combiner = CombineTags(0)
|
|
29 combiner.setInputFile(self._inputFileName, "gff3")
|
|
30 combiner.setOutputFile(self._outputFileName)
|
|
31 combiner.setTags(self._tag1, self._tag2, self._outputTag, None)
|
|
32 combiner.setOperation("plus")
|
|
33 combiner.run()
|
|
34 self._testOutputTag(3)
|
|
35
|
|
36 def test_minus(self):
|
|
37 transcript = self._createDummyTranscript()
|
|
38 transcript.setTagValue(self._tag1, 2)
|
|
39 transcript.setTagValue(self._tag2, 1)
|
|
40 self._writeTranscript(transcript)
|
|
41 combiner = CombineTags(0)
|
|
42 combiner.setInputFile(self._inputFileName, "gff3")
|
|
43 combiner.setOutputFile(self._outputFileName)
|
|
44 combiner.setTags(self._tag1, self._tag2, self._outputTag, None)
|
|
45 combiner.setOperation("minus")
|
|
46 combiner.run()
|
|
47 self._testOutputTag(1)
|
|
48
|
|
49 def test_times(self):
|
|
50 transcript = self._createDummyTranscript()
|
|
51 transcript.setTagValue(self._tag1, 6)
|
|
52 transcript.setTagValue(self._tag2, 3)
|
|
53 self._writeTranscript(transcript)
|
|
54 combiner = CombineTags(0)
|
|
55 combiner.setInputFile(self._inputFileName, "gff3")
|
|
56 combiner.setOutputFile(self._outputFileName)
|
|
57 combiner.setTags(self._tag1, self._tag2, self._outputTag, None)
|
|
58 combiner.setOperation("times")
|
|
59 combiner.run()
|
|
60 self._testOutputTag(18)
|
|
61
|
|
62 def test_div(self):
|
|
63 transcript = self._createDummyTranscript()
|
|
64 transcript.setTagValue(self._tag1, 6)
|
|
65 transcript.setTagValue(self._tag2, 3)
|
|
66 self._writeTranscript(transcript)
|
|
67 combiner = CombineTags(0)
|
|
68 combiner.setInputFile(self._inputFileName, "gff3")
|
|
69 combiner.setOutputFile(self._outputFileName)
|
|
70 combiner.setTags(self._tag1, self._tag2, self._outputTag, None)
|
|
71 combiner.setOperation("div")
|
|
72 combiner.run()
|
|
73 self._testOutputTag(2)
|
|
74
|
|
75 def test_default(self):
|
|
76 transcript = self._createDummyTranscript()
|
|
77 transcript.setTagValue(self._tag1, 1)
|
|
78 self._writeTranscript(transcript)
|
|
79 combiner = CombineTags(0)
|
|
80 combiner.setInputFile(self._inputFileName, "gff3")
|
|
81 combiner.setOutputFile(self._outputFileName)
|
|
82 combiner.setTags(self._tag1, self._tag2, self._outputTag, 0)
|
|
83 combiner.setOperation("plus")
|
|
84 combiner.run()
|
|
85 self._testOutputTag(0)
|
|
86
|
|
87 def _createDummyTranscript(self):
|
|
88 transcript = Transcript()
|
|
89 transcript.setChromosome("chr1")
|
|
90 transcript.setName("test1")
|
|
91 transcript.setEnd(200)
|
|
92 transcript.setStart(100)
|
|
93 transcript.setDirection("+")
|
|
94 return transcript
|
|
95
|
|
96 def _testOutputTag(self, value):
|
|
97 parser = GffParser(self._outputFileName, 0)
|
|
98 for transcript in parser.getIterator():
|
|
99 self.assertEquals(float(transcript.getTagValue(self._outputTag)), value)
|
|
100 parser.close()
|
|
101
|
|
102 def _writeTranscript(self, transcript):
|
|
103 f = open(self._inputFileName, "w")
|
|
104 f.write(transcript.printGff3("test"))
|
|
105 f.close()
|
|
106
|
|
107
|
|
108 if __name__ == "__main__":
|
|
109 unittest.main()
|