Covid-19 has driven up the interest of the bioinformatics, the data science of collecting and analyzing complex biological data such as genetic codes and sequences, a combination of mathematics and biology.
Context
ClinVar is a public resource containing annotations about human genetic variants. Detail can be found on the National Institute of Health website.
These variants are usually manually classified by clinical laboratories on a categorical spectrum ranging from benign, likely benign, uncertain significance, likely pathogenic, and pathogenic. Variants that have conflicting classifications (from laboratory to laboratory) can cause confusion when clinicians or researchers try to interpret whether the variant has an impact on the disease of a given patient.
Objective
The objective is to predict whether a ClinVar variant will have conflicting classifications. This is presented as a binary classification problem, each record in the dataset is a genetic variant.
Conflicting classifications are when two of any of the following three categories are present for one variant, two submissions of one category are not considered conflicting.
· Likely Benign or Benign
· VUS (Variance of Uncertain Significance. A variation in a genetic sequence for which the association with disease risk is unclear)
· Likely Pathogenic or Pathogenic
Class Label
Conflicting classification has been assigned to the CLASS column. It is a binary representation of whether or not a variant has conflicting classifications, where 0 represents consistent classifications and 1 represents conflicting classifications.
The genetic variant dataset is public domain available from Kaggle
import org.apache.spark._
import org.apache.spark.SparkContext._
import org.apache.spark.rdd._
import org.apache.spark.util.LongAccumulator
import org.apache.log4j._
import scala.collection.mutable.ArrayBuffer
import org.apache.spark.sql._
Logger.getLogger("org").setLevel(Level.ERROR)
val spark = SparkSession
.builder
.appName("genetic classifier")
.master("local[*]")
.config("spark.sql.warehouse.dir", "file:///tmp")
.getOrCreate()
/*
Load the csv file into SparkSQL DataFrame, with options:
inferSchema=true, set the datatype of the feature columns based upon value data types
header=true, set the DataFrame column names from the headers of the csv file.
*/
val ds = spark.read.format("csv").option("inferSchema", "true").option("header", "true")
.option("quote", "\"")
.load("file:///home/bigdata2/dataset/clinvar_conflicting.csv")
val df: DataFrame = ds.toDF()
Notice there are lots missing values, i.e., null values in the dataset. Dataset having significant missing values usually affect training and prediction accuracy.
Some column values are large, like 6 digits, some column values are smaller than 1. Without addressing, this can confuse ML algorithm, which may regard some small values as statistically insignificant and disregard as result. This can be addressed in the data preprocessing using scaler. I plan to use MinMaxScaler, which will make all feature column values between 0 and 1
Apache Spark ML requires to place feature data to be in form of vector, i.e., place all feature columns into one vector, following code is to generate a column that is a vector that contains all feature columns
import org.apache.spark.ml.feature.VectorAssembler
import org.apache.spark.ml.linalg.Vectors
val assembler = new VectorAssembler()
.setInputCols(Array("CHROMindex", "POS", "REFindex", "ALTindex", "AF_ESP", "AF_EXAC", "AF_TGP", "CLNDISDBindex"
, "CLNDISDBINCLindex", "CLNDNindex", "CLNDNINCLindex", "CLNHGVSindex", "CLNSIGINCLindex", "CLNVCindex"
, "CLNVIindex", "MCindex", "ORIGIN", "SSRindex", "Alleleindex", "Consequenceindex", "IMPACTindex"
, "SYMBOLindex", "Feature_typeindex", "Featureindex", "BIOTYPEindex", "EXONindex", "INTRONindex"
, "cDNA_positionindex", "CDS_positionindex", "Protein_positionindex", "Amino_acidsindex", "Codonsindex"
, "DISTANCE", "STRAND", "BAM_EDITindex", "SIFTindex", "PolyPhenindex", "MOTIF_NAMEindex", "MOTIF_POS"
, "HIGH_INF_POSindex", "MOTIF_SCORE_CHANGE", "LoFtool", "CADD_PHRED", "CADD_RAW", "BLOSUM62"))
.setOutputCol("features")
val output = assembler.transform(newDf)
Resultant output DataFrame now has a new column features that is the vector containing all feature columns.
output.select("features").show(2,false)
/*
|features
|(45,[0,1,2,3,4,5,6,11,14,16,18,21,23,25,27,28,29,30,31,33,35,36,42,43,44],[3.0,1168180.0,1.0,3.0,0.0771,0.1002,0.1066,326.0,27451.0,1.0,3.0,1614.0,1728.0,15.0,618.0,471.0,304.0,56.0,129.0,1.0,2.0,1.0,1.053,-0.208682,2.0])|
|(45,[0,1,2,3,7,9,11,14,16,18,21,23,25,27,28,29,30,31,33,34,35,36,42,43,44],[3.0,1470752.0,1.0,1.0,7922.0,8822.0,467.0,25430.0,1.0,1.0,2293.0,2054.0,9.0,1534.0,1465.0,116.0,13.0,17.0,-1.0,1.0,4.0,1.0,31.0,6.517838,-3.0]) |
*/
Extract from output DataFrame into a new DataFrame, that only contains vector column features and label column CLASS
Normalize feature values in vector column features, using MinMax Scaler
import org.apache.spark.ml.feature.MinMaxScaler
import org.apache.spark.ml.linalg.Vectors
val scaler = new MinMaxScaler()
.setInputCol("features")
.setOutputCol("scaledFeatures")
// Compute summary statistics and generate MinMaxScalerModel
val scalerModel = scaler.fit(whole)
// rescale each feature to range [min, max].
val scaledData = scalerModel.transform(whole)
println(s"Features scaled to range: [${scaler.getMin}, ${scaler.getMax}]")
scaledData.select("features", "scaledFeatures").show(2,false)
/*
Features scaled to range: [0.0, 1.0]
|features |scaledFeatures
|(45,[0,1,2,3,4,5,6,11,14,16,18,21,23,25,27,28,29,30,31,33,35,36,42,43,44],[3.0,1168180.0,1.0,3.0,0.0771,0.1002,0.1066,326.0,27451.0,1.0,3.0,1614.0,1728.0,15.0,618.0,471.0,304.0,56.0,129.0,1.0,2.0,1.0,1.053,-0.208682,2.0])|(45,[0,1,2,3,4,5,6,11,14,16,18,21,23,25,27,28,29,30,31,33,35,36,40,42,43,44],[0.13043478260869565,0.004713998164155383,0.0011560693641618498,0.006564551422319475,0.15450901803607214,0.2004440977014943,0.21328531412565024,0.005000997131329867,0.9926592897953279,0.001949317738791423,0.00804289544235925,0.6932989690721649,0.729421696918531,0.004595588235294118,0.044237652111667865,0.03447266339749689,0.041422537130399235,0.044374009508716325,0.05810810810810811,1.0,0.5,0.25,0.9999999999999999,0.010636363636363637,0.10125579884341004,0.8333333333333333])|
|(45,[0,1,2,3,7,9,11,14,16,18,21,23,25,27,28,29,30,31,33,34,35,36,42,43,44],[3.0,1470752.0,1.0,1.0,7922.0,8822.0,467.0,25430.0,1.0,1.0,2293.0,2054.0,9.0,1534.0,1465.0,116.0,13.0,17.0,-1.0,1.0,4.0,1.0,31.0,6.517838,-3.0]) |(45,[0,1,2,3,7,9,11,14,16,18,21,23,25,27,28,29,30,31,34,35,36,40,42,43],[0.13043478260869565,0.0059359829438109775,0.0011560693641618498,0.002188183807439825,0.8580093144156828,0.9528026784749973,0.007164005093040024,0.9195776379547261,0.001949317738791423,0.002680965147453083,0.9849656357388316,0.8670325031658928,0.0027573529411764703,0.1098067287043665,0.10722388933616336,0.015805968115547075,0.010301109350237718,0.0076576576576576575,0.5,1.0,0.25,0.9999999999999999,0.31313131313131315,0.2305282934974466])
*/
Train/Test Split, randomly split 70% of rows for training, 30% for test
// Prepare training and test data.
val wholeScaledData=scaledData.select("scaledFeatures","CLASS")
val Array(training, test) = wholeScaledData.randomSplit(Array(0.7, 0.3), seed = 12345)
Training and Prediction
Algorithm Selection
Since this is a binary classification, I plan to use Multilayer Perceptron Classifier and Logistic Regression.
Multilayer Perceptron Classifier, there are 45 feature columns, and binary classification, I construct a neural network of 4 layer as below:
val layers = Array[Int](45, 45, 45, 2)
import org.apache.spark.ml.classification.MultilayerPerceptronClassifier
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
val trainer = new MultilayerPerceptronClassifier()
.setLayers(layers)
.setBlockSize(128)
.setSeed(1234L)
.setMaxIter(100)
.setFeaturesCol("scaledFeatures")
.setLabelCol("CLASS")
Train and test the neural network
val model=trainer.fit(training.select("scaledFeatures","CLASS"))
//and Test
val result = model.transform(test)
//Evaluate accuracy metrics
val predictionAndLabels = result.select("prediction", "CLASS")
val evaluator = new MulticlassClassificationEvaluator().setMetricName("accuracy").setLabelCol("CLASS")
println(s"Test set accuracy = ${evaluator.evaluate(predictionAndLabels)}")
//Test set accuracy = 0.7482470955524848
Logistic Regression Classifier
import org.apache.spark.ml.{Pipeline, PipelineStage}
import org.apache.spark.ml.classification.{LogisticRegression, LogisticRegressionModel}
import scala.collection.mutable
val maxIter=100
val lr = new LogisticRegression()
.setFeaturesCol("scaledFeatures")
.setLabelCol("CLASS")
.setMaxIter(maxIter)
val model_lr = lr.fit(training)
val prediction_lr = model_lr.transform(test)
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
val my_mc_accu = new MulticlassClassificationEvaluator().setPredictionCol("prediction").setLabelCol("CLASS").setMetricName("accuracy")
my_mc_accu.evaluate(prediction_lr)
my_mc_accu: org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator = mcEval_74ebac4de5a0
//res29: Double = 0.7463534469522494
Take away:
Both Mutilayer Perceptron Classifier and Logistic Regression produce same prediction accuracy about 75% given the dataset having significant amount missing values (null values). To improve machine learning performance, reduce missing values and possible adding additional feature columns may help.
As always, code used in this writing is in my GitHub Repo: