import scanpy.api as sc
import numpy as np
sc.settings.verbosity = 3                # verbosity = 3: errors, warnings, info, hints
sc.settings.set_figure_params(dpi=80)    # dots (pixels) per inch determine size of inline figures
sc.logging.print_versions()

"""
Explanation: First compiled: November 11, 2017 by T. Callies.
Exporting to SPRING
<table style="width:100%">
  <tr>
    <th>Gene_Exploration</th>
    <th>Cluster_Visualization</th> 
    <th>Pseudotime</th>
  </tr>
  <tr>
    <td><img src="figures/Krumsiek_Gene.png", height="420" width="680"> </td>
    <td><img src="figures/Krumiek_Clusters.png" , height="420" width="680"> </td>
    <td><img src="figures/Krumsiek_Pseudotime.png", height="420" width="680" > </td>
  </tr>
</table>

In contrast to Scanpy's builtin visualization tools, the graph drawing tool SPRING of 
Weinreb et al. (2017) allows an interactive exploration of data.
Scanpy allows exporting SPRING projects via sc.export_to.spring_dir.
To start, clone (or download) https://github.com/AllonKleinLab/SPRING to your preferred destination directory, in the following referred to as SPRING_INSTALLATION. Here, we use the current working directory of this notebook.
git clone https://github.com/AllonKleinLab/SPRING.git
After exporting an AnnData object adata
sc.export_to.spring_project(adata)
in the terminal, type
python -m http.server 8000 &amp;
or python3 if python defaults to a Python 2 installation.
Now, in a browser, open 
http://localhost:8000/&lt;SPRING_INSTALLATION&gt;/springViewer.html?&lt;Relative_Path_To_SPRING_PROJECT&gt;
Example 1: simulated myeloid progenitors
End of explanation
"""


adata_krumsiek11 = sc.datasets.krumsiek11()

"""
Explanation: For background information, see here.
End of explanation
"""


sc.pp.neighbors(adata_krumsiek11)

sc.tl.louvain(adata_krumsiek11)
sc.tl.dpt(adata_krumsiek11, n_branchings=1)

"""
Explanation: Let us add some computational analysis results.
End of explanation
"""


sc.tl.draw_graph(adata_krumsiek11)

"""
Explanation: The sc.tl.draw_graph function provides various graph drawing layouts, but not in an interactive way.
End of explanation
"""


sc.pl.draw_graph(adata_krumsiek11, color=['cell_type', 'louvain', 'dpt_groups', 'dpt_pseudotime', 'Gata1'])

adata_krumsiek11.write('./write/krumsiek11.h5')

"""
Explanation: Let's plot this.
End of explanation
"""


adata_krumsiek11 = sc.read('./write/krumsiek11.h5')

sc.export_to.spring_project(adata_krumsiek11, './spring_krumsiek11')

"""
Explanation: Exporting the results to a SPRING project directory.
End of explanation
"""


adata = sc.read('../170505_seurat/write/pmbc3k.h5ad')

sc.tl.draw_graph(adata)

"""
Explanation: Now, after starting a server, open
http://localhost:8000/SPRING/springViewer.html?../spring_kurmsiek11
You might need to click 'center view' and 'deselect all' to see the data.
Example 2: 3k PBMCs data
This starts from the standard clustering tutorial: https://github.com/theislab/scanpy_usage/tree/master/170505_seurat.
Selecting genes: It is recommended that - especially for large datasets - only a preselected number of genes of interest is used for SPRING, so that these genes can be found more easily (in the dataset below, searching for a certain gene in a dropdown list of ca. 13000 entries can be hard ).
End of explanation
"""


sc.pl.draw_graph(adata, color=['NKG7', 'louvain'])

# Rank genes in order to include only significant ones. 
# Note that the ranking function by default ranks 100, not 20 as displayed below. 
sc.tl.rank_genes_groups(adata, 'louvain')
sc.pl.rank_genes_groups(adata, n_genes=20)

# export some known marker genes
known_marker_genes = ['IL7R','CD14','LYZ', 'MS4A1', 'CD8A', 'FCGR3A', 'MS4A7',
                      'GNLY', 'NKG7', 'FCER1A', 'CST3', 'PPBP']
sc.export_to.spring_project(
    adata, './spring_pbmc3k_known_markers', 
    use_genes=known_marker_genes)

# export the ranked genes
sc.export_to.spring_project(
    adata, './spring_pbmc3k_ranked_genes', 
    use_genes='rank_genes_groups')

"""
Explanation: Plot the groups.
End of explanation
"""


