# **************************************************************************
# *
# * Authors:     P. Conesa (pconesa@cnb.csic.es) [1]
# *              J.M. De la Rosa Trevin (delarosatrevin@scilifelab.se) [2]
# *
# * [1] Unidad de  Bioinformatica of Centro Nacional de Biotecnologia , CSIC
# * [2] SciLifeLab, Stockholm University
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2 of the License, or
# * (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# * 02111-1307  USA
# *
# *  All comments concerning this program package may be sent to the
# *  e-mail address 'scipion@cnb.csic.es'
# *
# **************************************************************************
from pwem.protocols import ProtImportPdb, ProtImportParticles, ProtImportVolumes
from pwem.tests.workflows import TestWorkflow
from pwem import Domain
from pyworkflow.tests import setupTestProject, DataSet

from continuousflex.protocols import (FlexProtNMA, FlexProtAlignmentNMA,
                                      FlexProtDimredNMA, NMA_CUTOFF_ABS,
                                      FlexProtConvertToPseudoAtoms)
from continuousflex.protocols.pdb.protocol_pseudoatoms_base import NMA_MASK_THRE


class TestHEMNMA(TestWorkflow):
    """ Check the images are converted properly to spider format. """
    
    @classmethod
    def setUpClass(cls):    
        # Create a new project
        setupTestProject(cls)
        cls.ds = DataSet.getDataSet('nma')
    
    def test_nma1(self):
        """ Run NMA simple workflow for both Atomic and Pseudoatoms. """
        
        #------------------------------------------------
        # Case 1. Import a Pdb -> NMA
        #------------------------------------------------
        
        # Import a PDB
        protImportPdb = self.newProtocol(ProtImportPdb, inputPdbData=1,
                                         pdbFile=self.ds.getFile('pdb'))
        self.launchProtocol(protImportPdb)
        
        # Launch NMA for PDB imported
        protNMA1 = self.newProtocol(FlexProtNMA,
                                    cutoffMode=NMA_CUTOFF_ABS)
        protNMA1.inputStructure.set(protImportPdb.outputPdb)
        self.launchProtocol(protNMA1)
        
        # Import the set of particles 
        # (in this order just to be in the middle in the tree)
        protImportParts = self.newProtocol(ProtImportParticles,
                                           filesPath=self.ds.getFile('particles'),
                                           samplingRate=1.0)
        self.launchProtocol(protImportParts) 

        # Launch NMA alignment, but just reading result from a previous metadata
        protAlignment = self.newProtocol(FlexProtAlignmentNMA,
                                         modeList='7-9',
                                         copyDeformations=self.ds.getFile('gold/pseudo_run1_images.xmd'))
        protAlignment.inputModes.set(protNMA1.outputModes)
        protAlignment.inputParticles.set(protImportParts.outputParticles)
        self.launchProtocol(protAlignment)       
        
        # Launch Dimred after NMA alignment 
        protDimRed = self.newProtocol(FlexProtDimredNMA,
                                      dimredMethod=0,  # PCA
                                      reducedDim=2)
        protDimRed.inputNMA.set(protAlignment)
        self.launchProtocol(protDimRed)     
    
    def test_nma2(self):
        #------------------------------------------------        
        # Case 2. Import Vol -> Pdb -> NMA
        #------------------------------------------------
        # Import the set of particles 
        # (in this order just to be in the middle in the tree)
        protImportParts = self.newProtocol(ProtImportParticles,
                                           filesPath=self.ds.getFile('particles'),
                                           samplingRate=1.0)
        self.launchProtocol(protImportParts) 
        
        # Import a Volume
        protImportVol = self.newProtocol(ProtImportVolumes,
                                         filesPath=self.ds.getFile('vol'),
                                         samplingRate=1.0)
        self.launchProtocol(protImportVol)
        
        # Convert the Volume to Pdb
        protConvertVol = self.newProtocol(FlexProtConvertToPseudoAtoms)
        protConvertVol.inputStructure.set(protImportVol.outputVolume)
        protConvertVol.maskMode.set(NMA_MASK_THRE)
        protConvertVol.maskThreshold.set(0.2)
        protConvertVol.pseudoAtomRadius.set(2.5)
        self.launchProtocol(protConvertVol)
        
        # Launch NMA with Pseudoatoms
        protNMA2 = self.newProtocol(FlexProtNMA,
                                    cutoffMode=NMA_CUTOFF_ABS)
        protNMA2.inputStructure.set(protConvertVol.outputPdb)
        self.launchProtocol(protNMA2)
                                          
        # Launch NMA alignment, but just reading result from a previous metadata
        protAlignment = self.newProtocol(FlexProtAlignmentNMA,
                                         modeList='7-9',
                                         copyDeformations=self.ds.getFile('gold/pseudo_run1_images.xmd'))
        protAlignment.inputModes.set(protNMA2.outputModes)
        protAlignment.inputParticles.set(protImportParts.outputParticles)
        self.launchProtocol(protAlignment)       
        
        # Launch Dimred after NMA alignment 
        protDimRed = self.newProtocol(FlexProtDimredNMA,
                                      dimredMethod=0,  # PCA
                                      reducedDim=2)
        protDimRed.inputNMA.set(protAlignment)
        self.launchProtocol(protDimRed)     

