Classify utils
classify_utils
This module provides functions for classifying biological sequences into variants.
Contents
- extract_features: Extract features from sequences and save to a DataFrame.
- process_dataframe: Process a DataFrame and optionally save data and model.
- sequence_classification: Perform sequence classification based on provided data and options.
- classify: Perform sequence classification pipeline.
- extract_features_to_predict: Extract features from sequences for prediction and return as a DataFrame.
- process_dataframe_predict: Process a DataFrame for prediction using MinMaxScaler.
- predict_data: Predict classes using a trained RandomForestClassifier model.
- predict: Predict sequence classes using a trained model.
Note
This module is designed to work with biological sequences and their classifications, allowing researchers to quickly classify and analyze sequence variants.
Todo
- Implement tests.
message = Messages()
module-attribute
Set the Message class for logging.
classify(word, step, save_path, dir_path, get_kmers=False, reference_path=None, dictonary='DNA', chunk_size=100)
Perform sequence classification pipeline.
This is the main function for the classification module. It performs sequence classification using the specified parameters and options. The function includes feature extraction, model training, and optional saving of data, model, and confusion matrix plot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
word
|
int
|
The length of each k-mer. |
required |
step
|
int
|
The step size for moving the sliding window. |
required |
save_path
|
str
|
The path to save the processed data and model files. |
required |
dir_path
|
str
|
The path to the directory containing sequence data. |
required |
get_kmers
|
bool
|
Whether to extract exclusive k-mers. Default is False. |
False
|
reference_path
|
str
|
The path to the reference sequence data file. Default is None. |
None
|
dictonary
|
str
|
The DNA dictionary for k-mer analysis. Default is 'DNA'. |
'DNA'
|
chunk_size
|
int
|
The chunk size for loading sequences. Default is 100. |
100
|
Returns:
| Type | Description |
|---|---|
|
Message class: A message confirming the classification pipeline has completed. |
Source code in python/gramep/classify_utils.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
extract_features(word, step, save_path, dir_path, dictonary, variants_kmers=None, chunk_size=100)
Extract features from sequences and save to a DataFrame.
This function extracts features from sequences located in the specified directory, and then saves the extracted features to a pandas DataFrame. The extracted features are based on the exclusive kmers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
word
|
int
|
The length of each k-mer. |
required |
step
|
int
|
The step size for moving the sliding window. |
required |
save_path
|
str
|
The path to save the extracted features DataFrame. |
required |
dir_path
|
str
|
The path to the directory containing sequence data. |
required |
dictonary
|
str
|
The DNA dictionary for k-mer analysis. |
required |
variants_kmers
|
None
|
The exclusive k-mers. Default is None. |
None
|
chunk_size
|
int
|
The chunk size for loading sequences. Default is 100. |
100
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: A pandas DataFrame containing the extracted features. |
Source code in python/gramep/classify_utils.py
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 | |
extract_features_to_predict(word, step, save_path, predict_seq_path, dictonary='DNA', chunk_size=100)
Extract features from sequences for prediction and return as a DataFrame.
This function extracts features from sequences located in the specified file for prediction purposes. The extracted features are based on the specified word length, step size, and DNA dictionary. The extracted features are returned as a pandas DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
word
|
int
|
The length of each k-mer. |
required |
step
|
int
|
The step size for moving the sliding window. |
required |
save_path
|
str
|
The path to save the extracted features for prediction. |
required |
predict_seq_path
|
str
|
The path to the file containing sequences for prediction. |
required |
dictonary
|
str
|
The DNA dictionary for k-mer analysis. Default is 'DNA'. |
'DNA'
|
chunk_size
|
int
|
The chunk size for loading sequences. Default is 100. |
100
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: A pandas DataFrame containing the extracted features for prediction. |
Source code in python/gramep/classify_utils.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
predict(word, step, save_path, predict_seq_path, dir_path, dictonary, load_ranges_path, load_model_path, chunk_size=100)
Predict sequence classes using a trained model.
This function performs sequence class prediction using the specified parameters and a trained model. It extracts features from sequences in the specified file for prediction and scales them using the MinMaxScaler object loaded from the given path. The prediction results are saved as a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
word
|
int
|
The length of each k-mer. |
required |
step
|
int
|
The step size for moving the sliding window. |
required |
save_path
|
str
|
The path to save the processed data and predictions. |
required |
predict_seq_path
|
str
|
The path to the file containing sequences for prediction. |
required |
dir_path
|
str
|
The path to the directory containing additional files. |
required |
dictonary
|
str
|
The DNA dictionary for k-mer analysis. |
required |
load_ranges_path
|
str
|
The path to load the MinMaxScaler object ranges. |
required |
load_model_path
|
str
|
The path to load the trained model. |
required |
chunk_size
|
int
|
The chunk size for loading sequences. Default is 100. |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
A message confirming the successful prediction and saving of results. |
Source code in python/gramep/classify_utils.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 | |
predict_data(data_frame, load_model_path)
Predict classes using a trained RandomForestClassifier model.
This function predicts classes for the provided data frame using the trained RandomForestClassifier model loaded from the specified path. It returns two pandas Series: one containing the predicted classes and the other containing the 'ID' values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_frame
|
DataFrame
|
The data frame containing features for prediction. |
required |
load_model_path
|
str
|
The path to load the trained RandomForestClassifier model. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Series, Series]
|
tuple[pd.Series, pd.Series]: A tuple containing a Series of predicted classes and a Series of 'ID' values. |
Source code in python/gramep/classify_utils.py
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | |
process_dataframe(data_frame, dir_path=None)
Process a DataFrame and optionally save data and model.
This function takes a DataFrame and performs processing on it. It optionally saves the processed data and a model based on the specified flags. It returns a tuple containing the processed DataFrame and a numpy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_frame
|
DataFrame
|
The DataFrame to be processed. |
required |
dir_path
|
str
|
The directory path for saving data and model. Default is None. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[DataFrame, ndarray]
|
tuple[pd.DataFrame, np.ndarray]: A tuple containing the processed DataFrame and a numpy array. |
Source code in python/gramep/classify_utils.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
process_dataframe_predict(data_frame, load_ranges_path)
Process a DataFrame for prediction using MinMaxScaler.
This function processes the provided DataFrame for prediction using the MinMaxScaler object loaded from the specified path. It scales the data and restores column names and 'ID' values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_frame
|
DataFrame
|
The DataFrame to be processed for prediction. |
required |
load_ranges_path
|
str
|
The path to load the MinMaxScaler object ranges. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: A processed DataFrame with scaled values and restored 'ID' column. |
Source code in python/gramep/classify_utils.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
sequence_classification(data_frame, name_class, dir_path)
Perform sequence classification based on provided data and options.
This function performs sequence classification using the provided data frame and class names. It allows for optional saving of a trained model and confusion matrix plot based on specified flags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_frame
|
DataFrame
|
The data frame containing sequence data and features. |
required |
name_class
|
ndarray
|
The array of class names corresponding to the data. |
required |
dir_path
|
str
|
The path to the directory for saving model and plot files. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in python/gramep/classify_utils.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |