class MyForm(forms.Form):
  #  display_type = forms.ChoiceField(widget=forms.RadioSelect, choices=DISPLAY_CHOICES)
    display_type = forms.ChoiceField(widget=forms.CheckboxInput, choices=DISPLAY_CHOICES) class Experiments(models.Model):
	#~ text do not have limits
    run_name = models.TextField(primary_key=True)
    ftpStatus = models.TextField(default='no ftpstatus')
    status = models.TextField(default='no status')
    cnvFileName = models.TextField(default='no cnv')
    resultsQuery = models.TextField( default='no results')
    dateIonProton = models.TextField(default='no date')
    #~ dictionnary = JSONField(default='')
    dictionnary = models.TextField( default='no dict')


    def __str__(self):
        return self.run_name class GalaxyUsers(models.Model):
    #~ text do not have limits
    user_id = models.CharField(max_length=200)
    user_email = models.CharField(max_length=200,primary_key=True)
    user_aphid = models.CharField(max_length=200)
    user_apikey = models.CharField(max_length=200,default='no_apikey')
    
    
    def __str__(self):
        return self.user_email class toDownloads(models.Model):
	#~ text do not have limits
    folder_name = models.TextField(primary_key=True)
    filesystempath = models.TextField(default='no filesystempath')
    reference = models.TextField(default='no reference')
    status = models.TextField(default='no status')
    timeStamp = models.TextField(default='no timeStamp')
    timeToComplete = models.TextField( default='no timeToComplete')
    backupOnNas = models.TextField(default='false')
    backupValidate = models.TextField(default='false')
    diskusage = models.TextField(default='data 0')
    
    def __str__(self):
        return self.filesystempath +str(self.reference) + str(self.timeStamp) class GalaxyJobs(models.Model):
    #~ Put here info about tools state tools info tools id such as version and so and so
    tag_id=models.CharField(max_length=200,default='no_tag',primary_key=True) #personal
    resultsName = models.TextField(default='no_resultsName') #from Proton
    history_id = models.CharField(max_length=200,default='no_id') #from GALAXY
    history_name = models.CharField(max_length=200,default='no_name') #from GALAXY
    #~ history_user_email = models.CharField(max_length=200,default='no_email')
    history_user_email = models.ForeignKey(GalaxyUsers, on_delete=models.CASCADE) #from GalaxyUsers
    #~ history_analyse_type = models.CharField(max_length=200,default='no_type') #from Workflows
    history_analyse_type = models.ForeignKey(Workflows) #from Workflows
    history_state = models.CharField(max_length=200,default='no_state')#from GALAXY
    history_today = models.CharField(max_length=200,default='no_day')#from GALAXY
    history_percent_complete = models.CharField(max_length=200,default='no_complete')#from GALAXY
    #a comma separated list of ids
    #~ list_experimentRawData = models.ManyToManyField(ExperimentRawData) #from ExperimentRawData
    list_experimentRawData = models.ManyToManyField(NGSBamData) #from ExperimentRawData
    history_datasets_id = models.TextField(default='no_datasets')#from GALAXY
    history_download = models.BooleanField(default=False)#from GALAXY (download results)
    #~ history_upload = models.BooleanField(default=False)#from GALAXY (upload rawdata)
    #~ #suspendu,en_cours,en_upload, complet
    #~suspendu -->et history_download=False le job a ete creer par l'utilisateur
    #~ en_cours --> et history_download=False le telechargement des donnees sur le nas est en train de s'effectuer
    #~ en_upload--> et history_download=True et history_upload=True le telechargement des donnees sur le nas est effectuer
    #~ Les donnees sont chargees dans galaxy
    progression = models.CharField(max_length=200,default='suspendu') 
    galaxy_dictionnary=  models.TextField( default='no dict')
        
    def __str__(self):
        return ("JOB TAG :"+self.tag_id+"\nHISTORY NAME : "+self.history_name+"\nCREATED FOR USER : "+self.history_user_email.user_email) class ExperimentRawData(models.Model):
    #~ text do not have limits
    ionTag = models.TextField(default='no tag')
    sampleName = models.TextField(default='no sample name')
    bam_path = models.TextField(default='no path')
    
    experienceName = models.ForeignKey(Experiments, on_delete=models.CASCADE)
    
    def __str__(self):
        return self.bam_path class savedNGSData(models.Model):
	#~ text do not have limits
    filesystempath = models.TextField(primary_key=True)
    folder_name = models.TextField(default='no filesystempath')
    status = models.TextField(default='no status')    
    dataDictionnary = models.TextField(default='no dictionnary')    
    #~ bamData = models.TextField(default='no reference')
    #~ covData = models.TextField(default='no status')
    #~ vcfData = models.TextField(default='no timeStamp')
    def __str__(self):
        return self.filesystempath +str(self.folder_name) + str(self.status) class Workflows(models.Model):
    #~ text do not have limits
    name = models.TextField(default='no_name',primary_key=True)
    description = models.TextField(default='description')
    tools_list= models.ManyToManyField(WorkflowsTools)
    
    def __str__(self):
        return (self.name +" "+self.description) class WorkflowsTools(models.Model):
    #~ text do not have limits
    primary_name = models.CharField(max_length=200,default='no_name',primary_key=True)
    name = models.CharField(max_length=200,default='no_name')
    version = models.CharField(max_length=200,default='version')
    inputlist= models.ManyToManyField(Supportedfiles,related_name='inputlist')
    outputlist = models.ManyToManyField(Supportedfiles,related_name='outputlist')
    def __str__(self):
        return (self.name +" "+self.version) class Supportedfiles(models.Model):
    #~ text do not have limits
    dataHandle = models.TextField(default='dataHandle')
    dataDescription = models.TextField(default='dataDescription')
    dataFormatEdamOntology = models.TextField(default='dataFormatEdamOntology')
   
    def __str__(self):
        return (self.dataDescription +" "+self.dataFormatEdamOntology) class NGSBamData(models.Model):
    #~ text do not have limits
    ionTag = models.TextField(default='no tag')
    sampleName = models.TextField(default='no sample name')
    bam_path = models.TextField(default='no path')
    experienceName = models.ForeignKey(savedNGSData, on_delete=models.CASCADE)
    
    def __str__(self):
        return self.bam_path
import logging
import json
import os
import tasks
import string
import random
import requests
import paramiko
from django.shortcuts import get_object_or_404,render
from .forms import MyForm
from django.urls import reverse
from django.http import HttpResponseRedirect, HttpResponse
from django.template import loader
from datamanagerpkg import ProtonCommunication_data_manager
from datamanagerpkg import GalaxyCommunication_data_manager
from .models import Experiments, GalaxyUsers ,toDownloads
from .models import GalaxyJobs, ExperimentRawData, savedNGSData
from .models import Workflows,WorkflowsTools,Supportedfiles,NGSBamData
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
from bioblend.galaxy import GalaxyInstance
from pprint import pprint
from scp import SCPClient
from GlobalVariables import sequencer_base_url 
from GlobalVariables import sequencer_user
from GlobalVariables import sequencer_password
from GlobalVariables import sequencer_severName
from GlobalVariables import sequencer_ExperimentLimit
from GlobalVariables import  galaxy_base_url
from GlobalVariables import  apiKey
from GlobalVariables import nasInput
from GlobalVariables import CNVfolderName
from GlobalVariables import plasmaFolderName
from GlobalVariables import nasResults
from GlobalVariables import workflowPath
    gi=GalaxyCommunication_data_manager.galaxyConnection(galaxy_base_url,apiKey)
    users=GalaxyCommunication_data_manager.returnGalaxyUsers(gi) #return a list of dictionnary
    logger.info( "##########################")
    logger.info( "checkusers from getGalaxyUsers")
    checkUsersKey(users,gi)
    djangoUsers( GalaxyUsers.objects.all())  
    logger.info( "##########################")
    #~ # Always return an HttpResponseRedirect after successfully dealing
    #~ with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
    return HttpResponseRedirect(reverse('sequencer:savedData'))
    #~ return HttpResponseRedirect(reverse('sequencer:projects'))

def getBamReviewed(request,experiment_name):
    logger.info("##########################")
    logger.info("START getBamReviewed() view")
    logger.info("##########################")
    logger.info("Check User autentification")
    usermail=request.user.username
    if request.user.is_authenticated():
        logger.debug("User logged as ; %s",request.user.username)    
    else:
        logger.error("No user autentificated, No able to pursue further the analysis")
    logger.info("##########################")
    logger.info("Parsed User Request")
    logger.info("##########################")    
    UserRequest=request.POST
    myCheckedBam=[]
    for key, val in UserRequest.iteritems():
        #~ print str(key)
        #~ print str(val)
        if 'plasmaCheckboxValue' in str(key):
            myCheckedBam.append(str(val))
    current_exp = Experiments.objects.get(run_name=experiment_name)
    experimentsDict=json.loads(current_exp.dictionnary)
    bamtoDl=[]
    #create here the local history
    #add the bam like this throught the experiments
    currentuser= GalaxyUsers.objects.get(user_email=usermail)
    PlasmaWorkflow= Workflows.objects.get(name='Plasma_mutation')   
    local_history=GalaxyJobs(tag_id=id_generator(),
    history_user_email=currentuser,
    resultsName=experimentsDict['resultsName'],
    history_analyse_type=PlasmaWorkflow,
    progression="suspendu",
    history_download=False,
    galaxy_dictionnary=json.dumps(experimentsDict))
    logger.info("save the galaxyJobs object")    
    local_history.save()     
    for bamfileTag in myCheckedBam:
        try:
            currentBam=current_exp.experimentrawdata_set.get(ionTag=bamfileTag)
            local_history.list_experimentRawData.add(currentBam)
            bamtoDl.append(str(currentBam.bam_path))
            local_history.save()        
        except ExperimentRawData.DoesNotExist:
            currentBam = None
            logger.error("This bam is not not avaible in this experiment")     
        logger.info(str(currentBam.bam_path).split("/")[len(str(currentBam.bam_path).split("/"))-1])
        logger.info("##########################")
    logger.info("##########################")    
    logger.info("CREATE INPUT FOLDER FROM PROTON NAS_DIR/%s/bam",experimentsDict['resultsName'])     
    if not os.path.exists(nasInput+experimentsDict['resultsName']+plasmaFolderName):
        os.makedirs(nasInput+experimentsDict['resultsName']+plasmaFolderName)
        
    logger.info("CREATE RESULTS FOLDER FROM GALAXY NAS_DIR/RESULTS/%s/bam",experimentsDict['resultsName'])     
    if not os.path.exists(nasResults+experimentsDict['resultsName']+plasmaFolderName):
        os.makedirs(nasResults+experimentsDict['resultsName']+plasmaFolderName)
    local_history.save()    
    logger.info("##########################")
    logger.info("Connect to the server : %s",sequencer_severName )     
    logger.info("##########################")    
    logger.info("Copy data throught ssh and scp")
    logger.debug("##########################")        
    #~ logger.debug(experimentsDict['bammd5sum'])
    template = loader.get_template('sequencer/plasma_Main.html')    
    logger.debug("##########################")           
    logger.debug("go the GalaxyUser"+currentuser.user_email)   
    logger.debug("##########################")      
    bamFileName=bamtoDl
    context = {
        'current_exp': current_exp,
        #~ 'experimentsDict':experimentsDict['bamForPlasma'], #will print the bam absolut path
        'experimentsDict':bamFileName,
        #~ 'historyPlasma': "yoyo",
    }      
    #this tasks will be run before by celery as a baground event
    #~ tasks.task_Download_RawData() 
    return HttpResponse(template.render(context, request)) 


def getBamFromNgsData(request,experiment_name):
    logger.info("##########################")
    logger.info("START getBamFromNgsData() view")
    logger.info("##########################")
    logger.info("Check User autentification")
    usermail=request.user.username
    if request.user.is_authenticated():
        logger.debug("User logged as ; %s",request.user.username)
    else:
        logger.error("No user autentificated, No able to pursue further the analysis")
        
    logger.info("##########################")
    logger.info("Parsed User Request")
    logger.info("##########################")    
    UserRequest=request.POST
    myCheckedBam=[]
    for key, val in UserRequest.iteritems():
        #~ print str(key)
        #~ print str(val)
        if 'plasmaCheckboxValue' in str(key):
            myCheckedBam.append(str(val))
    current_exp = savedNGSData.objects.get(folder_name=experiment_name)
    #~ experimentsDict=json.loads(current_exp.dictionnary)
    bamtoDl=[]
    #create here the local history
    #add the bam like this throught the experiments
    currentuser= GalaxyUsers.objects.get(user_email=usermail)
    #~ PlasmaWorkflow= Workflows.objects.get(name='Plasma_mutation')   
    PlasmaSamtools= Workflows.objects.get(name='demo_samtools')   

    local_history=GalaxyJobs(tag_id=id_generator(),