Conditions | 29 |
Total Lines | 169 |
Code Lines | 104 |
Lines | 69 |
Ratio | 40.83 % |
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 triplexibility2.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 |
||
82 | def get_rmsd_to(self, other_rnamodel, way="", triple_mode=False, save=True, tseq=''): |
||
83 | """Calc rmsd P-atom based rmsd to other rna model |
||
84 | |||
85 | sugar now 10 atoms ;-) """ |
||
86 | sup = Bio.PDB.Superimposer() |
||
87 | |||
88 | other_atoms_for_rmsd = other_rnamodel.atoms |
||
89 | |||
90 | if triple_mode: |
||
91 | def chunks(lst, n): |
||
92 | """Yield successive n-sized chunks from lst. |
||
93 | https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks |
||
94 | """ |
||
95 | for i in range(0, len(lst), n): |
||
96 | yield lst[i:i + n] |
||
97 | |||
98 | rmsd_min = 10000 # ugly |
||
99 | import itertools |
||
100 | # ok, for different residues now it's a problem |
||
101 | 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)] |
||
102 | #lst = list(chunks(other_atoms_for_rmsd, |
||
103 | # int(len(other_atoms_for_rmsd)/3))) # for 1 atom, this will be 1 x 3 [3 residues] |
||
104 | # # so so len is 3 atoms so / by 3 to get how many atoms per residue |
||
105 | |||
106 | self.atoms_for_rmsd = [] |
||
107 | for i in self.atoms: |
||
108 | self.atoms_for_rmsd.extend(i) |
||
109 | |||
110 | sup_min = None |
||
111 | seq_min = 'not yet obtained, rmsd rejected!' |
||
112 | p_min = None |
||
113 | |||
114 | rms = -1 |
||
115 | |||
116 | for p in per: # for each combo make a list of atoms |
||
117 | |||
118 | patoms = [] |
||
119 | for i in p: # p=(1, 2, 3) |
||
120 | #patoms.extend(lst[i]) |
||
121 | # list is not needed, just other_atoms !!!! it's a list of [1, 2, 3] residues |
||
122 | patoms.extend(other_atoms_for_rmsd[i]) |
||
123 | |||
124 | #print(self.atoms_for_rmsd) |
||
125 | ## print('patoms') |
||
126 | ## for a in patoms: |
||
127 | ## print(a, a.get_parent().get_id()) |
||
128 | ## print('self.atoms_for_rmsd') |
||
129 | ## for a in self.atoms_for_rmsd: |
||
130 | ## print(a, a.get_parent().get_id()) |
||
131 | #sup.set_atoms(patoms, self.atoms_for_rmsd) |
||
132 | |||
133 | rt = None |
||
134 | seq = '' |
||
135 | for a in patoms: |
||
136 | r = a.get_parent() |
||
137 | if r != rt: |
||
138 | rt = r |
||
139 | seq += r.get_resname().strip() |
||
140 | |||
141 | ic(self.tseq.lower(), seq.lower(), other_rnamodel.fpath) |
||
142 | # dont' even calc rmsd if the curr seq and tseq are not the same |
||
143 | if self.tseq.lower() == seq.lower(): # only if seq is the same |
||
144 | |||
145 | sup.set_atoms(self.atoms_for_rmsd, patoms) |
||
146 | rms = round(sup.rms, 2) |
||
147 | if rms < rmsd_min: |
||
148 | rmsd_min = rms |
||
149 | sup_min = copy.copy(sup) |
||
150 | suffix = seq |
||
151 | p_min = p |
||
152 | seq_min = seq |
||
153 | if args.debug: 'set new rmsd_min', rmsd_min |
||
|
|||
154 | |||
155 | if args.debug: |
||
156 | print(p, '', [i + 1 for i in p], end=' ') |
||
157 | print(seq, 'seq_min: ' + seq_min, rms) |
||
158 | |||
159 | View Code Duplication | if p_min: # found target sequence |
|
160 | # what is this? ;-) |
||
161 | index = [0 ,0 ,0] |
||
162 | index[0] = p_min.index(0) |
||
163 | index[1] = p_min.index(1) |
||
164 | index[2] = p_min.index(2) |
||
165 | |||
166 | # ugly re-set 123 to crazy id ! + 100, so this will |
||
167 | # fill up 1 2 3 for the second for |
||
168 | rs = other_rnamodel.struc[0].get_residues() |
||
169 | for i, r in enumerate(rs): |
||
170 | r.id = (' ', index[i] + 253, ' ') # ugly, some random offset |
||
171 | |||
172 | for i, r in enumerate(other_rnamodel.struc[0].get_residues()): |
||
173 | r.id = (' ', index[i] + 1, ' ') |
||
174 | if args.debug: print('r', r) |
||
175 | |||
176 | io = Bio.PDB.PDBIO() |
||
177 | sup_min.apply(other_rnamodel.struc.get_atoms()) |
||
178 | # if args.debug: print(p_min, [i + 1 for i in p_min]) |
||
179 | io.set_structure(other_rnamodel.struc) |
||
180 | |||
181 | args.save_here = True |
||
182 | if args.save_here and save: |
||
183 | folder = os.path.basename(self.fpath.replace('.pdb', '_' + args.folder_prefix + '_aligned')) |
||
184 | # print(f) |
||
185 | try: |
||
186 | os.mkdir(folder) |
||
187 | except: |
||
188 | pass |
||
189 | fout = folder + os.sep + "{:1.2f}".format(rmsd_min) + '-' + os.path.basename(other_rnamodel.fpath)#.replace('.pdb', '-' + str(rms) + '.pdb')) |
||
190 | #_s' + suffix + '.pdb')) |
||
191 | else: |
||
192 | fout = other_rnamodel.fpath.replace('.pdb', '_aligned.pdb')#_s' + suffix + '.pdb') |
||
193 | |||
194 | if args.debug: print(fout) |
||
195 | |||
196 | if save: |
||
197 | io.save(fout) |
||
198 | # ugly set chain to A |
||
199 | set_chain_for_struc(fout, 'A', save_file_inplace=True) |
||
200 | # and now run this to sort into 1 2 3 |
||
201 | |||
202 | r = RNAStructure(fout) |
||
203 | remarks = r.get_remarks_text() |
||
204 | r1 = r.get_res_text('A', 1) |
||
205 | r2 = r.get_res_text('A', 2) |
||
206 | r3 = r.get_res_text('A', 3) |
||
207 | with open(fout, 'w') as f: |
||
208 | f.write(remarks) |
||
209 | f.write(r1) |
||
210 | f.write(r2) |
||
211 | f.write(r3) |
||
212 | r.reload() |
||
213 | r.get_rnapuzzle_ready() |
||
214 | if rmsd_min < 1: # !!!!!!!!!!!! ugly |
||
215 | r.write() |
||
216 | return str(rmsd_min)# + ',s' + seq_min + ',' + os.path.basename(fout) |
||
217 | else: |
||
218 | # check if number of the same atoms # |
||
219 | # if not the same then return -1 # |
||
220 | # print(len(self.atoms_for_rmsd), len(other_atoms_for_rmsd)) |
||
221 | if len(self.atoms_for_rmsd) != len(other_atoms_for_rmsd): |
||
222 | return -1 |
||
223 | sup.set_atoms(self.atoms_for_rmsd, other_atoms_for_rmsd) |
||
224 | rms = round(sup.rms, 2) |
||
225 | |||
226 | if save: |
||
227 | ## io = Bio.PDB.PDBIO() |
||
228 | ## sup.apply(self.struc.get_atoms()) |
||
229 | ## io.set_structure(self.struc) |
||
230 | ## io.save("aligned.pdb") |
||
231 | |||
232 | io = Bio.PDB.PDBIO() |
||
233 | sup.apply(other_rnamodel.struc.get_atoms()) |
||
234 | io.set_structure(other_rnamodel.struc) |
||
235 | |||
236 | args.save_here = True |
||
237 | View Code Duplication | if args.save_here and save: |
|
238 | f = os.path.basename(self.fpath.replace('.pdb', '_aligned')) |
||
239 | # print(f) |
||
240 | try: |
||
241 | os.mkdir(f) |
||
242 | except: |
||
243 | pass |
||
244 | # fout = f + os.sep + os.path.basename(other_rnamodel.fpath.replace('.pdb', '_aligned_s' + suffix + '.pdb')) |
||
245 | fout = f + os.sep + os.path.basename(other_rnamodel.fpath)#.replace('.pdb', '_aligned')) |
||
246 | else: |
||
247 | fout = other_rnamodel.fpath.replace('.pdb', '_aligned.pdb') |
||
248 | if save: |
||
249 | io.save(fout) |
||
250 | return rms |
||
251 | |||
355 |