Ejemplo n.º 1
0
Archivo: Quade.py Proyecto: mablt/Quade
    def __init__(self, conf_file=None, init_conf=None):
        """
        Initialization function, parse options from configuration file and verify their values.
        All self.variables are initialized explicitly in init.
        """

        # Create a example conf file if needed
        if init_conf:
            print("Create an example configuration file in the current folder")
            write_example_conf()
            sys.exit(0)

        print("Initialize Quade")
        # Parse the configuration file and verify the values of variables
        try:

            # Verify if conf file was given and is valid
            assert conf_file, "A path to the configuration file is mandatory"
            self._is_readable_file(conf_file)
            self.conf = conf_file

            # Define a configuration file parser object and load the configuration file
            cp = ConfigParser.RawConfigParser(allow_no_value=True)
            cp.read(self.conf)

            # Quality section
            self.minimal_qual = cp.getint("quality", "minimal_qual")

            # Boolean flag of subindex presence
            self.idx1 = True
            self.idx2 = cp.getboolean("index", "index2")
            self.mol1 = self.idx1 and cp.getboolean("index", "molecular1")
            self.mol2 = self.idx2 and cp.getboolean("index", "molecular2")

            # Positions of subindex
            self.idx1_pos = {
                "start":cp.getint("index", "index1_start")-1,
                "end":cp.getint("index", "index1_end")}
            self.idx2_pos = {
                "start":0 if not self.idx2 else cp.getint("index", "index2_start")-1,
                "end":0 if not self.idx2 else cp.getint("index", "index2_end")}
            self.mol1_pos = {
                "start":0 if not self.mol1 else cp.getint("index", "molecular1_start")-1,
                "end":0 if not self.mol1 else cp.getint("index", "molecular1_end")}
            self.mol2_pos = {
                "start":0 if not self.mol2 else cp.getint("index", "molecular2_start")-1,
                "end":0 if not self.mol2 else cp.getint("index", "molecular2_end")}

            # List of fastq files
            self.seq_R1 = cp.get("fastq", "seq_R1").split()
            self.seq_R2 = cp.get("fastq", "seq_R2").split()
            self.index_R1 = cp.get("fastq", "index_R1").split()
            self.index_R2 = [] if not self.idx2 else cp.get("fastq", "index_R2").split()

            # Init the class Sample with generic values (unconventional initialization)
            Sample.CLASS_INIT(
                write_undetermined = cp.getboolean("output", "write_undetermined"),
                write_pass = cp.getboolean("output", "write_pass"),
                write_fail = cp.getboolean("output", "write_fail"),
                min_qual = self.minimal_qual)

            # Samples are a special case, since the number of sections is variable
            # Iterate only on sections starting by "sample"
            for sample in [i for i in cp.sections() if i.startswith("sample")]:

                # Create a autoreferenced Sample object and fuse index if needed
                if self.idx2:
                    Sample(name=cp.get(sample, "name"), index=(cp.get(sample, "index1_seq")+cp.get(sample, "index2_seq")))
                else:
                    Sample(name=cp.get(sample, "name"), index=cp.get(sample, "index1_seq"))

            # Values are tested in a private function
            self._test_values()

        # Handle the many possible errors occurring during conf file parsing or variable test
        except (ConfigParser.NoOptionError, ConfigParser.NoSectionError) as E:
            print ("Option or section missing. Report to the template configuration file\n" + E.message)
            sys.exit(1)
        except (ValueError, AssertionError) as E:
            print ("One of the value in the configuration file is not correct\n" + E.message)
            sys.exit(1)
        except (IOError) as E:
            print ("One of the file is incorrect or unreadable\n" + E.message)
            sys.exit(1)