From 42746925519a4df2313191b36793a5bab122ae6c Mon Sep 17 00:00:00 2001
From: Cyril  NERIN <cnerin@pasteur.fr>
Date: Tue, 27 Jul 2021 17:14:11 +0200
Subject: [PATCH] FIX #92 FEAT #93 FEAT #94

---
 jass/models/project.py | 80 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)

diff --git a/jass/models/project.py b/jass/models/project.py
index 15a1eb54..bf2cf535 100644
--- a/jass/models/project.py
+++ b/jass/models/project.py
@@ -5,6 +5,8 @@ compute joint statistics and generate plots for a given set of phenotypes
 from __future__ import absolute_import
 from typing import List
 import os
+import sys
+import shutil
 import hashlib
 
 from jass.models.base_model_ import Model
@@ -15,6 +17,7 @@ from jass.models.worktable import (
     get_worktable_genomedata,
     get_worktable_local_manhattan_data,
     get_worktable_local_heatmap_data,
+    get_annotation_gene_exon_data,
 )
 
 from jass.config import config
@@ -150,6 +153,13 @@ class Project(Model):
         """
         return os.path.join(self.get_folder_path(), "workTable.hdf5")
 
+    def get_inittable_path(self):
+        """
+        get_inittable_path
+        Gets the path of the file initTable.hdf5
+        """
+        return os.path.join(config["DATA_DIR"], "initTable.hdf5")
+
     def get_csv_path(self):
         """
         get_csv_path
@@ -193,6 +203,15 @@ class Project(Model):
             self.get_worktable_path(), chromosome, region
         )
 
+    def get_project_gene_exon_data(
+        self, chromosome: str = None, start: str = None, end: str = None
+    ):
+        return get_annotation_gene_exon_data(
+            self.get_worktable_path(),
+            self.get_inittable_path(),
+            chromosome, start, end
+        )
+
     def get_id(self, chromosome=None, start=None, end=None):
         m = hashlib.md5()
         for phenotype_id in [phenotype.id for phenotype in self._phenotypes]:
@@ -275,6 +294,64 @@ class Project(Model):
         print("csv_file_generation:csv_file_status={}".format(csv_file_status))
         return csv_file_status
 
+    def get_error_msg(self):
+        fileExt = r"_MSG.log"
+        the_folder = self.get_folder_path()
+        list_file = [os.path.join(the_folder, fichier) \
+        for fichier in os.listdir(the_folder) if fichier.endswith(fileExt)]
+        if (len(list_file) == 0):
+            the_message = " "
+        else:
+            for fichier in list_file:
+                the_message = "\n "
+                with open(fichier, 'r') as filin:
+                    lignes = filin.readlines()
+                    for ligne in lignes:
+                        the_message += "{}".format(ligne)
+        
+        return the_message
+
+    def get_dispo_gene_exon(self):
+        return self.get_project_functional_state("ERROR_GENE_EXON_MSG.log")
+
+    def get_project_functional_state(self, detail=None):
+        the_folder = self.get_folder_path()
+        if (detail == None):
+            fileExt = r"_MSG.log"
+            list_file = [os.path.join(the_folder, fichier) \
+            for fichier in os.listdir(the_folder) if fichier.endswith(fileExt)]
+            
+            if (len(list_file) == 0):
+                the_state = Project.READY
+            else:
+                the_state = Project.ERROR
+        else:
+            if os.path.isfile(os.path.join(the_folder, detail)):
+                the_state = Project.ERROR
+            else:
+                the_state = Project.READY
+        return the_state
+
+    def get_initialized(self, to_init="0"):
+        print("project::get_initialized({})".format(to_init))
+        P_state = self.status
+        if ((P_state != Project.DOES_NOT_EXIST) and (to_init != "0")):
+            print("suppression si erreur")
+            # 
+            F_state = self.get_project_functional_state()
+            
+            # if an error occurred during a previous execution, 
+            # the project files are deleted to force the creation 
+            # of a new project
+            
+            if ((F_state == Project.ERROR) or (P_state == Project.ERROR)):
+                try:
+                    shutil.rmtree(self.get_folder_path())
+                except OSError as e:
+                    print(e, file=sys.stderr)
+        
+        return self.status
+
 
 def get_file_building_tb_path(file_path):
     return file_path + ".log"
@@ -287,3 +364,6 @@ def get_file_status(file_path):
         return Project.ERROR
     else:
         return Project.CREATING
+
+
+    
-- 
GitLab