-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbucketed_deep.py
136 lines (111 loc) · 6.58 KB
/
bucketed_deep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
" "
" This file is part of the 20n/act project. "
" 20n/act enables DNA prediction for synthetic biology/bioengineering. "
" Copyright (C) 2017 20n Labs, Inc. "
" "
" Please direct all queries to [email protected]. "
" "
" This program is free software: you can redistribute it and/or modify "
" it under the terms of the GNU General Public License as published by "
" the Free Software Foundation, either version 3 of the License, or "
" (at your option) any later version. "
" "
" This program is distributed in the hope that it will be useful, "
" but WITHOUT ANY WARRANTY; without even the implied warranty of "
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the "
" GNU General Public License for more details. "
" "
" You should have received a copy of the GNU General Public License "
" along with this program. If not, see <http://www.gnu.org/licenses/>. "
" "
"""
from __future__ import absolute_import, division, print_function
import argparse
import os
import numpy as np
from bucketed_peaks.modules.lcms_autoencoder import LcmsAutoencoder
from bucketed_peaks.modules.preprocessing.LcmsPreprocessing import ScanWindower
from bucketed_peaks.modules.utility import magic, utility_functions
"""
This is the primary control file. Run new Deep processings from here.
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("lcmsDirectory", help="The LCMS scan directory.")
parser.add_argument("lcmsScanName", help="The name of the LCMS scan file. Currently supports '.nc' format.")
parser.add_argument("outputDirectory", help="Where to save all intermediate and final files.")
parser.add_argument("--previousModelLocation", help="Location of a previously created model.")
parser.add_argument("-w", "--lcmsWindowSize",
type=int,
help="The size of the window to include over time. Each unit is about 0.2 seconds here.",
default=magic.window_size)
parser.add_argument("-e", "--encodingSize",
type=int,
help="The size of the NN's encoding layer. "
"This is the compressed plot's representation and how many neurons it has to move around.",
default=magic.encoding_size)
parser.add_argument("-z", "--mzSplit",
type=float,
help="The level of granularity when dividing LCMS plots up.",
default=magic.mz_split)
parser.add_argument("-c", "--clusterNumber",
type=int,
help="Number of kMeans clusters to cluster on.",
default=magic.cluster_number)
parser.add_argument("-d", "--outputDescriptor", help="The label output files should be labeled with",
default="differential_expression")
parser.add_argument("-n", "--mzMin", type=float, help="The lowest M/Z value allowed.", default=magic.mz_min)
parser.add_argument("-x", "--mzMax", type=float, help="The highest M/Z value allowed.", default=magic.mz_max)
args = parser.parse_args()
lcms_directory = args.lcmsDirectory
lcms_scan_name = args.lcmsScanName
output_directory = args.outputDirectory
model_location = args.previousModelLocation
output_descriptor = args.outputDescriptor
block_size = args.lcmsWindowSize
encoding_size = args.encodingSize
mz_division = args.mzSplit
mz_min = args.mzMin
mz_max = args.mzMax
number_clusters = args.clusterNumber
summary_dict = {}
summary_dict.update(vars(args))
if model_location and os.path.exists(model_location):
autoencoder = utility_functions.load_previous_model(model_location, output_directory)
elif model_location and not os.path.exists(model_location):
raise \
RuntimeError("Supplied a model location path of {}, but that model does not exist.".format(model_location))
else:
autoencoder = LcmsAutoencoder(output_directory, block_size, encoding_size,
number_clusters, mz_division, mz_min, mz_max, debug=False)
print("Processing LCMS scan.")
row_matrix = autoencoder.process_lcms_scan(lcms_directory, lcms_scan_name)
print("Preparing matrix for autoencoder application.")
named_windows = ScanWindower.prepare_matrix_for_encoding(row_matrix.get_array(), row_matrix, row_matrix,
magic.threshold, block_size,
magic.local_area_band_halfwidth)
training_data = np.vstack([w.window for w in named_windows])
summary_dict["number_of_valid_windows"] = len(named_windows)
if not model_location or not os.path.exists(model_location):
print("Training autoencoder model.")
autoencoder.train(training_data)
encoded_samples = autoencoder.predict(training_data)
if not model_location or not os.path.exists(model_location):
print("Using clustering to fit autoencoder ")
autoencoder.fit_clusters(encoded_samples)
# This currently also does the writing
print("Creating file containing clusters for each peak.")
autoencoder.predict_clusters(encoded_samples, named_windows, output_descriptor,
[row_matrix], drop_rt=0)
if not model_location or not os.path.exists(model_location):
try:
autoencoder.visualize(output_descriptor, lower_axis=-1)
except RuntimeError as e:
# We expect this to occur if the Display variable is not valid.
print("Unable to create visualizations due to runtime error of \n:{}".format(e.message))
pass
if not model_location:
summary_dict["model_location"] = utility_functions.save_model(output_directory, output_descriptor, autoencoder)
# Write the summary information out for later analysis of what occurred.
utility_functions.output_analysis_summary(output_directory, output_descriptor, summary_dict)