18
|
1 import unittest
|
|
2 import os, os.path
|
|
3 from optparse import OptionParser
|
|
4 from SMART.Java.Python.misc import Utils
|
|
5 from SMART.Java.Python.structure.Transcript import Transcript
|
|
6 from commons.core.writer.Gff3Writer import Gff3Writer
|
|
7 from commons.core.parsing.GffParser import GffParser
|
|
8 from SMART.Java.Python.RestrictFromCoverage import RestrictFromCoverage
|
|
9
|
|
10 REFERENCE = 0
|
|
11 QUERY = 1
|
|
12
|
|
13 class Test_F_Clusterize(unittest.TestCase):
|
|
14
|
|
15 def setUp(self):
|
|
16 self._queryFileName = "testQuery.gff3"
|
|
17 self._refFileName = "testRef.gff3"
|
|
18 self._outputFileName = "output.gff3"
|
|
19 self._parsers = {QUERY: Gff3Writer(self._queryFileName, 0), REFERENCE: Gff3Writer(self._refFileName, 0)}
|
|
20 self._writeQuery()
|
|
21
|
|
22 def tearDown(self):
|
|
23 for file in (self._queryFileName, self._refFileName, self._outputFileName):
|
|
24 if os.path.exists(file):
|
|
25 os.remove(file)
|
|
26
|
|
27 def _writeQuery(self):
|
|
28 self._addTranscript(QUERY, 1, 1000, 2000, "+")
|
|
29 self._parsers[QUERY].close()
|
|
30
|
|
31 def _writeReferences(self, values):
|
|
32 for value in values:
|
|
33 self._addTranscript(REFERENCE, value["cpt"], value["start"], value["end"], value["strand"])
|
|
34 self._parsers[REFERENCE].close()
|
|
35
|
|
36 def _addTranscript(self, type, cpt, start, end, strand):
|
|
37 t = Transcript()
|
|
38 t.setChromosome("chr1")
|
|
39 t.setName("test%d" % (cpt))
|
|
40 t.setStart(start)
|
|
41 t.setEnd(end)
|
|
42 t.setDirection(strand)
|
|
43 self._parsers[type].addTranscript(t)
|
|
44
|
|
45 def _checkTranscript(self, transcript, start, end, strand):
|
|
46 self.assertEquals(transcript.getStart(), start)
|
|
47 self.assertEquals(transcript.getEnd(), end)
|
|
48 self.assertEquals(transcript.getDirection(), strand)
|
|
49
|
|
50 def _startTool(self, minNucleotides = None, maxNucleotides = None, minPercent = None, maxPercent = None, minOverlap = None, maxOverlap = None, strands = False):
|
|
51 rfc = RestrictFromCoverage(0)
|
|
52 rfc.setInputFileName(self._queryFileName, "gff3", QUERY)
|
|
53 rfc.setInputFileName(self._refFileName, "gff3", REFERENCE)
|
|
54 rfc.setOutputFileName(self._outputFileName)
|
|
55 rfc.setNbNucleotides(minNucleotides, maxNucleotides)
|
|
56 rfc.setPercent(minPercent, maxPercent)
|
|
57 rfc.setOverlap(minOverlap, maxOverlap)
|
|
58 rfc.setStrands(strands)
|
|
59 rfc.run()
|
|
60 return GffParser(self._outputFileName, 0)
|
|
61
|
|
62 def test_simple(self):
|
|
63 self._writeReferences([{"cpt": 1, "start": 1000, "end": 2000, "strand": "+"}])
|
|
64 parser = self._startTool()
|
|
65 self.assertEquals(parser.getNbTranscripts(), 1)
|
|
66 for transcript in parser.getIterator():
|
|
67 self._checkTranscript(transcript, 1000, 2000, 1)
|
|
68
|
|
69 def test_nbOverlapsMin_pos(self):
|
|
70 self._writeReferences([{"cpt": 1, "start": 1000, "end": 2000, "strand": "+"}, {"cpt": 2, "start": 1000, "end": 2000, "strand": "+"}])
|
|
71 parser = self._startTool(1, None, None, None, 2)
|
|
72 self.assertEquals(parser.getNbTranscripts(), 1)
|
|
73 for transcript in parser.getIterator():
|
|
74 self._checkTranscript(transcript, 1000, 2000, 1)
|
|
75
|
|
76 def test_nbOverlapsMin_neg(self):
|
|
77 self._writeReferences([{"cpt": 1, "start": 1000, "end": 2000, "strand": "+"}])
|
|
78 parser = self._startTool(1, None, None, None, 2)
|
|
79 self.assertEquals(parser.getNbTranscripts(), 0)
|
|
80
|
|
81 def test_nbOverlapsMax_pos(self):
|
|
82 self._writeReferences([{"cpt": 1, "start": 1000, "end": 2000, "strand": "+"}])
|
|
83 parser = self._startTool(1, None, None, None, None, 1)
|
|
84 self.assertEquals(parser.getNbTranscripts(), 1)
|
|
85 for transcript in parser.getIterator():
|
|
86 self._checkTranscript(transcript, 1000, 2000, 1)
|
|
87
|
|
88 def test_nbOverlapsMax_neg(self):
|
|
89 self._writeReferences([{"cpt": 1, "start": 1000, "end": 2000, "strand": "+"}, {"cpt": 2, "start": 1000, "end": 2000, "strand": "+"}])
|
|
90 parser = self._startTool(1, None, None, None, None, 1)
|
|
91 self.assertEquals(parser.getNbTranscripts(), 0)
|
|
92
|
|
93 def test_nbNucleotidesMin_pos(self):
|
|
94 self._writeReferences([{"cpt": 1, "start": 1000, "end": 1100, "strand": "+"}])
|
|
95 parser = self._startTool(100, None, None, None, 1)
|
|
96 self.assertEquals(parser.getNbTranscripts(), 1)
|
|
97 for transcript in parser.getIterator():
|
|
98 self._checkTranscript(transcript, 1000, 2000, 1)
|
|
99
|
|
100 def test_nbNucleotidesMin_neg(self):
|
|
101 self._writeReferences([{"cpt": 1, "start": 1000, "end": 1100, "strand": "+"}])
|
|
102 parser = self._startTool(200, None, None, None, 1)
|
|
103 self.assertEquals(parser.getNbTranscripts(), 0)
|
|
104
|
|
105 def test_PercentMin_pos(self):
|
|
106 self._writeReferences([{"cpt": 1, "start": 1000, "end": 1500, "strand": "+"}])
|
|
107 parser = self._startTool(None, None, 50, None, 1)
|
|
108 self.assertEquals(parser.getNbTranscripts(), 1)
|
|
109 for transcript in parser.getIterator():
|
|
110 self._checkTranscript(transcript, 1000, 2000, 1)
|
|
111
|
|
112 def test_PercentMin_neg(self):
|
|
113 self._writeReferences([{"cpt": 1, "start": 1000, "end": 1500, "strand": "+"}])
|
|
114 parser = self._startTool(None, None, 100, None, 1)
|
|
115 self.assertEquals(parser.getNbTranscripts(), 0)
|
|
116
|
|
117 def test_NoStrand_neg(self):
|
|
118 self._writeReferences([{"cpt": 1, "start": 1000, "end": 1500, "strand": "-"}])
|
|
119 parser = self._startTool(1, None, None, None, 1)
|
|
120 self.assertEquals(parser.getNbTranscripts(), 1)
|
|
121
|
|
122 def test_strand_pos(self):
|
|
123 self._writeReferences([{"cpt": 1, "start": 1000, "end": 1500, "strand": "+"}])
|
|
124 parser = self._startTool(1, None, None, None, 1, None, True)
|
|
125 self.assertEquals(parser.getNbTranscripts(), 1)
|
|
126
|
|
127 def test_strand_neg(self):
|
|
128 self._writeReferences([{"cpt": 1, "start": 1000, "end": 1500, "strand": "-"}])
|
|
129 parser = self._startTool(1, None, None, None, 1, None, True)
|
|
130 self.assertEquals(parser.getNbTranscripts(), 0)
|
|
131
|
|
132 if __name__ == "__main__":
|
|
133 unittest.main()
|