{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Writing PB in file\n",
    "\n",
    "The API allows to write all the files the command line tools can. This includes the outputs of PBassign. The functions to handle several file formats are available in the :mod:`pbxplore.io` module."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function, division\n",
    "from pprint import pprint\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pbxplore as pbx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fasta files\n",
    "\n",
    "The most common way to save PB sequences is to write them in a fasta file."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PBxplore allows two ways to write fasta files. The sequences can be written either all at once or one at a time. To write a batch of sequences at once, we need a list of sequences and a list of the corresponding sequence names. The writing function here is :func:`pbxplore.io.write_fasta`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read 10 chain(s) in /home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb\n",
      "['/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 2 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 3 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 4 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 5 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 6 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 7 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 8 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 9 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A']\n",
      "['ZZbghiacfkbccdddddehiadddddddddddfklggcdddddddddddddehifbdcddddddddddfklopadddddfhpamlnopcddddddehjadddddehjacbddddddddfklmaccddddddfbgniaghiapaddddddfklnoambZZ',\n",
      " 'ZZpcfblcffbccdddddeehjacdddddddddfklggcddddddddddddddfblghiadddddddddfklopadddddehpmmmnopcddddddeehiacdddfblopadcddddddfklpaccdddddfklmlmgcdehiaddddddfklmmgopZZ',\n",
      " 'ZZmgghiafbbccdddddehjbdcdddddddddfklggcddddddddddddddfbfghpacddddddddfklopadddddehiaklmmmgcdddddeehiaddddfkbgciacdddddefklpaccddddddfkgojbdfehpaddddddfkbccfbgZZ',\n",
      " 'ZZcghiacfkbacdddddfbhpacdddddddddfklmcfdddddddddddddehiacddddddddddddfknopadddddfkpamlnopaddddddehjaccdddfklnopacddddddfklmpccdddddddehiabghehiaddddddfklpccfkZZ',\n",
      " 'ZZpaehiehkaccdddddehjbccdddddddddfklggcddddddddddddddfbhpadddddddddddfklopadddddehiamlmmpccdddddeehiadddddfbacddcddddddfklmaccddddddfbgghiafehiadddddddfklpacfZZ',\n",
      " 'ZZmghbacfkbccdddddeehpacdddddddddfklggcdddddddddddddehiacadddddddddddfklopadddddehiaklnopcddddddeehiadddehjlnopacddddddfklmaccddddehiaehbgcdehiadddddddfehjlpcZZ',\n",
      " 'ZZcchbacfkbccdddddfehpacdddddddddfklggcdddddddddddddddehjapadddddddddfknopadddddfklmmmnopcddddddehjiddddddfknopacddddddfklpaccdddddfklmaacdfehpadddddehjblckknZZ',\n",
      " 'ZZcehjdeehiacdjdddedjbdcdddddddddfklggcdddddddddddddddbfblbacddddddddfklopacddddehiamlnopaddddddehjacddddfehpaaccdddddefklpaccdddddfklmbfbehehiaddddddffkgoiehZZ',\n",
      " 'ZZpccdjdfkbccdddddehhpacdddddddddfklggcdddddddddddddehiacbdcdddddddddfklopadddddehiammnopcddddddeejiadddehjlgobacddddddfklmpccddddehiacbcbdfehpadddddehjklmklmZZ',\n",
      " 'ZZccfklcfkbccdddddehjbdcdddddddddfklggcdddddddddddddehiapaccdddddddddfklopadddddehjamlnopaddddddehjddcdddfbfghpacddddddfklpaccddddddfbcfbacfehpadddddddekpghiaZZ']\n"
     ]
    }
   ],
   "source": [
    "names = []\n",
    "pb_sequences = []\n",
    "pdb_path = os.path.join(pbx.DEMO_DATA_PATH, '2LFU.pdb')\n",
    "for chain_name, chain in pbx.chains_from_files([pdb_path]):\n",
    "    dihedrals = chain.get_phi_psi_angles()\n",
    "    pb_seq = pbx.assign(dihedrals)\n",
    "    names.append(chain_name)\n",
    "    pb_sequences.append(pb_seq)\n",
    "\n",
    "pprint(names)\n",
    "\n",
    "pprint(pb_sequences)\n",
    "\n",
    "with open('output.fasta', 'w') as outfile:\n",
    "    pbx.io.write_fasta(outfile, pb_sequences, names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A\r\n",
      "ZZbghiacfkbccdddddehiadddddddddddfklggcdddddddddddddehifbdcd\r\n",
      "dddddddddfklopadddddfhpamlnopcddddddehjadddddehjacbddddddddf\r\n",
      "klmaccddddddfbgniaghiapaddddddfklnoambZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 2 | chain A\r\n",
      "ZZpcfblcffbccdddddeehjacdddddddddfklggcddddddddddddddfblghia\r\n",
      "dddddddddfklopadddddehpmmmnopcddddddeehiacdddfblopadcddddddf\r\n",
      "klpaccdddddfklmlmgcdehiaddddddfklmmgopZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 3 | chain A\r\n",
      "ZZmgghiafbbccdddddehjbdcdddddddddfklggcddddddddddddddfbfghpa\r\n",
      "cddddddddfklopadddddehiaklmmmgcdddddeehiaddddfkbgciacdddddef\r\n",
      "klpaccddddddfkgojbdfehpaddddddfkbccfbgZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 4 | chain A\r\n",
      "ZZcghiacfkbacdddddfbhpacdddddddddfklmcfdddddddddddddehiacddd\r\n",
      "dddddddddfknopadddddfkpamlnopaddddddehjaccdddfklnopacddddddf\r\n",
      "klmpccdddddddehiabghehiaddddddfklpccfkZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 5 | chain A\r\n",
      "ZZpaehiehkaccdddddehjbccdddddddddfklggcddddddddddddddfbhpadd\r\n",
      "dddddddddfklopadddddehiamlmmpccdddddeehiadddddfbacddcddddddf\r\n",
      "klmaccddddddfbgghiafehiadddddddfklpacfZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 6 | chain A\r\n",
      "ZZmghbacfkbccdddddeehpacdddddddddfklggcdddddddddddddehiacadd\r\n",
      "dddddddddfklopadddddehiaklnopcddddddeehiadddehjlnopacddddddf\r\n",
      "klmaccddddehiaehbgcdehiadddddddfehjlpcZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 7 | chain A\r\n",
      "ZZcchbacfkbccdddddfehpacdddddddddfklggcdddddddddddddddehjapa\r\n",
      "dddddddddfknopadddddfklmmmnopcddddddehjiddddddfknopacddddddf\r\n",
      "klpaccdddddfklmaacdfehpadddddehjblckknZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 8 | chain A\r\n",
      "ZZcehjdeehiacdjdddedjbdcdddddddddfklggcdddddddddddddddbfblba\r\n",
      "cddddddddfklopacddddehiamlnopaddddddehjacddddfehpaaccdddddef\r\n",
      "klpaccdddddfklmbfbehehiaddddddffkgoiehZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 9 | chain A\r\n",
      "ZZpccdjdfkbccdddddehhpacdddddddddfklggcdddddddddddddehiacbdc\r\n",
      "dddddddddfklopadddddehiammnopcddddddeejiadddehjlgobacddddddf\r\n",
      "klmpccddddehiacbcbdfehpadddddehjklmklmZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A\r\n",
      "ZZccfklcfkbccdddddehjbdcdddddddddfklggcdddddddddddddehiapacc\r\n",
      "dddddddddfklopadddddehjamlnopaddddddehjddcdddfbfghpacddddddf\r\n",
      "klpaccddddddfbcfbacfehpadddddddekpghiaZZ\r\n"
     ]
    }
   ],
   "source": [
    "!cat output.fasta\n",
    "!rm output.fasta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sequences can be written once at a time using the :func:`pbxplore.io.write_fasta_entry` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read 10 chain(s) in /home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb\n"
     ]
    }
   ],
   "source": [
    "pdb_path = os.path.join(pbx.DEMO_DATA_PATH, '2LFU.pdb')\n",
    "with open('output.fasta', 'w') as outfile:\n",
    "    for chain_name, chain in pbx.chains_from_files([pdb_path]):\n",
    "        dihedrals = chain.get_phi_psi_angles()\n",
    "        pb_seq = pbx.assign(dihedrals)\n",
    "        pbx.io.write_fasta_entry(outfile, pb_seq, chain_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A\r\n",
      "ZZbghiacfkbccdddddehiadddddddddddfklggcdddddddddddddehifbdcd\r\n",
      "dddddddddfklopadddddfhpamlnopcddddddehjadddddehjacbddddddddf\r\n",
      "klmaccddddddfbgniaghiapaddddddfklnoambZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 2 | chain A\r\n",
      "ZZpcfblcffbccdddddeehjacdddddddddfklggcddddddddddddddfblghia\r\n",
      "dddddddddfklopadddddehpmmmnopcddddddeehiacdddfblopadcddddddf\r\n",
      "klpaccdddddfklmlmgcdehiaddddddfklmmgopZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 3 | chain A\r\n",
      "ZZmgghiafbbccdddddehjbdcdddddddddfklggcddddddddddddddfbfghpa\r\n",
      "cddddddddfklopadddddehiaklmmmgcdddddeehiaddddfkbgciacdddddef\r\n",
      "klpaccddddddfkgojbdfehpaddddddfkbccfbgZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 4 | chain A\r\n",
      "ZZcghiacfkbacdddddfbhpacdddddddddfklmcfdddddddddddddehiacddd\r\n",
      "dddddddddfknopadddddfkpamlnopaddddddehjaccdddfklnopacddddddf\r\n",
      "klmpccdddddddehiabghehiaddddddfklpccfkZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 5 | chain A\r\n",
      "ZZpaehiehkaccdddddehjbccdddddddddfklggcddddddddddddddfbhpadd\r\n",
      "dddddddddfklopadddddehiamlmmpccdddddeehiadddddfbacddcddddddf\r\n",
      "klmaccddddddfbgghiafehiadddddddfklpacfZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 6 | chain A\r\n",
      "ZZmghbacfkbccdddddeehpacdddddddddfklggcdddddddddddddehiacadd\r\n",
      "dddddddddfklopadddddehiaklnopcddddddeehiadddehjlnopacddddddf\r\n",
      "klmaccddddehiaehbgcdehiadddddddfehjlpcZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 7 | chain A\r\n",
      "ZZcchbacfkbccdddddfehpacdddddddddfklggcdddddddddddddddehjapa\r\n",
      "dddddddddfknopadddddfklmmmnopcddddddehjiddddddfknopacddddddf\r\n",
      "klpaccdddddfklmaacdfehpadddddehjblckknZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 8 | chain A\r\n",
      "ZZcehjdeehiacdjdddedjbdcdddddddddfklggcdddddddddddddddbfblba\r\n",
      "cddddddddfklopacddddehiamlnopaddddddehjacddddfehpaaccdddddef\r\n",
      "klpaccdddddfklmbfbehehiaddddddffkgoiehZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 9 | chain A\r\n",
      "ZZpccdjdfkbccdddddehhpacdddddddddfklggcdddddddddddddehiacbdc\r\n",
      "dddddddddfklopadddddehiammnopcddddddeejiadddehjlgobacddddddf\r\n",
      "klmpccddddehiacbcbdfehpadddddehjklmklmZZ\r\n",
      ">/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A\r\n",
      "ZZccfklcfkbccdddddehjbdcdddddddddfklggcdddddddddddddehiapacc\r\n",
      "dddddddddfklopadddddehjamlnopaddddddehjddcdddfbfghpacddddddf\r\n",
      "klpaccddddddfbcfbacfehpadddddddekpghiaZZ\r\n"
     ]
    }
   ],
   "source": [
    "!cat output.fasta\n",
    "!rm output.fasta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, the lines in fasta files are wrapped at 60 caracters as defined in :const:`pbxplore.io.fasta.FASTA_WIDTH`. Both :func:`pbxplore.io.write_fasta` and :func:`pbxplore.io.write_fasta_entry` have a `width` optionnal argument that allows to control the wrapping."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ZZbghiacfkbccdddddehiadddddddddddfklggcdddddddddddddehifbdcddddddddddfklopadddddfhpamlnopcddddddehjadddddehjacbddddddddfklmaccddddddfbgniaghiapaddddddfklnoambZZ\n"
     ]
    }
   ],
   "source": [
    "print(pb_sequences[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('output.fasta', 'w') as outfile:\n",
    "    for width in (60, 70, 80):\n",
    "        pbx.io.write_fasta_entry(outfile, pb_sequences[0],\n",
    "                                        'width={} blocks'.format(width),\n",
    "                                        width=width)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ">width=60 blocks\r\n",
      "ZZbghiacfkbccdddddehiadddddddddddfklggcdddddddddddddehifbdcd\r\n",
      "dddddddddfklopadddddfhpamlnopcddddddehjadddddehjacbddddddddf\r\n",
      "klmaccddddddfbgniaghiapaddddddfklnoambZZ\r\n",
      ">width=70 blocks\r\n",
      "ZZbghiacfkbccdddddehiadddddddddddfklggcdddddddddddddehifbdcddddddddddf\r\n",
      "klopadddddfhpamlnopcddddddehjadddddehjacbddddddddfklmaccddddddfbgniagh\r\n",
      "iapaddddddfklnoambZZ\r\n",
      ">width=80 blocks\r\n",
      "ZZbghiacfkbccdddddehiadddddddddddfklggcdddddddddddddehifbdcddddddddddfklopaddddd\r\n",
      "fhpamlnopcddddddehjadddddehjacbddddddddfklmaccddddddfbgniaghiapaddddddfklnoambZZ\r\n"
     ]
    }
   ],
   "source": [
    "!cat output.fasta\n",
    "!rm output.fasta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dihedral angles\n",
    "\n",
    "One needs the phi and psi dihedral angles to assign protein block sequences. Having these angles, it is sometime convenient to store them in a file. This can be done easily."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read 10 chain(s) in /home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb\n"
     ]
    }
   ],
   "source": [
    "pdb_path = os.path.join(pbx.DEMO_DATA_PATH, '2LFU.pdb')\n",
    "\n",
    "with open('output.phipsi', 'w') as outfile:\n",
    "    for chain_name, chain in pbx.chains_from_files([pdb_path]):\n",
    "        dihedral = chain.get_phi_psi_angles()\n",
    "        for res in sorted(dihedral):\n",
    "            phi = \"{:8.2f}\".format(dihedral[res][\"phi\"]) if dihedral[res][\"phi\"] else \"    None\"\n",
    "            psi = \"{:8.2f}\".format(dihedral[res][\"psi\"]) if dihedral[res][\"psi\"] else \"    None\"\n",
    "            print(\"{} {:6d} {} {} \".format(chain_name, res, phi, psi), file=outfile)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note it's better to write the dihedral for each PDB/frame due to the high memory cost to store all of them in a list."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The output is formated with one line per residue. The first columns repeat the name given for the chain, then is the residue id followed by the phi and the psi angle. If an angle is not defined, 'None' is written instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A    276     None    44.77 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A    277   -81.67    15.27 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A    278   -79.03   -21.52 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A    279  -144.43    64.08 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A    280   -84.23   144.90 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A    281    70.83   -64.01 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A    282  -107.77    93.35 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A    283   -71.42   108.03 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A    284   -72.99    99.31 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A    285   -92.93     2.32 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A    426   -88.16   127.99 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A    427  -107.40  -161.76 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A    428   -72.12   -14.94 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A    429    66.62   -81.17 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A    430  -141.67   111.76 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A    431   -69.37   141.19 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A    432    76.08   -75.91 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A    433  -149.23   167.34 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A    434   -63.43   -27.06 \n",
      "/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A    435  -166.70     None \n"
     ]
    }
   ],
   "source": [
    "!head output.phipsi\n",
    "!tail output.phipsi\n",
    "!rm output.phipsi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read fasta files\n",
    "\n",
    "We want to read sequences that we wrote in files. PBxplore provides a function to read fasta files: the :func:`pbxplore.io.read_fasta` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read 10 chain(s) in /home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb\n",
      "read 10 sequences in output.fasta\n",
      "names:\n",
      "['/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 2 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 3 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 4 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 5 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 6 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 7 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 8 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 9 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 10 | chain A']\n",
      "sequences:\n",
      "['ZZbghiacfkbccdddddehiadddddddddddfklggcdddddddddddddehifbdcddddddddddfklopadddddfhpamlnopcddddddehjadddddehjacbddddddddfklmaccddddddfbgniaghiapaddddddfklnoambZZ',\n",
      " 'ZZpcfblcffbccdddddeehjacdddddddddfklggcddddddddddddddfblghiadddddddddfklopadddddehpmmmnopcddddddeehiacdddfblopadcddddddfklpaccdddddfklmlmgcdehiaddddddfklmmgopZZ',\n",
      " 'ZZmgghiafbbccdddddehjbdcdddddddddfklggcddddddddddddddfbfghpacddddddddfklopadddddehiaklmmmgcdddddeehiaddddfkbgciacdddddefklpaccddddddfkgojbdfehpaddddddfkbccfbgZZ',\n",
      " 'ZZcghiacfkbacdddddfbhpacdddddddddfklmcfdddddddddddddehiacddddddddddddfknopadddddfkpamlnopaddddddehjaccdddfklnopacddddddfklmpccdddddddehiabghehiaddddddfklpccfkZZ',\n",
      " 'ZZpaehiehkaccdddddehjbccdddddddddfklggcddddddddddddddfbhpadddddddddddfklopadddddehiamlmmpccdddddeehiadddddfbacddcddddddfklmaccddddddfbgghiafehiadddddddfklpacfZZ',\n",
      " 'ZZmghbacfkbccdddddeehpacdddddddddfklggcdddddddddddddehiacadddddddddddfklopadddddehiaklnopcddddddeehiadddehjlnopacddddddfklmaccddddehiaehbgcdehiadddddddfehjlpcZZ',\n",
      " 'ZZcchbacfkbccdddddfehpacdddddddddfklggcdddddddddddddddehjapadddddddddfknopadddddfklmmmnopcddddddehjiddddddfknopacddddddfklpaccdddddfklmaacdfehpadddddehjblckknZZ',\n",
      " 'ZZcehjdeehiacdjdddedjbdcdddddddddfklggcdddddddddddddddbfblbacddddddddfklopacddddehiamlnopaddddddehjacddddfehpaaccdddddefklpaccdddddfklmbfbehehiaddddddffkgoiehZZ',\n",
      " 'ZZpccdjdfkbccdddddehhpacdddddddddfklggcdddddddddddddehiacbdcdddddddddfklopadddddehiammnopcddddddeejiadddehjlgobacddddddfklmpccddddehiacbcbdfehpadddddehjklmklmZZ',\n",
      " 'ZZccfklcfkbccdddddehjbdcdddddddddfklggcdddddddddddddehiapaccdddddddddfklopadddddehjamlnopaddddddehjddcdddfbfghpacddddddfklpaccddddddfbcfbacfehpadddddddekpghiaZZ']\n"
     ]
    }
   ],
   "source": [
    "def pdb_to_fasta_pb(pdb_path, fasta_path):\n",
    "    \"\"\"\n",
    "    Write a fasta file with all the PB sequences from a PDB\n",
    "    \"\"\"\n",
    "    with open(fasta_path, 'w') as outfile:\n",
    "        for chain_name, chain in pbx.chains_from_files([pdb_path]):\n",
    "            dihedrals = chain.get_phi_psi_angles()\n",
    "            pb_seq = pbx.assign(dihedrals)\n",
    "            pbx.io.write_fasta_entry(outfile, pb_seq, chain_name)\n",
    "\n",
    "# Write a fasta file\n",
    "pdb_path = os.path.join(pbx.DEMO_DATA_PATH, '2LFU.pdb')\n",
    "pdb_to_fasta_pb(pdb_path, 'output.fasta')\n",
    "\n",
    "# Read a list of headers and a list of sequences from a fasta file\n",
    "names, sequences = pbx.io.read_fasta('output.fasta')\n",
    "\n",
    "print('names:')\n",
    "pprint(names)\n",
    "print('sequences:')\n",
    "pprint(sequences)\n",
    "\n",
    "!rm output.fasta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the sequences we want to read are spread amongst several fasta files, then we can use the :func:`pbxplore.io.read_several_fasta` function that takes a list of fasta file path as argument instead of a single path."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read 1 chain(s) in /home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/1BTA.pdb\n",
      "Read 10 chain(s) in /home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb\n",
      "read 1 sequences in 1BTA.fasta\n",
      "read 10 sequences in 2FLU.fasta\n",
      "names:\n",
      "['/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/1BTA.pdb | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 1 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 2 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 3 | chain A',\n",
      " '/home/temple/santuz/projets/depots/PBxplore/pbxplore/demo/2LFU.pdb | model 4 | chain A']\n",
      "sequences:\n",
      "['ZZdddfklonbfklmmmmmmmmnopafklnoiaklmmmmmnoopacddddddehkllmmmmngoilmmmmmmmmmmmmnopacdcddZZ',\n",
      " 'ZZbghiacfkbccdddddehiadddddddddddfklggcdddddddddddddehifbdcddddddddddfklopadddddfhpamlnopcddddddehjadddddehjacbddddddddfklmaccddddddfbgniaghiapaddddddfklnoambZZ',\n",
      " 'ZZpcfblcffbccdddddeehjacdddddddddfklggcddddddddddddddfblghiadddddddddfklopadddddehpmmmnopcddddddeehiacdddfblopadcddddddfklpaccdddddfklmlmgcdehiaddddddfklmmgopZZ',\n",
      " 'ZZmgghiafbbccdddddehjbdcdddddddddfklggcddddddddddddddfbfghpacddddddddfklopadddddehiaklmmmgcdddddeehiaddddfkbgciacdddddefklpaccddddddfkgojbdfehpaddddddfkbccfbgZZ',\n",
      " 'ZZcghiacfkbacdddddfbhpacdddddddddfklmcfdddddddddddddehiacddddddddddddfknopadddddfkpamlnopaddddddehjaccdddfklnopacddddddfklmpccdddddddehiabghehiaddddddfklpccfkZZ']\n"
     ]
    }
   ],
   "source": [
    "# Write several fasta files\n",
    "pdb_to_fasta_pb(os.path.join(pbx.DEMO_DATA_PATH, '1BTA.pdb'), '1BTA.fasta')\n",
    "pdb_to_fasta_pb(os.path.join(pbx.DEMO_DATA_PATH, '2LFU.pdb'), '2FLU.fasta')\n",
    "\n",
    "# Read the fasta files\n",
    "names, sequences = pbx.io.read_several_fasta(['1BTA.fasta', '2FLU.fasta'])\n",
    "\n",
    "# Print the first entries\n",
    "print('names:')\n",
    "pprint(names[:5])\n",
    "print('sequences:')\n",
    "pprint(sequences[:5])\n",
    "\n",
    "!rm 1BTA.fasta 2FLU.fasta"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
