Mercurial > repos > thondeboer > neat_genreads
comparison py/cigar.py @ 0:6e75a84e9338 draft
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
author | thondeboer |
---|---|
date | Tue, 15 May 2018 02:39:53 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6e75a84e9338 |
---|---|
1 import re | |
2 | |
3 class CigarString: | |
4 def __init__(self, stringIn=None, listIn=None): | |
5 | |
6 if stringIn == None and listIn == None: | |
7 print '\nError: CigarString object not initialized.\n' | |
8 exit(1) | |
9 | |
10 self.cigarData = [] | |
11 | |
12 if stringIn != None: | |
13 self.joinCigar(j_stringIn=stringIn) | |
14 | |
15 if listIn != None: | |
16 self.joinCigar(j_listIn=listIn) | |
17 | |
18 | |
19 def stringToList(self, s): | |
20 | |
21 cigarDat = [] | |
22 letters = re.split(r"\d+",s)[1:] | |
23 numbers = [int(n) for n in re.findall(r"\d+",s)] | |
24 dReserve = 0 | |
25 for i in xrange(len(letters)): | |
26 if letters[i] == 'D': | |
27 dReserve = numbers[i] | |
28 if letters[i] == 'M' or letters[i] == 'I': | |
29 if dReserve: | |
30 cigarDat += ['D'*dReserve+letters[i]] + [letters[i]]*(int(numbers[i])-1) | |
31 else: | |
32 cigarDat += [letters[i]]*int(numbers[i]) | |
33 dReserve = 0 | |
34 return cigarDat | |
35 | |
36 | |
37 def listToString(self, l): | |
38 | |
39 symbols = '' | |
40 currentSym = l[0] | |
41 currentCount = 1 | |
42 if 'D' in currentSym: | |
43 currentSym = currentSym[-1] | |
44 for k in xrange(1,len(l)): | |
45 nextSym = l[k] | |
46 if len(nextSym) == 1 and nextSym == currentSym: | |
47 currentCount += 1 | |
48 else: | |
49 symbols += str(currentCount) + currentSym | |
50 if 'D' in nextSym: | |
51 symbols += str(nextSym.count('D')) + 'D' | |
52 currentSym = nextSym[-1] | |
53 else: | |
54 currentSym = nextSym | |
55 currentCount = 1 | |
56 symbols += str(currentCount) + currentSym | |
57 return symbols | |
58 | |
59 def getList(self): | |
60 | |
61 return self.cigarData | |
62 | |
63 | |
64 def getString(self): | |
65 | |
66 return self.listToString(self.cigarData) | |
67 | |
68 | |
69 def joinCigar(self, j_stringIn=None, j_listIn=None): | |
70 | |
71 if j_stringIn == None and j_listIn == None: | |
72 print '\nError: Invalid join operation in CigarString\n' | |
73 exit(1) | |
74 | |
75 if j_stringIn != None: | |
76 self.cigarData += self.stringToList(j_stringIn) | |
77 | |
78 if j_listIn != None: | |
79 self.cigarData += j_listIn | |
80 | |
81 | |
82 def insertCigarElement(self, pos, i_stringIn=None, i_listIn=None): | |
83 | |
84 if i_stringIn == None and i_listIn == None: | |
85 print '\nError: Invalid insertion operation in CigarString\n' | |
86 exit(1) | |
87 | |
88 if pos < 0 or pos >= len(self.cigarData): | |
89 print '\nError: Invalid insertion position in CigarString\n' | |
90 exit(1) | |
91 | |
92 if i_stringIn != None: | |
93 self.cigarData = self.cigarData[:pos] + self.stringToList(i_stringIn) + self.cigarData[pos:] | |
94 | |
95 if i_listIn != None: | |
96 self.cigarData = self.cigarData[:pos] + i_listIn + self.cigarData[pos:] | |
97 | |
98 | |
99 if __name__ == '__main__': | |
100 print 'testing CigarString class...' | |
101 | |
102 str1 = '50M10D7I23M' | |
103 str2 = '10I25M' | |
104 iPos = 20 | |
105 myCigar = CigarString(stringIn=str1) | |
106 myCigar.insertCigarElement(iPos,i_stringIn=str2) | |
107 print str1,'+',str2,'[inserted at position',str(iPos)+']','=',myCigar.getString() | |
108 |