Conditions | 58 |
Total Lines | 240 |
Code Lines | 171 |
Lines | 69 |
Ratio | 28.75 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like triplexibility.RNAmodel.get_rmsd_to() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python |
||
70 | def get_rmsd_to(self, other_rnamodel, way="", triple_mode=False, save=True, tseq=''): |
||
71 | """Calc rmsd P-atom based rmsd to other rna model |
||
72 | |||
73 | sugar now 10 atoms ;-) """ |
||
74 | sup = Bio.PDB.Superimposer() |
||
75 | if way in ['c1', 'backbone+sugar', 'sugar']: |
||
76 | if way == 'c1': |
||
77 | atomslist = ["C1'"]# ,"C2'","O4'"] #, "C2'"] |
||
78 | |||
79 | elif way == 'sugar': |
||
80 | atomslist = "C5',O5',C4',O4',C3',O3',C2',O2',C1'".split(',') |
||
81 | |||
82 | elif way == 'backbone+sugar': |
||
83 | atomslist = "P,OP1,OP2,C5',O5',C4',O4',C3',O3',C2',O2',C1'".split(',') |
||
84 | |||
85 | self.atoms_for_rmsd = [] |
||
86 | for a in self.atoms: |
||
87 | nt = a.get_parent().get_resname().strip() |
||
88 | if nt in ['G', 'A']: |
||
89 | atomslistx = atomslist + ['N9'] |
||
|
|||
90 | if nt in ['C', 'U']: |
||
91 | atomslistx = atomslist + ['N1'] |
||
92 | if a.name in atomslistx: |
||
93 | self.atoms_for_rmsd.append(a) |
||
94 | if args.debug: print('atoms_for_rmsd', len(self.atoms_for_rmsd)) |
||
95 | |||
96 | other_atoms_for_rmsd = [] |
||
97 | for a in other_rnamodel.atoms: |
||
98 | nt = a.get_parent().get_resname().strip() |
||
99 | if nt in ['G', 'A']: |
||
100 | atomslistx = atomslist + ['N9'] |
||
101 | if nt in ['C', 'U']: |
||
102 | atomslistx = atomslist + ['N1'] |
||
103 | if a.name in atomslistx: |
||
104 | other_atoms_for_rmsd.append(a) |
||
105 | |||
106 | if args.debug: print('other_atoms_for_rmsd', len(other_atoms_for_rmsd)) |
||
107 | |||
108 | elif way == 'c1+Nx': |
||
109 | self.atoms_for_rmsd = [] |
||
110 | for a in self.atoms: |
||
111 | nt = a.get_parent().get_resname().strip() # G |
||
112 | if nt in ['G', 'A']: |
||
113 | atomslist = ["C1'", 'N9'] # , 'N1'] |
||
114 | if nt in ['C', 'U']: |
||
115 | atomslist = ["C1'", 'N1'] # , 'N1'] |
||
116 | if a.name in atomslist: |
||
117 | self.atoms_for_rmsd.append(a) |
||
118 | |||
119 | if args.debug: print('atoms_for_rmsd', len(self.atoms_for_rmsd)) |
||
120 | |||
121 | other_atoms_for_rmsd = [] |
||
122 | for a in other_rnamodel.atoms: |
||
123 | nt = a.get_parent().get_resname().strip() # G |
||
124 | if nt in ['G', 'A']: |
||
125 | atomslist = ["C1'", 'N9'] # , 'N1'] |
||
126 | if nt in ['C', 'U']: |
||
127 | atomslist = ["C1'", 'N1'] # , 'N1'] |
||
128 | if a.name in atomslist: |
||
129 | other_atoms_for_rmsd.append(a) |
||
130 | if args.debug: print('other_atoms_for_rmsd', len(other_atoms_for_rmsd)) |
||
131 | |||
132 | else: |
||
133 | self.atoms_for_rmsd = self.atoms |
||
134 | other_atoms_for_rmsd = other_rnamodel.atoms |
||
135 | |||
136 | # calc rmsd # |
||
137 | if not tseq: |
||
138 | tseq = '' |
||
139 | rt = None |
||
140 | for a in self.atoms_for_rmsd: |
||
141 | r = a.get_parent() |
||
142 | if r != rt: |
||
143 | rt = r |
||
144 | tseq += r.get_resname().strip() |
||
145 | |||
146 | if triple_mode: |
||
147 | def chunks(lst, n): |
||
148 | """Yield successive n-sized chunks from lst. |
||
149 | https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks |
||
150 | """ |
||
151 | for i in range(0, len(lst), n): |
||
152 | yield lst[i:i + n] |
||
153 | |||
154 | rmsd_min = 10000 # ugly |
||
155 | import itertools |
||
156 | # ok, for different residues now it's a problem |
||
157 | per = list(itertools.permutations([0, 1, 2])) # [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)] |
||
158 | lst = list(chunks(other_atoms_for_rmsd, |
||
159 | int(len(other_atoms_for_rmsd)/3))) # for 1 atom, this will be 1 x 3 [3 residues] |
||
160 | # so so len is 3 atoms so / by 3 to get how many atoms per residue |
||
161 | print(lst) |
||
162 | |||
163 | sup_min = None |
||
164 | seq_min = 'not yet obtained, rmsd rejected!' |
||
165 | p_min = None |
||
166 | |||
167 | rms = -1 |
||
168 | |||
169 | for p in per: |
||
170 | patoms = [] |
||
171 | for i in p: # p=(1, 2, 3) |
||
172 | patoms.extend(lst[i]) |
||
173 | |||
174 | #print(self.atoms_for_rmsd) |
||
175 | ## print('patoms') |
||
176 | ## for a in patoms: |
||
177 | ## print(a, a.get_parent().get_id()) |
||
178 | ## print('self.atoms_for_rmsd') |
||
179 | ## for a in self.atoms_for_rmsd: |
||
180 | ## print(a, a.get_parent().get_id()) |
||
181 | #sup.set_atoms(patoms, self.atoms_for_rmsd) |
||
182 | |||
183 | rt = None |
||
184 | seq = '' |
||
185 | for a in patoms: |
||
186 | r = a.get_parent() |
||
187 | if r != rt: |
||
188 | rt = r |
||
189 | seq += r.get_resname().strip() |
||
190 | |||
191 | ic(tseq.lower(), seq.lower(), other_rnamodel.fpath) |
||
192 | # dont' even calc rmsd if the curr seq and tseq are not the same |
||
193 | if tseq.lower() == seq.lower(): # only if seq is the same |
||
194 | print(self.atoms_for_rmsd) |
||
195 | print(patoms) |
||
196 | print(len(self.atoms_for_rmsd), len(patoms)) |
||
197 | for a, b in zip(self.atoms_for_rmsd, patoms): |
||
198 | print(a, a.get_parent().id, a.get_parent().get_resname(), |
||
199 | b.get_parent().id, b.get_parent().get_resname()) |
||
200 | print(len(self.atoms_for_rmsd), len(patoms)) |
||
201 | try: |
||
202 | sup.set_atoms(self.atoms_for_rmsd, patoms) |
||
203 | except: |
||
204 | pass |
||
205 | rms = round(sup.rms, 2) |
||
206 | if rms < rmsd_min: |
||
207 | rmsd_min = rms |
||
208 | sup_min = copy.copy(sup) |
||
209 | suffix = seq |
||
210 | p_min = p |
||
211 | seq_min = seq |
||
212 | if args.debug: 'set new rmsd_min', rmsd_min |
||
213 | |||
214 | if args.debug: |
||
215 | print(p, '', [i + 1 for i in p], end=' ') |
||
216 | print(seq, 'seq_min: ' + seq_min, rms) |
||
217 | |||
218 | View Code Duplication | if p_min: # found target sequence |
|
219 | # what is this? ;-) |
||
220 | index = [0 ,0 ,0] |
||
221 | index[0] = p_min.index(0) |
||
222 | index[1] = p_min.index(1) |
||
223 | index[2] = p_min.index(2) |
||
224 | |||
225 | # ugly re-set 123 to crazy id ! + 100, so this will |
||
226 | # fill up 1 2 3 for the second for |
||
227 | rs = other_rnamodel.struc[0].get_residues() |
||
228 | for i, r in enumerate(rs): |
||
229 | r.id = (' ', index[i] + 253, ' ') # ugly, some random offset |
||
230 | |||
231 | for i, r in enumerate(other_rnamodel.struc[0].get_residues()): |
||
232 | r.id = (' ', index[i] + 1, ' ') |
||
233 | if args.debug: print('r', r) |
||
234 | |||
235 | io = Bio.PDB.PDBIO() |
||
236 | sup_min.apply(other_rnamodel.struc.get_atoms()) |
||
237 | # if args.debug: print(p_min, [i + 1 for i in p_min]) |
||
238 | io.set_structure(other_rnamodel.struc) |
||
239 | |||
240 | args.save_here = True |
||
241 | if args.save_here and save: |
||
242 | folder = os.path.basename(self.fpath.replace('.pdb', '_' + args.folder_prefix + '_aligned')) |
||
243 | # print(f) |
||
244 | try: |
||
245 | os.mkdir(folder) |
||
246 | except: |
||
247 | pass |
||
248 | fout = folder + os.sep + "{:1.2f}".format(rmsd_min) + '-' + os.path.basename(other_rnamodel.fpath)#.replace('.pdb', '-' + str(rms) + '.pdb')) |
||
249 | #_s' + suffix + '.pdb')) |
||
250 | else: |
||
251 | fout = other_rnamodel.fpath.replace('.pdb', '_aligned.pdb')#_s' + suffix + '.pdb') |
||
252 | |||
253 | if args.debug: print(fout) |
||
254 | |||
255 | if save: |
||
256 | io.save(fout) |
||
257 | # ugly set chain to A |
||
258 | set_chain_for_struc(fout, 'A', save_file_inplace=True) |
||
259 | # and now run this to sort into 1 2 3 |
||
260 | |||
261 | r = RNAStructure(fout) |
||
262 | remarks = r.get_remarks_text() |
||
263 | r1 = r.get_res_text('A', 1) |
||
264 | r2 = r.get_res_text('A', 2) |
||
265 | r3 = r.get_res_text('A', 3) |
||
266 | with open(fout, 'w') as f: |
||
267 | f.write(remarks) |
||
268 | f.write(r1) |
||
269 | f.write(r2) |
||
270 | f.write(r3) |
||
271 | r.reload() |
||
272 | r.get_rnapuzzle_ready() |
||
273 | if rmsd_min < 1: # !!!!!!!!!!!! ugly |
||
274 | r.write() |
||
275 | return str(rmsd_min)# + ',s' + seq_min + ',' + os.path.basename(fout) |
||
276 | else: |
||
277 | # check if number of the same atoms # |
||
278 | # if not the same then return -1 # |
||
279 | # print(len(self.atoms_for_rmsd), len(other_atoms_for_rmsd)) |
||
280 | if len(self.atoms_for_rmsd) != len(other_atoms_for_rmsd): |
||
281 | return -1 |
||
282 | sup.set_atoms(self.atoms_for_rmsd, other_atoms_for_rmsd) |
||
283 | rms = round(sup.rms, 2) |
||
284 | |||
285 | if save: |
||
286 | ## io = Bio.PDB.PDBIO() |
||
287 | ## sup.apply(self.struc.get_atoms()) |
||
288 | ## io.set_structure(self.struc) |
||
289 | ## io.save("aligned.pdb") |
||
290 | |||
291 | io = Bio.PDB.PDBIO() |
||
292 | sup.apply(other_rnamodel.struc.get_atoms()) |
||
293 | io.set_structure(other_rnamodel.struc) |
||
294 | |||
295 | args.save_here = True |
||
296 | View Code Duplication | if args.save_here and save: |
|
297 | f = os.path.basename(self.fpath.replace('.pdb', '_aligned')) |
||
298 | # print(f) |
||
299 | try: |
||
300 | os.mkdir(f) |
||
301 | except: |
||
302 | pass |
||
303 | # fout = f + os.sep + os.path.basename(other_rnamodel.fpath.replace('.pdb', '_aligned_s' + suffix + '.pdb')) |
||
304 | fout = f + os.sep + os.path.basename(other_rnamodel.fpath)#.replace('.pdb', '_aligned')) |
||
305 | else: |
||
306 | fout = other_rnamodel.fpath.replace('.pdb', '_aligned.pdb') |
||
307 | if save: |
||
308 | io.save(fout) |
||
309 | return rms |
||
310 | |||
412 |