# -*- coding: utf-8 -*-
"""
Created on Thu March 7 19:11:49 2016

Please, used the copy of this file 
    available from here http://bioinformatics.center/static/FLC/FLC_1.0.zip

@author:        Emmanuel Oluwatobi Salawu
@email:         es13acp@herts.ac.uk
@affiliations:  Bioinformatics Program, Institute of Information Science,
                    Academia Sinica, Taiwan
                Institute of Bioinformatics and Structural Biology, 
                    National Tsing Hua University, Taiwan
                School of Computer Science, University of Hertfordshire, UK

"""

"""
This user guide is written as a Python Script so that users can run it directly
and have better feeling of how to use FLC (Fuzzy Logic and Continuous Space 
Probabilistic Cellular Automata).

A. REQUIRED MODULES AND DATA
    To use FLC, a user should have access to the following 
    
    [1] Python
        We recommend Python 2.7.x. Your computer may already have Python.
        If not, you can follow the steps here https://www.python.org/downloads/
    
    [2] Required modules 
    
        I) NumPy
            http://www.numpy.org/ 
            Installation Command:               easy_install numpy
            Alternative Installation Command:   pip install numpy
        
        II) BioPython
            http://biopython.org/wiki/Main_Page
            Installation Command:               easy_install biopython
            Alternative Installation Command:   pip install biopython
        
    [3] Knowledge-based Parameters
        Knowledge-based parameters derived from the statistics of the physical 
        parameters (such as internal coordinates) that cells in the cellular 
        automata  represent in nature.
        You can create yours, but we recommeding downloaded a copy from
        our website http://bioinformatics.center/tools/flc
        If you require additional help on how you could create new set of 
        knowledge-based parameters, please, email us.
        
    [4] PDB File 
        PDB file of the molecules whose conformation space is to be sampled is 
        required.
        
B. SAMPLING CONFORMATION SPACE OF A SIMPLE PROTEIN MOLECULE
    The code below shows how to use FLC to sample conformation space of a 
    protein. 1PGB is used in the example below. 
    
    NOTE:
        I) The code shown below assumes that the PDB file "1PGB.pdb" is in the 
            same directory as FLC_1.0.py .
        
    [1] Section 1 loads the knowledge-based parameter
        
    [2] Section 2 sets some parameters
        
    [3] Section 3 sets random seed
        
    [4] Section 4 sets prefix of pdb file
        
    [5] Section 5 estracts needed information from PDB file
        
    [6] Section 6 generates needed conformations using FLC

C. SUPPPORT
    Please, email us if you require further assistance 
    Email Address: es13acp@herts.ac.uk
    

"""


execfile ("FLC_1.0.py")

#if __name__ == "__main__":
if True:
    "[1] Loading/calculating parametersa"
    try: trashThis = len (parameters_);
    except:
        import cPickle, numpy as np, math
        with open ('parameters.cP', 'rb') as dictionariesCpk: parameters_ = cPickle.load (dictionariesCpk)
    
    "[2] Setting some sampling paramaters"
    numberOfSteps = 500; 
    temperature = 300 #K
    rndSeed = 1
    replicate = 1
    
    "[3] Setting random seed"
    if not rndSeed:
        rnd.seed (rndSeed)
    else:
        rnd.seed (time.time())
    
    "[4] Setting prefix of pdb file"
    PDB_ID = "1PGB"
    
    "[5] Extracting needed information from PDB file"
    initStrctFileName = "%s.pdb.info" % (PDB_ID,)
    extractCoordIntoInfoFile ("%s" % (PDB_ID,), "%s.pdb" % (PDB_ID,), initStrctFileName, selection_ = {"CA"})
    
    
    "[6] Generating the conformations"
    initialCoordinate = genInitCoordFromCAxInfo (initStrctFileName); 
    resNames_doCA = [i[1] for i in initialCoordinate]
    #_start = time.time ()
    generatedCa = doFzCA (parameters_, initialCoordinate, numberOfSteps, temperature, samplingFactor = 2.0); 
    genPdbFromCA (generatedCa, stepping = 1, fileName = '%s_%dK_rep%d_.pdb' % (PDB_ID, temperature, replicate))
    #print '%d conformations took %.3f seconds.' % (numberOfSteps, time.time () - _start)
        

