MASSA-Algorithm 0.9.6__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
MASSA_Algorithm/MASSA.py CHANGED
@@ -9,127 +9,278 @@ from MASSA_Algorithm import MASSAcluster
9
9
  from MASSA_Algorithm import MASSAsplit
10
10
  from MASSA_Algorithm import MASSAmoloutput
11
11
 
12
- def returns_zero(total, test):
13
- # It evaluates if the distribution is not adequate = the iterated cluster has a percentage greater than 0.5% in the complete data set, but a percentage lower than 0.5% in the test set.
14
- definer = False
15
- for i in total.keys():
16
- if (total[i] > 0.5) and (test[i] <= 0.5):
17
- definer = True # Definer = True (Distribution was not done properly).
18
- return definer
19
-
20
- def main(): # Main subroutine, allows the program to run directly from the command line after installed via pip.
21
- ## Initializing from the command line:
22
- MASSAlogos.initial_print() # Print the program logo.
23
- FileInput, FileOutput, directoryFileOutput, extension_type, dendrogram_Xfont_size, barplot_Xfont_size, training_percent, test_percent, numberBioAct, BioActAsArgs, nPCS, svd_parameter, linkage_method, flag_dendrogram = MASSAargs.capture_args() # It captures command line arguments.
24
- print('Initializing, wait...\n')
25
12
 
13
+ def returns_zero(total, test, validation):
14
+ """
15
+ Description:
16
+ It evaluates whether the distribution is inadequate. That is,
17
+ when the iterated cluster has a percentage greater than 0.5%
18
+ in the complete dataset but less than 0.5% in the test set.
26
19
 
27
- ## Create log.txt file in directory:
28
- ArqLog = directoryFileOutput+'/log.txt'
29
- WriteLog = open(ArqLog, 'w')
30
-
31
-
32
- ## Initial file management:
33
- MASSAod.output_directory(directoryFileOutput) # It creates the output directories.
34
- mols = MASSAopen_files.read_molecules(FileInput, WriteLog) # Read molecules.
35
- sdf_property_names = MASSAopen_files.get_sdf_property_names(mols) # Extracting the property names from the ".sdf" input file.
36
- molsH = MASSAopen_files.hydrogen_add(mols) # Structure 3D management - It adds hydrogens keeping 3D coordenates.
37
-
38
-
39
- ## Extraction properties from ".sdf":
40
- names, dataframe = MASSAextraction.name_extraction(molsH) # It extracts the names of the molecules and creates a name:molecule dictionary and a dataframe.
41
- biological_activity = MASSAextraction.the_biological_handler(sdf_property_names, numberBioAct, BioActAsArgs) # It defines a list of what biological activities are being extracted.
42
- dataframe = MASSAextraction.list_activities(dataframe, biological_activity) # It adds the biological activities to the dataframe.
43
-
44
-
45
- ## Get fingeprint and other descriptors:
46
- dataframe = MASSAdescriptors.physicochemical_descriptors(dataframe) # Get physicochemical descriptors.
47
- dataframe = MASSAdescriptors.atompairs_fingerprint(dataframe) # Get AtomPairs fingerprint.
48
-
49
-
50
- ## Normalizes physicochemical and biological properties and creates matrices for the three domains:
51
- bio_matrix, PhCh_matrix, FP_matrix = MASSApreparation.normalizer_or_matrix(dataframe, biological_activity)
52
-
53
-
54
- ## PCA:
55
- bio_PCA = MASSApreparation.pca_maker(bio_matrix, nPCS, svd_parameter) # PCA for the biological domain.
56
- PhCh_PCA = MASSApreparation.pca_maker(PhCh_matrix, nPCS, svd_parameter) # PCA for the physicochemical domain.
57
- FP_PCA = MASSApreparation.pca_maker(FP_matrix, nPCS, svd_parameter) # PCA for the structural domain.
58
-
59
-
60
- ## First clustering (HCA):
61
- leaves_cluster_bio, bioHCA, linkage_bio, CutOff_bio = MASSAcluster.hca_clusters(bio_PCA, names, 'bio', directoryFileOutput, extension_type, linkage_method) # It performs HCA clustering without generating the dendrogram for the biological domain.
62
- leaves_cluster_phch, phchHCA, linkage_phch, CutOff_phch = MASSAcluster.hca_clusters(PhCh_PCA, names, 'PhCh', directoryFileOutput, extension_type, linkage_method) # It performs HCA clustering without generating the dendrogram for the physicochemical domain.
63
- leaves_cluster_fp, fpHCA, linkage_fp, CutOff_fp = MASSAcluster.hca_clusters(FP_PCA, names, 'FP', directoryFileOutput, extension_type, linkage_method) # It performs HCA clustering without generating the dendrogram for the structural domain.
64
-
65
- dataframe = MASSApreparation.organize_df_clusterization(dataframe, bioHCA, 'bio') # It adds the biological cluster identification to the spreadsheet.
66
- dataframe = MASSApreparation.organize_df_clusterization(dataframe, phchHCA, 'PhCh') # It adds the physicochemical cluster identification to the spreadsheet.
67
- dataframe = MASSApreparation.organize_df_clusterization(dataframe, fpHCA, 'FP') # It adds the structural cluster identification to the spreadsheet.
68
-
69
-
70
- ## Second clustering (Kmodes):
71
- matrix_for_kmodes = MASSApreparation.organize_for_kmodes(dataframe) # It creates a matrix with cluster identifications for each of the three domains, in order to prepare for Kmodes.
72
- allHCA = MASSAcluster.kmodes_clusters(matrix_for_kmodes, names) # It performs Kmodes clustering for the general domain.
73
- dataframe = MASSApreparation.organize_df_clusterization(dataframe, allHCA, 'all') # It adds the general cluster identification to the spreadsheet.
74
-
75
-
76
- ## Split into training, test:
77
- dataframe, test_molecules = MASSAsplit.split_train_test_sets(dataframe, training_percent, test_percent)
78
-
79
-
80
- ## Bar plot of frequencies (Calculates the percentages of molecules in each cluster for each dataset and generates a bar graph for each domain):
81
- bio_total, bio_training, bio_test = MASSAsplit.freq_clusters(dataframe, directoryFileOutput, extension_type, 'Cluster_Biological', barplot_Xfont_size) # Biological Bar Plot
82
- PhCh_total, PhCh_training, PhCh_test = MASSAsplit.freq_clusters(dataframe, directoryFileOutput, extension_type, 'Cluster_Physicochemical', barplot_Xfont_size) # Physicochemical Bar Plot
83
- FP_total, FP_training, FP_test = MASSAsplit.freq_clusters(dataframe, directoryFileOutput, extension_type, 'Cluster_Structural', barplot_Xfont_size) # Structural Bar Plot
84
- all_total, all_training, all_test = MASSAsplit.freq_clusters(dataframe, directoryFileOutput, extension_type, 'Cluster_General', barplot_Xfont_size) # General Bar Plot
85
-
86
-
87
- ## Verifying percentages:
88
- bio_ok = returns_zero(bio_total, bio_test) # For biological domain.
89
- PhCh_ok = returns_zero(PhCh_total, PhCh_test) # For physicochemical domain.
90
- FP_ok = returns_zero(FP_total, FP_test) # For structural domain.
91
- ok = [bio_ok, PhCh_ok, FP_ok]
92
- max_iters = 0
93
-
94
- # Redo the distribution in case of errors (up to 10 times):
95
- while (True in ok) and (max_iters < 10):
96
- ## Split into training, test:
97
- dataframe, test_molecules = MASSAsplit.split_train_test_sets(dataframe, training_percent, test_percent)
98
-
99
- ## Bar plot of frequencies (Calculates the percentages of molecules in each cluster for each dataset and generates a bar graph for each domain):
100
- bio_total, bio_training, bio_test = MASSAsplit.freq_clusters(dataframe, directoryFileOutput, extension_type, 'Cluster_Biological', barplot_Xfont_size) # Biological Bar Plot
101
- PhCh_total, PhCh_training, PhCh_test = MASSAsplit.freq_clusters(dataframe, directoryFileOutput, extension_type, 'Cluster_Physicochemical', barplot_Xfont_size) # Physicochemical Bar Plot
102
- FP_total, FP_training, FP_test = MASSAsplit.freq_clusters(dataframe, directoryFileOutput, extension_type, 'Cluster_Structural', barplot_Xfont_size) # Structural Bar Plot
103
- all_total, all_training, all_test = MASSAsplit.freq_clusters(dataframe, directoryFileOutput, extension_type, 'Cluster_General', barplot_Xfont_size) # General Bar Plot
104
-
105
- ## Verifying percentages:
106
- bio_ok = returns_zero(bio_total, bio_test) # For biological domain.
107
- PhCh_ok = returns_zero(PhCh_total, PhCh_test) # For physicochemical domain.
108
- FP_ok = returns_zero(FP_total, FP_test) # For structural domain.
109
- ok = [bio_ok, PhCh_ok, FP_ok]
110
- max_iters += 1
111
-
112
-
113
- ## Write distribution information to log file:
114
- bio_distr = 'Biological Distribution'
115
- MASSAsplit.log_of_distributions(bio_distr, bio_total, bio_training, bio_test, WriteLog)
116
- PhCh_distr = 'Physicochemical Distribution'
117
- MASSAsplit.log_of_distributions(PhCh_distr, PhCh_total, PhCh_training, PhCh_test, WriteLog)
118
- FP_distr = 'Structural (FP) Distribution'
119
- MASSAsplit.log_of_distributions(FP_distr, FP_total, FP_training, FP_test, WriteLog)
120
- all_distr = 'General Distribution'
121
- MASSAsplit.log_of_distributions(all_distr, all_total, all_training, all_test, WriteLog)
122
- WriteLog.close()
20
+ Args:
21
+ total (dict):
22
+ Percentage of molecules in each cluster for the whole dataset.
123
23
 
24
+ test (dict):
25
+ Percentage of molecules in each cluster for the test subset.
124
26
 
125
- ## Plot HCAs:
126
- if flag_dendrogram == True:
127
- print('\nGenerating dendrogram images. Please wait...')
128
- MASSAcluster.hca_plot(linkage_bio, names, leaves_cluster_bio, CutOff_bio, 'bio', directoryFileOutput, extension_type, dendrogram_Xfont_size, test_molecules) #Bio_Plot: Plot the HCA dendrogram
129
- MASSAcluster.hca_plot(linkage_phch, names, leaves_cluster_phch, CutOff_phch, 'PhCh', directoryFileOutput, extension_type, dendrogram_Xfont_size, test_molecules) #PhCh_Plot: Plot the HCA dendrogram
130
- MASSAcluster.hca_plot(linkage_fp, names, leaves_cluster_fp, CutOff_fp, 'FP', directoryFileOutput, extension_type, dendrogram_Xfont_size, test_molecules) #FP_Plot: Plot the HCA dendrogram
27
+ validation (dict):
28
+ Percentage of molecules in each cluster for the validation subset.
131
29
 
30
+ Returns:
31
+ definer (boolean): Returns True if the distribution is
32
+ inadequate, and False if it is adequate.
33
+ """
34
+ definer = False
35
+ for i in total.keys():
36
+ if (total[i] > 0.5) and (test[i] <= 0.5):
37
+ # Definer = True (Distribution was not done properly).
38
+ definer = True
39
+ elif validation != None:
40
+ if (total[i] > 0.5) and (validation[i] <= 0.5):
41
+ definer = True
42
+ return definer
132
43
 
133
- ## Output management:
134
- MASSAmoloutput.output_mols(dataframe, FileOutput) # It adds, for each molecule, the values of the calculated properties, the identifications of each cluster and which set the molecule belongs to.
135
- print('Completed')
44
+
45
+ def main():
46
+ """
47
+ Description:
48
+ Main subroutine, allows the program to run directly from
49
+ the command line when installed via pip.
50
+ """
51
+ # Initializing from the command line.
52
+ # Print the program logo:
53
+ MASSAlogos.initial_print()
54
+
55
+ # It captures command line arguments.
56
+ (file_input,
57
+ file_output,
58
+ directoryfile_output,
59
+ extension_type,
60
+ dendrogram_xfont_size,
61
+ barplot_xfont_size,
62
+ training_percent,
63
+ number_bioact,
64
+ bioactivities_as_args,
65
+ npcs,
66
+ svd_parameter,
67
+ linkage_method,
68
+ flag_dendrogram,
69
+ splitting_strategy,
70
+ drop_errors) = MASSAargs.capture_args()
71
+ print('Initializing, wait...\n')
72
+
73
+ # Create log.txt file in directory:
74
+ arqlog = directoryfile_output+'/log.txt'
75
+ writelog = open(arqlog, 'w')
76
+
77
+ # Initial file management:
78
+ # It creates the output directories.
79
+ MASSAod.output_directory(directoryfile_output)
80
+ # Read molecules.
81
+ mols = MASSAopen_files.read_molecules(file_input, writelog, drop_errors)
82
+ # Extracting the property names from the ".sdf" input file.
83
+ sdf_property_names = MASSAopen_files.get_sdf_property_names(mols)
84
+ # Structure 3D management - It adds hydrogens keeping 3D coordenates.
85
+ mols_h = MASSAopen_files.hydrogen_add(mols)
86
+
87
+ # Extraction properties from ".sdf":
88
+ # It extracts the names of the molecules and creates a name:molecule dictionary and a dataframe.
89
+ names, dataframe = MASSAextraction.name_extraction(mols_h)
90
+ # It defines a list of what biological activities are being extracted.
91
+ biological_activity = MASSAextraction.the_biological_handler(
92
+ sdf_property_names, number_bioact, bioactivities_as_args)
93
+ # It adds the biological activities to the dataframe.
94
+ dataframe = MASSAextraction.list_activities(dataframe, biological_activity)
95
+
96
+ # Get fingeprint and other descriptors:
97
+ # Get physicochemical descriptors.
98
+ dataframe = MASSAdescriptors.physicochemical_descriptors(dataframe)
99
+ # Get AtomPairs fingerprint.
100
+ dataframe = MASSAdescriptors.atompairs_fingerprint(dataframe)
101
+
102
+ # Normalizes physicochemical and biological properties.
103
+ # Creates matrices for the three domains (struc, phys-chem, bio):
104
+ bio_matrix, phch_matrix, fp_matrix = MASSApreparation.normalizer_or_matrix(
105
+ dataframe, biological_activity)
106
+
107
+ # PCA:
108
+ # PCA for the biological domain.
109
+ bio_pca = MASSApreparation.pca_maker(bio_matrix, npcs, svd_parameter)
110
+ # PCA for the physicochemical domain.
111
+ phch_pca = MASSApreparation.pca_maker(phch_matrix, npcs, svd_parameter)
112
+ # PCA for the structural domain.
113
+ fp_pca = MASSApreparation.pca_maker(fp_matrix, npcs, svd_parameter)
114
+
115
+ # First clustering (HCA):
116
+ # It performs HCA clustering without generating the dendrogram for the biological domain.
117
+ leaves_cluster_bio, bio_hca, linkage_bio, cutoff_bio = MASSAcluster.hca_clusters(
118
+ bio_pca, names, 'bio', directoryfile_output, extension_type, linkage_method)
119
+ # It performs HCA clustering without generating the dendrogram for the physicochemical domain.
120
+ leaves_cluster_phch, phch_hca, linkage_phch, cutoff_phch = MASSAcluster.hca_clusters(
121
+ phch_pca, names, 'PhCh', directoryfile_output, extension_type, linkage_method)
122
+ # It performs HCA clustering without generating the dendrogram for the structural domain.
123
+ leaves_cluster_fp, fp_hca, linkage_fp, cutoff_fp = MASSAcluster.hca_clusters(
124
+ fp_pca, names, 'FP', directoryfile_output, extension_type, linkage_method)
125
+
126
+ # It adds the biological cluster identification to the spreadsheet.
127
+ dataframe = MASSApreparation.organize_df_clusterization(
128
+ dataframe, bio_hca, 'bio')
129
+ # It adds the physicochemical cluster identification to the spreadsheet.
130
+ dataframe = MASSApreparation.organize_df_clusterization(
131
+ dataframe, phch_hca, 'PhCh')
132
+ # It adds the structural cluster identification to the spreadsheet.
133
+ dataframe = MASSApreparation.organize_df_clusterization(
134
+ dataframe, fp_hca, 'FP')
135
+
136
+ # Second clustering (Kmodes):
137
+ # It creates a matrix with cluster identifications for each of the three domains for Kmodes.
138
+ matrix_for_kmodes = MASSApreparation.organize_for_kmodes(dataframe)
139
+ # It performs Kmodes clustering for the general domain.
140
+ all_hca = MASSAcluster.kmodes_clusters(matrix_for_kmodes, names)
141
+ # It adds the general cluster identification to the spreadsheet.
142
+ dataframe = MASSApreparation.organize_df_clusterization(
143
+ dataframe, all_hca, 'all')
144
+
145
+ # Split into training, test:
146
+ dataframe, test_or_val_molecules = MASSAsplit.split_train_test_sets(
147
+ dataframe, training_percent, splitting_strategy)
148
+
149
+ # Bar plot of frequencies
150
+ # (Calculates the percentages of molecules in each cluster for each
151
+ # dataset and generates a bar graph for each domain):
152
+
153
+ # Biological Bar Plot
154
+ bio_total, bio_training, bio_test, bio_val = MASSAsplit.freq_clusters(
155
+ dataframe,
156
+ directoryfile_output,
157
+ extension_type,
158
+ 'Cluster_Biological',
159
+ barplot_xfont_size,
160
+ splitting_strategy)
161
+ # Physicochemical Bar Plot
162
+ phch_total, phch_training, phch_test, phch_val = MASSAsplit.freq_clusters(
163
+ dataframe,
164
+ directoryfile_output,
165
+ extension_type,
166
+ 'Cluster_Physicochemical',
167
+ barplot_xfont_size,
168
+ splitting_strategy)
169
+ # Structural Bar Plot
170
+ fp_total, fp_training, fp_test, fp_val = MASSAsplit.freq_clusters(
171
+ dataframe,
172
+ directoryfile_output,
173
+ extension_type,
174
+ 'Cluster_Structural',
175
+ barplot_xfont_size,
176
+ splitting_strategy)
177
+ # General Bar Plot
178
+ all_total, all_training, all_test, all_val = MASSAsplit.freq_clusters(
179
+ dataframe,
180
+ directoryfile_output,
181
+ extension_type,
182
+ 'Cluster_General',
183
+ barplot_xfont_size,
184
+ splitting_strategy)
185
+
186
+ # Verifying percentages:
187
+ # For biological domain:
188
+ bio_ok = returns_zero(bio_total, bio_test, bio_val)
189
+ # For physicochemical domain:
190
+ phch_ok = returns_zero(phch_total, phch_test, phch_val)
191
+ # For structural domain:
192
+ fp_ok = returns_zero(fp_total, fp_test, fp_val)
193
+ ok = [bio_ok, phch_ok, fp_ok]
194
+ max_iters = 0
195
+
196
+ # Redo the distribution in case of errors (up to 10 times):
197
+ while (True in ok) and (max_iters < 10):
198
+ # Split into training, test:
199
+ dataframe, test_or_val_molecules = MASSAsplit.split_train_test_sets(
200
+ dataframe, training_percent, splitting_strategy)
201
+
202
+ # Bar plot of frequencies (Calculates the percentages of molecules in each
203
+ # cluster for each dataset and generates a bar graph for each domain):
204
+
205
+ # Biological Bar Plot:
206
+ bio_total, bio_training, bio_test, bio_val = MASSAsplit.freq_clusters(
207
+ dataframe, directoryfile_output, extension_type,
208
+ 'Cluster_Biological', barplot_xfont_size, splitting_strategy
209
+ )
210
+ # Physicochemical Bar Plot
211
+ phch_total, phch_training, phch_test, phch_val = MASSAsplit.freq_clusters(
212
+ dataframe, directoryfile_output, extension_type,
213
+ 'Cluster_Physicochemical', barplot_xfont_size, splitting_strategy
214
+ )
215
+ # Structural Bar Plot
216
+ fp_total, fp_training, fp_test, fp_val = MASSAsplit.freq_clusters(
217
+ dataframe, directoryfile_output, extension_type,
218
+ 'Cluster_Structural', barplot_xfont_size, splitting_strategy
219
+ )
220
+ # General Bar Plot
221
+ all_total, all_training, all_test, all_val = MASSAsplit.freq_clusters(
222
+ dataframe, directoryfile_output, extension_type,
223
+ 'Cluster_General', barplot_xfont_size, splitting_strategy
224
+ )
225
+
226
+ # Verifying percentages:
227
+
228
+ # For biological domain:
229
+ bio_ok = returns_zero(bio_total, bio_test, bio_val)
230
+ # For physicochemical domain:
231
+ phch_ok = returns_zero(phch_total, phch_test, phch_val)
232
+ # For structural domain:
233
+ fp_ok = returns_zero(fp_total, fp_test, fp_val)
234
+ ok = [bio_ok, phch_ok, fp_ok]
235
+ max_iters += 1
236
+
237
+ # Write distribution information to log file:
238
+ bio_distr = 'Biological Distribution'
239
+ MASSAsplit.log_of_distributions(
240
+ bio_distr, bio_total, bio_training,
241
+ bio_test, bio_val, splitting_strategy,
242
+ writelog
243
+ )
244
+ phch_distr = 'Physicochemical Distribution'
245
+ MASSAsplit.log_of_distributions(
246
+ phch_distr, phch_total, phch_training,
247
+ phch_test, phch_val, splitting_strategy,
248
+ writelog
249
+ )
250
+ fp_distr = 'Structural (FP) Distribution'
251
+ MASSAsplit.log_of_distributions(
252
+ fp_distr, fp_total, fp_training,
253
+ fp_test, fp_val, splitting_strategy,
254
+ writelog
255
+ )
256
+ all_distr = 'General Distribution'
257
+ MASSAsplit.log_of_distributions(
258
+ all_distr, all_total, all_training,
259
+ all_test, all_val, splitting_strategy,
260
+ writelog
261
+ )
262
+ writelog.close()
263
+
264
+ # Plot HCAs:
265
+ if flag_dendrogram == True:
266
+ print('\nGenerating dendrogram images. Please wait...')
267
+ MASSAcluster.hca_plot(
268
+ linkage_bio, names, leaves_cluster_bio,
269
+ cutoff_bio, 'bio', directoryfile_output,
270
+ extension_type, dendrogram_xfont_size, test_or_val_molecules
271
+ ) # Bio_Plot: Plot the HCA dendrogram
272
+ MASSAcluster.hca_plot(
273
+ linkage_phch, names, leaves_cluster_phch,
274
+ cutoff_phch, 'PhCh', directoryfile_output,
275
+ extension_type, dendrogram_xfont_size, test_or_val_molecules
276
+ ) # phch_Plot: Plot the HCA dendrogram
277
+ MASSAcluster.hca_plot(
278
+ linkage_fp, names, leaves_cluster_fp,
279
+ cutoff_fp, 'FP', directoryfile_output,
280
+ extension_type, dendrogram_xfont_size, test_or_val_molecules
281
+ ) # fp_Plot: Plot the HCA dendrogram
282
+
283
+ # Output management:
284
+ # It adds, for each molecule, the values of the calculated properties, the identifications of each cluster and which set the molecule belongs to.
285
+ MASSAmoloutput.output_mols(dataframe, file_output)
286
+ print('Completed')