counts, featuredata=read10X('Data/')

"""
Explanation: Read Input data
End of explanation
"""


counts.shape

plotQC(counts)

"""
Explanation: Shape of dataset: Genes, Cells
End of explanation
"""


cpt=normalize(counts)

"""
Explanation: Normalize data
Since number of genes and transcipts detected is directly dependent on read depth, library size normalization is essential. This function will normalize gene expression based on total transcripts detected in each cell, multiply with a constant and log transform.
End of explanation
"""


top1000=overdispersion(cpt,1000)

"""
Explanation: Feature Selection
One of the most important steps in single cell RNA seq processing, is selecting genes that describe most of the biological variance. However, this is confounded by the high levels of technical noise associated with single cell RNA-seq data. 
This jupyter notebook contains 2 functions to enable feature selection:
1. variance - select the top variable genes in the dataset
2. overdispersion - select the top variable genes in the dataset corrected for technical variance
End of explanation
"""


tsnedata=runTSNE(cpt,top1000)

"""
Explanation: Dimensionality reduction
After gene selection, the high dimensionality of single cell RNA-seq data is commnly reduced to cluster similar cells together. 
This jupyter notebook contains 2 functions for dimensionality reduction:
1. PCA 
2. tSNE - for the purposes of the demonstration, we will use tSNE and reduce data to 3 dimensions
End of explanation
"""


plt.scatter(tsnedata['V2'],tsnedata['V3'],s=5)

"""
Explanation: Visualization
Visualization is an important part of an single cell experiment. Exploring data with genes of interest helps validate clustering as wells begins the process of identifying the cell type of each cluster
Lets take a look at our dimensionality reduction by plotting cells.
End of explanation
"""


plotTSNE(cpt,tsnedata,'Snap25',featuredata,'V2','V3')

"""
Explanation: Visualization
Visualization is an important part of an single cell experiment. Exploring data with genes of interest helps validate clustering as wells begins the process of identifying the cell type of each cluster
Lets take a look at our dimensionality reduction by plotting cells, but this time color each cell by the expression of particular gene. Pick from Emcn, Olig1, Olig2, Pdgra, Fyn, Aqp4,Mog,Slc32a1,Slc17a6,Cx3cr1.
End of explanation
"""


tsnedata_dbscan=dbscan(tsnedata,3.5,40)

"""
Explanation: Cluster identification
After dimensionality reduction, clusters are identified using a variety of approaches. We will use a simple algorithm called DBSCAN to identify clusters
This jupyter notebook contains 1 functions for dimensionality reduction:
1. DBSCAN
End of explanation
"""


plotTSNEClusters(tsnedata_dbscan,'V2','V3')

"""
Explanation: Visualization
Lets take a look at our dimensionality reduction by plotting cells, but this time color each cell by the cluster assignment as determined by DBSCAN
End of explanation
"""


tsne3d(tsnedata_dbscan)

"""
Explanation: Visualization
Lets take a look at our dimensionality reduction by plotting cells, but this time color each cell by the cluster assignment as determined by DBSCAN. Remember that our data was reduced to 3 dimensions. So, lets plot all 3 dimensions
End of explanation
"""


aurocScoresAll=pd.DataFrame()
for cluster in range(1,8,1):

    cells1=tsnedata_dbscan[tsnedata_dbscan['dbCluster']==cluster].index
    cells2=tsnedata_dbscan.index.difference(cells1)

    data1=cpt.loc[cpt.index,cells1].apply(expMean,1)
    data2=cpt.loc[cpt.index,cells2].apply(expMean,1)
    totaldiff=(data1-data2)
    genes=totaldiff[totaldiff>1.].index

    aurocScores=findMarkers(cpt,
                  cells1,
                  cells2,
                  genes
                 )
    aurocScores['Associated.Gene.Name']=featuredata['Associated.Gene.Name']
    aurocScores['dbCluster']=cluster
    aurocScoresAll=aurocScoresAll.append(aurocScores)

"""
Explanation: Marker Identification
Identifying genes that differentiate each of these cell populations is an important aspect of single cell RNA-seq data. There are many different methods to do this type of analysis. Given the size of the dataset ome of these are compute heavy. For the sake of brevity, we will use AUROC classification of differentially expressed genes.
End of explanation
"""


markerHeatmap(cpt,aurocScoresAll.index,tsnedata_dbscan,featuredata)

"""
Explanation: Visualization
Let's make a heatmap of all markergenes
End of explanation
"""


mkRds(cpt,featuredata,tsnedata_dbscan)

"""
Explanation: Make .Rds file for CellView
And finally, let's summarize this analysis into an .Rds file that we can share with others
End of explanation
"""


