annotate gff2Togff3.py @ 20:04e57f9ef873 draft

planemo upload commit 6e3286c6569d531846474dcd6959378af0317ce3-dirty
author yating-l
date Fri, 12 Aug 2016 15:01:58 -0400
parents a7f57cf408e8
children 211c9b3a5c15
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
1
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
2 from Group import Group
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
3
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
4 class Convertor:
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
5 def __init__(self, input, output):
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
6 with open(input) as self.f:
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
7 self.li = [line.rstrip().split("\t") for line in self.f]
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
8 self.gff3 = open(output, "w")
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
9 self.gff3.write("##gff-version 3\n")
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
10
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
11 def convert(self):
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
12 index = 0
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
13 while index in range(0, len(self.li)):
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
14 index = self.groupAsgene(index)
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
15 self.gff3.close()
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
16
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
17
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
18 def groupAsgene(self, start = 0):
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
19 gene = self.li[start][8]
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
20 index = len(self.li)
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
21 for i in range(start+1, len(self.li)):
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
22 line = self.li[i]
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
23 if gene != line[8]:
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
24 index = i
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
25 break
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
26 if index >= len(self.li):
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
27 group = self.li[start:len(self.li)]
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
28 else:
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
29 group = self.li[start:index]
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
30 g = Group(group)
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
31 g.writer(self.gff3)
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
32 return index
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
33
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
34
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
35
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
36
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
37 if __name__ == "__main__":
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
38 file = Convertor("dbia3.gff", "test.txt")
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
39 file.convert()
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
40
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
41
a7f57cf408e8 planemo upload commit ff6c810cf2b46e59b45738700e68431743e4b83d
yating-l
parents:
diff changeset
42