{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Map Reduce Example\n", "\n", "This demo shows how to use Map and Reduce to count the total number of atoms in PDB\n", "\n", "## Import" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pyspark import SparkConf, SparkContext\n", "from mmtfPyspark.io import mmtfReader" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Configure Spark" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "conf = SparkConf().setMaster(\"local[*]\") \\\n", " .setAppName(\"MapReduceExample\")\n", "sc = SparkContext(conf = conf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Read in MMTF files" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "path = \"../../resources/mmtf_full_sample/\"\n", "\n", "pdb = mmtfReader.read_sequence_file(path, sc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Count number of atoms\n", "\n", "1) Map each mmtf structure to it's number of atoms\n", "\n", "2) Count total nubmer of atoms using reduce" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total number of atoms in PDB: 29059439\n" ] } ], "source": [ "numAtoms = pdb.map(lambda t: t[1].num_atoms).reduce(lambda a,b: a+b)\n", "\n", "print(f\"Total number of atoms in PDB: {numAtoms}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Terminate Spark" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "sc.stop()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }