Conditions | 30 |
Total Lines | 186 |
Code Lines | 113 |
Lines | 0 |
Ratio | 0 % |
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 build.rna_tools.tools.PyMOL4RNA.libs.show_contacts.show_contacts() 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/python |
||
33 | def show_contacts(selection='*', selection2='*', |
||
34 | result="contacts", |
||
35 | cutoff=3.6, |
||
36 | bigcutoff = 4.0, |
||
37 | labels=False, |
||
38 | SC_DEBUG = DEBUG): |
||
39 | """ |
||
40 | USAGE |
||
41 | |||
42 | show_contacts selection, selection2, [result=contacts],[cutoff=3.6],[bigcutoff=4.0] |
||
43 | |||
44 | Show various polar contacts, the good, the bad, and the ugly. |
||
45 | |||
46 | Edit MPB 6-26-14: The distances are heavy atom distances, so I upped the default cutoff to 4.0 |
||
47 | |||
48 | Returns: |
||
49 | True/False - if False, something went wrong |
||
50 | """ |
||
51 | if SC_DEBUG > 4: |
||
52 | print('Starting show_contacts') |
||
53 | print('selection = "' + selection + '"') |
||
54 | print('selection2 = "' + selection2 + '"') |
||
55 | |||
56 | result = cmd.get_legal_name(result) |
||
57 | |||
58 | #if the group of contacts already exist, delete them |
||
59 | cmd.delete(result) |
||
60 | |||
61 | # ensure only N and O atoms are in the selection |
||
62 | all_don_acc1 = selection + " and (donor or acceptor)" |
||
63 | all_don_acc2 = selection2 + " and (donor or acceptor)" |
||
64 | |||
65 | if SC_DEBUG > 4: |
||
66 | print('all_don_acc1 = "' + all_don_acc1 + '"') |
||
67 | print('all_don_acc2 = "' + all_don_acc2 + '"') |
||
68 | |||
69 | #if theses selections turn out not to have any atoms in them, pymol throws cryptic errors when calling the dist function like: |
||
70 | #'Selector-Error: Invalid selection name' |
||
71 | #So for each one, manually perform the selection and then pass the reference to the distance command and at the end, clean up the selections |
||
72 | #the return values are the count of the number of atoms |
||
73 | all1_sele_count = cmd.select('all_don_acc1_sele', all_don_acc1) |
||
74 | all2_sele_count = cmd.select('all_don_acc2_sele', all_don_acc2) |
||
75 | |||
76 | #print out some warnings |
||
77 | if DEBUG > 3: |
||
78 | if not all1_sele_count: |
||
79 | print('Warning: all_don_acc1 selection empty!') |
||
80 | if not all2_sele_count: |
||
81 | print('Warning: all_don_acc2 selection empty!') |
||
82 | |||
83 | ######################################## |
||
84 | allres = result + "_all" |
||
85 | if all1_sele_count and all2_sele_count: |
||
86 | #print(allres) |
||
87 | #print(cmd.get_distance(allres, 'all_don_acc1_sele', 'all_don_acc2_sele', bigcutoff, mode = 0)) |
||
88 | any = cmd.distance(allres, 'all_don_acc1_sele', 'all_don_acc2_sele', bigcutoff, mode = 0) |
||
89 | # if any is 0 it seems that there is no distance! |
||
90 | if any: |
||
91 | cmd.set("dash_radius", "0.05", allres) |
||
92 | if not labels: |
||
93 | cmd.hide("labels", allres) |
||
94 | else: |
||
95 | # just do nothing and clena up |
||
96 | print('no contacts') |
||
97 | cmd.delete('all_don_acc1_sele') |
||
98 | cmd.delete('all_don_acc2_sele') |
||
99 | cmd.delete(result + "_all") |
||
100 | return None |
||
101 | ######################################## |
||
102 | # compute good polar interactions according to pymol |
||
103 | polres = result + "_polar" |
||
104 | if all1_sele_count and all2_sele_count: |
||
105 | cmd.distance(polres, 'all_don_acc1_sele', 'all_don_acc2_sele', cutoff, mode = 2) #hopefully this checks angles? Yes |
||
106 | #cmd.set("dash_color", "marine", allres) |
||
107 | #cmd.set('dash_gap', '0') |
||
108 | cmd.set("dash_radius","0.2", polres) #"0.126" |
||
109 | #cmd.set("dash_color", "marine", allres) |
||
110 | if not labels: |
||
111 | cmd.hide("labels", polres) |
||
112 | |||
113 | ######################################## |
||
114 | # When running distance in mode=2, the cutoff parameter is ignored if set higher then the default of 3.6 |
||
115 | # so set it to the passed in cutoff and change it back when you are done. |
||
116 | old_h_bond_cutoff_center = cmd.get('h_bond_cutoff_center') # ideal geometry |
||
117 | old_h_bond_cutoff_edge = cmd.get('h_bond_cutoff_edge') # minimally acceptable geometry |
||
118 | cmd.set('h_bond_cutoff_center', bigcutoff) |
||
119 | cmd.set('h_bond_cutoff_edge', bigcutoff) |
||
120 | |||
121 | # compute possibly suboptimal polar interactions using the user specified distance |
||
122 | pol_ok_res = result + "_polar_ok" |
||
123 | if all1_sele_count and all2_sele_count: |
||
124 | cmd.distance(pol_ok_res, 'all_don_acc1_sele', 'all_don_acc2_sele', bigcutoff, mode = 2) |
||
125 | cmd.set("dash_radius", "0.06", pol_ok_res) |
||
126 | if not labels: |
||
127 | cmd.hide("labels", pol_ok_res) |
||
128 | |||
129 | #now reset the h_bond cutoffs |
||
130 | cmd.set('h_bond_cutoff_center', old_h_bond_cutoff_center) |
||
131 | cmd.set('h_bond_cutoff_edge', old_h_bond_cutoff_edge) |
||
132 | |||
133 | |||
134 | ######################################## |
||
135 | |||
136 | onlyacceptors1 = selection + " and (acceptor and !donor)" |
||
137 | onlyacceptors2 = selection2 + " and (acceptor and !donor)" |
||
138 | onlydonors1 = selection + " and (!acceptor and donor)" |
||
139 | onlydonors2 = selection2 + " and (!acceptor and donor)" |
||
140 | |||
141 | #perform the selections |
||
142 | onlyacceptors1_sele_count = cmd.select('onlyacceptors1_sele', onlyacceptors1) |
||
143 | onlyacceptors2_sele_count = cmd.select('onlyacceptors2_sele', onlyacceptors2) |
||
144 | onlydonors1_sele_count = cmd.select('onlydonors1_sele', onlydonors1) |
||
145 | onlydonors2_sele_count = cmd.select('onlydonors2_sele', onlydonors2) |
||
146 | |||
147 | #print out some warnings |
||
148 | if SC_DEBUG > 2: |
||
149 | if not onlyacceptors1_sele_count: |
||
150 | print('Warning: onlyacceptors1 selection empty!') |
||
151 | if not onlyacceptors2_sele_count: |
||
152 | print('Warning: onlyacceptors2 selection empty!') |
||
153 | if not onlydonors1_sele_count: |
||
154 | print('Warning: onlydonors1 selection empty!') |
||
155 | if not onlydonors2_sele_count: |
||
156 | print('Warning: onlydonors2 selection empty!') |
||
157 | |||
158 | # acceptors acceptors |
||
159 | accres = result+"_aa" |
||
160 | if onlyacceptors1_sele_count and onlyacceptors2_sele_count: |
||
161 | aa_dist_out = cmd.distance(accres, 'onlyacceptors1_sele', 'onlyacceptors2_sele', cutoff, 0) |
||
162 | if aa_dist_out < 0: |
||
163 | print('\n\nCaught a pymol selection error in acceptor-acceptor selection of show_contacts') |
||
164 | print('accres:', accres) |
||
165 | print('onlyacceptors1', onlyacceptors1) |
||
166 | print('onlyacceptors2', onlyacceptors2) |
||
167 | return False |
||
168 | cmd.set("dash_color","red",accres) |
||
169 | cmd.set("dash_radius","0.125",accres) |
||
170 | if not labels: |
||
171 | cmd.hide("labels", accres) |
||
172 | |||
173 | ######################################## |
||
174 | # donors donors |
||
175 | donres = result+"_dd" |
||
176 | if onlydonors1_sele_count and onlydonors2_sele_count: |
||
177 | dd_dist_out = cmd.distance(donres, 'onlydonors1_sele', 'onlydonors2_sele', cutoff, 0) |
||
178 | |||
179 | #try to catch the error state |
||
180 | if dd_dist_out < 0: |
||
181 | print('\n\nCaught a pymol selection error in dd selection of show_contacts') |
||
182 | print('donres:', donres) |
||
183 | print('onlydonors1', onlydonors1) |
||
184 | print('onlydonors2', onlydonors2) |
||
185 | print("cmd.distance('" + donres + "', '" + onlydonors1 + "', '" + onlydonors2 + "', " + str(cutoff) + ", 0)") |
||
186 | return False |
||
187 | |||
188 | cmd.set("dash_color","red",donres) |
||
189 | cmd.set("dash_radius","0.125",donres) |
||
190 | if not labels: |
||
191 | cmd.hide("labels", donres) |
||
192 | |||
193 | ########################################################## |
||
194 | ##### find the buried unpaired atoms of the receptor ##### |
||
195 | ########################################################## |
||
196 | |||
197 | #initialize the variable for when CALC_SASA is False |
||
198 | unpaired_atoms = '' |
||
199 | |||
200 | ## Group |
||
201 | print(allres) # contacts_all |
||
202 | cmd.group(result,"%s %s %s %s %s %s" % (polres, allres, accres, donres, pol_ok_res, unpaired_atoms)) |
||
203 | |||
204 | ## Clean up the selection objects |
||
205 | #if the show_contacts debug level is high enough, don't delete them. |
||
206 | if SC_DEBUG < 5: |
||
207 | cmd.delete('all_don_acc1_sele') |
||
208 | cmd.delete('all_don_acc2_sele') |
||
209 | cmd.delete('onlyacceptors1_sele') |
||
210 | cmd.delete('onlyacceptors2_sele') |
||
211 | cmd.delete('onlydonors1_sele') |
||
212 | cmd.delete('onlydonors2_sele') |
||
213 | |||
214 | cmd.disable('contacts_all') |
||
215 | cmd.disable('contacts_polar_ok') |
||
216 | cmd.disable('contacts_aa') |
||
217 | cmd.disable('contacts_dd') |
||
218 | return True |
||
219 | |||
385 |