# Genetic Algorithm Search for Features in Mass Spectrometry Data

This example shows how to use the **Global Optimization Toolbox** with the **Bioinformatics Toolbox™** to optimize the search for features to classify mass spectrometry (SELDI) data.

## Contents

## Introduction

Genetic algorithms optimize search results for problems with large data sets. You can use the MATLAB® genetic algorithm function to solve these problems in Bioinformatics. Genetic algorithms have been applied to phylogenetic tree building, gene expression and mass spectrometry data analysis, and many other areas of Bioinformatics that have large and computationally expensive problems. This example searches for optimal features (peaks) in mass spectrometry data. We will look for specific peaks in the data that distinguish cancer patients from control patients.

## Global Optimization Toolbox

First familiarize yourself with the Global Optimization Toolbox. The documentation describes how a genetic algorithm works and how to use it in MATLAB. To access the documentation, use the **doc** command.

```
doc ga
```

## Load Mass Spectrometry Data into MATLAB®

The data in this example is also used in the example Identifying Significant Features and Classifying Protein Profiles and is available from the FDA-NCI Clinical Proteomics Program Databank at http://home.ccr.cancer.gov/ncifdaproteomics/ppatterns.asp. It is a collection of samples from 121 ovarian cancer patients and 95 control patients. This example assumes that you downloaded, uncompressed, and preprocessed the raw mass-spectrometry data from the FDA-NCI Web site. You can recreate the preprocessed data file needed for this example, OvarianCancerQAQCdataset.mat, by running the script **msseqprocessing**, or by following the steps in the example Batch Processing of Spectra Using Sequential and Parallel Computing. There should be two variables that are loaded into MATLAB (**MZ** and **Y**). **MZ** is the mass/charge vector while **Y** is the intensity values for all 216 patients (control and cancer). To visualize this data see the example Identifying Significant Features and Classifying Protein Profiles.

```
load OvarianCancerQAQCdataset
whos
```

Name Size Bytes Class Attributes MZ 15000x1 120000 double Y 15000x216 25920000 double grp 216x1 26784 cell

Initialize the variables used in the example.

[numPoints, numSamples] = size(Y); % total number of samples and data points id = grp2idx(grp); % ground truth: Cancer=1, Control=2

## Create a Fitness Function for the Genetic Algorithm

A genetic algorithm requires an objective function, also known as the fitness function, which describes the phenomenon that we want to optimize. In this example, the genetic algorithm machinery tests small subsets of M/Z values using the fitness function and then determines which M/Z values get passed on to or removed from each subsequent generation. The fitness function **biogafit** is passed to the genetic algorithm solver using a function handle. In this example, **biogafit** maximizes the separability of two classes by using a linear combination of 1) the a-posteriori probability and 2) the empirical error rate of a linear classifier (**classify**). You can create your own fitness function to try different classifiers or alternative methods for assessing the performance of the classifiers.

```
type biogafit
```

function classPerformance = biogafit(thePopulation,Y,id) %BIOGAFIT The fitness function for BIOGAMSDEMO % % This function uses the classify function to measure how well mass % spectrometry data is grouped using certain masses. The input argument % thePopulation is a vector of row indices from the mass spectrometry % data Y. Classification performance is a linear combination of the error % rate and the posteriori probability of the classifier. % Copyright 2003-2013 The MathWorks, Inc. thePopulation = round(thePopulation); try [c,~,p] = classify(Y(thePopulation,:)',Y(thePopulation,:)',double(id),'linear'); cp = classperf(id,c); classPerformance = 100*cp.ErrorRate + 1 - mean(max(p,[],2)); catch % In case pooled covariance matrix is not positive definite we try a % naive-Bayes classifier: try [c,~,p] = classify(Y(thePopulation,:)',Y(thePopulation,:)',double(id),'diaglinear'); cp = classperf(id,c); classPerformance = 100*cp.ErrorRate + 1 - mean(max(p,[],2)); catch classPerformance = Inf; end end

## Create an Initial Population

Users can change how the optimization is performed by the genetic algorithm by creating custom functions for crossover, fitness scaling, mutation, selection, and population creation. In this example you will use the **biogacreate** function written for this example to create initial random data points from the mass spectrometry data. The function header requires specific input parameters as specified by the GA documentation. There is a default creation function in the toolbox for creating initial populations of data points.

```
type biogacreate
```

function pop = biogacreate(GenomeLength,~,options,Y,id) %BIOGACREATE Population creation function for MSGADEMO % % This function creates a population matrix with dimensions of % options.PopulationSize rows by the number of independent variables % (GenomeLength) columns. These values are integers that correspond to % randomly selected rows of the mass spectrometry data Y. Each row of the % population matrix is a random sample of row indices of the mass spec % data. % Note: This function's input arguments are required by the GA function. % See GA documentation for further detail. % Copyright 2005-2013 The MathWorks, Inc. pop = zeros(options.PopulationSize,GenomeLength); npop = numel(pop); ranked_features = rankfeatures(Y,id,'NumberOfIndices',npop,'NWeighting',.5); pop(:) = randsample(ranked_features,npop);

## Set Genetic Algorithm Options

The GA function uses an options structure to hold the algorithm parameters that it uses when performing a minimization with a genetic algorithm. The **gaoptimset** function will create this options structure. For the purposes of this example, the genetic algorithm will run only for 50 generations. However, you may set 'Generations' to a larger value.

options = gaoptimset('CreationFcn', {@biogacreate,Y,id},... 'PopulationSize',120,... 'Generations',50,... 'Display', 'iter')

options = struct with fields: PopulationType: [] PopInitRange: [] PopulationSize: 120 EliteCount: [] CrossoverFraction: [] ParetoFraction: [] MigrationDirection: [] MigrationInterval: [] MigrationFraction: [] Generations: 50 TimeLimit: [] FitnessLimit: [] StallGenLimit: [] StallTest: [] StallTimeLimit: [] TolFun: [] TolCon: [] InitialPopulation: [] InitialScores: [] NonlinConAlgorithm: [] InitialPenalty: [] PenaltyFactor: [] PlotInterval: [] CreationFcn: {@biogacreate [15000x216 double] [216x1 double]} FitnessScalingFcn: [] SelectionFcn: [] CrossoverFcn: [] MutationFcn: [] DistanceMeasureFcn: [] HybridFcn: [] Display: 'iter' PlotFcns: [] OutputFcns: [] Vectorized: [] UseParallel: []

## Run GA to Find 20 Discriminative Features

Use **ga** to start the genetic algorithm function. 100 groups of 20 datapoints each will evolve over 50 generations. Selection, crossover, and mutation events generate a new population in every generation.

% initialize the random generators to the same state used to generate the % published example rng('default') nVars = 12; % set the number of desired features FitnessFcn = {@biogafit,Y,id}; % set the fitness function feat = ga(FitnessFcn,nVars,options); % call the Genetic Algorithm feat = round(feat); Significant_Masses = MZ(feat) cp = classperf(classify(Y(feat,:)',Y(feat,:)',id),id); cp.CorrectRate

Best Mean Stall Generation Func-count f(x) f(x) Generations 1 240 2.827 8.928 0 2 360 2.827 8.718 1 3 480 0.9663 8.001 0 4 600 0.9516 7.249 0 5 720 0.9516 6.903 1 6 840 0.4926 6.804 0 7 960 0.4926 6.301 1 8 1080 0.02443 5.215 0 9 1200 0.02443 4.77 1 10 1320 0.02101 4.084 0 11 1440 0.02101 3.792 1 12 1560 0.01854 3.437 0 13 1680 0.01606 3.44 0 14 1800 0.01372 2.768 0 15 1920 0.01218 2.74 0 16 2040 0.01204 2.471 0 17 2160 0.01204 2.649 1 18 2280 0.01189 2.326 0 19 2400 0.01189 2.003 0 20 2520 0.0118 2.341 0 21 2640 0.01099 1.714 0 22 2760 0.01094 1.828 0 23 2880 0.01094 1.94 1 24 3000 0.01094 2.285 2 25 3120 0.009843 2.026 0 26 3240 0.009843 1.899 1 27 3360 0.009183 1.802 0 28 3480 0.007877 1.5 0 29 3600 0.007788 1.793 0 30 3720 0.007788 1.756 1 Best Mean Stall Generation Func-count f(x) f(x) Generations 31 3840 0.007091 1.719 0 32 3960 0.006982 1.598 0 33 4080 0.006982 1.269 1 34 4200 0.006732 1.279 0 35 4320 0.005008 1.229 0 36 4440 0.004325 1.179 0 37 4560 0.004325 1.534 1 38 4680 0.003982 1.15 0 39 4800 0.003982 0.9602 1 40 4920 0.003982 0.8547 2 41 5040 0.003891 0.9083 0 42 5160 0.003683 0.7409 0 43 5280 0.003683 0.516 1 44 5400 0.003364 0.5153 0 45 5520 0.003172 0.4218 0 46 5640 0.003172 0.3783 1 47 5760 0.002997 0.1883 0 48 5880 0.002675 0.1297 0 49 6000 0.002611 0.04382 0 50 6120 0.002519 0.007859 0 Optimization terminated: maximum number of generations exceeded. Significant_Masses = 1.0e+03 * 7.6861 7.9234 8.9834 8.6171 7.1808 7.3057 8.1132 8.5241 7.0527 7.7600 7.7442 7.7245 ans = 1

## Display the Features that are Discriminatory

To visualize which features have been selected by the genetic algorithm, the data is plotted with peak positions marked with red vertical lines.

xAxisLabel = 'Mass/Charge (M/Z)'; % x label for plots yAxisLabel = 'Relative Ion Intensity'; % y label for plots figure; hold on; hC = plot(MZ,Y(:,1:15) ,'b'); hN = plot(MZ,Y(:,141:155),'g'); hG = plot(MZ(feat(ceil((1:3*nVars )/3))), repmat([0 100 NaN],1,nVars),'r'); xlabel(xAxisLabel); ylabel(yAxisLabel); axis([1900 12000 -1 40]); legend([hN(1),hC(1),hG(1)],{'Control','Ovarian Cancer', 'Discriminative Features'}, ... 'Location', 'NorthWest'); title('MS Data with Discriminative Features found by Genetic Algorithm');

Observe the interesting peak around 8100 Da., which seems to be shifted to the right on healthy samples.

axis([8082 8128 -.5 4])