Example #1
0
 def exec_mapping_stage(self):
     """Executor of the Wedring pipeline."""
     if self.quiet:
         wedr_check_path(self.index + '.*')
         self.exec_mapping()
         self.aln_file = (path.join(self._mapper_out, "accepted_hits.bam") if
                          self.mapper == "tophat" else
                          self._mapper_out + ".sam")
         wedr_check_path(self.aln_file)
         if wedr_check_program("samtools"):
             self.exec_samtools()
             if wedr_check_program("bedtools"):
                 self.exec_bedtools()
     else:
         wedr_check_path(self.index + '.*')
         self.exec_mapping()
         self.aln_file = (path.join(self._mapper_out, "accepted_hits.bam") if
                          self.mapper == "tophat" else
                          self._mapper_out + ".sam")
         wedr_check_path(self.aln_file)
         if wedr_check_program("samtools"):
             wedr_report("[%s] Processing aligments with SAMtools." %
                         self._out_pref)
             self.exec_samtools()
             wedr_report("[%s] SAMtools - Done!." % self._out_pref)
             if wedr_check_program("bedtools"):
                 wedr_report("[%s] Calculating mapping coverage with BEDTools." %
                             self._out_pref)
                 self.exec_bedtools()
                 wedr_report("[%s] BEDTools - Done!" % self._out_pref)
     return self
Example #2
0
    def parse_args(self, out_dir="./wedr_index", ref_seq=None, index_label=None,
                   cfg_file=None, quiet=False):
        """This method checks the validity of :class:IndexBuilder parameters.


        :param out_dir: Which output directory? (default: ./wedr_out)
        :type out_dir: str
        :param ref_seq: Which reference sequence(s)?
        :type ref_seq: str
        :param index_label: A personalized name to the index.
        :type index_label: str
        :param cfg_file: Which configuration file?
        :type cfg_file: str
        :param quiet: If True don't print anything besides errors.
        :type quiet: bool
        :raises: :class:WedringError 

        """
        if wedr_check_program("bowtie-build"):
            if quiet:
                self.quiet = True
            if out_dir != "./wedr_index":
                self.out_dir = out_dir
            if cfg_file is not None:
                if wedr_check_path(cfg_file):
                    self.cfg_file = cfg_file
            if ref_seq is not None:
                if wedr_check_path(self.ref_seq.split(',')):
                    self.ref_seq = ref_seq
            else:
                raise WedringError(135, "You must provide the reference sequence file(s).")
            if index_label is not None:
                self.index = path.join(self.out_dir, index_label)
                self._out_pref = index_label
            elif ',' in self.ref_seq:
                index_name = [i for i in self.ref_seq.split(',') if i != ''][0]
                self.index = path.join(self.out_dir,
                                       path.split(path.splitext(index_name)[0])[1] +
                                                    "_andothers")
                self._out_pref = index_name
            else:
                self._out_pref = path.split(path.splitext(self.ref_seq)[0])[1]
                self.index = path.join(self.out_dir, self._out_pref)
Example #3
0
    def parse_args(self, out_dir="./wedr_out", mapper="bowtie", index=None,
                   lib_file=None, lib_mate_1=None, lib_mate_2=None,
                   qual_file=None, q_mate_1=None, q_mate_2=None, annot_file=None,
                   cov_file=None, cfg_file=None, map_label=None, quiet=False,
                   barrier=0):
        """This method verifies if **Wedring** parameters are set correctly and
        makes other adjustments.

        :param out_dir: Which output directory? (default: ./wedr_out)
        :type out_dir: str -- Pathname to an existing (or not) directory.
        :param mapper: Which mapper? (default: bowtie)
        :type mapper: str -- "bowtie" or "tophat"
        :param index: Which BW index?
        :type index: str -- Pathname to *Bowtie*'s index (just the suffix)
        :param lib_file: Which library file(s)?
        :type lib_file: str
        :param lib_mate_1: Which first mate reads file?
        :type lib_mate_1: str
        :param lib_mate_2: Which second mate reads file?
        :type lib_mate_2: str
        :param qual_file: Which quality file(s)?
        :type qual_file: str
        :param q_mate_1: Which quality file(s) for mate 1?
        :type q_mate_1: str
        :param q_mate_2: Which quality file(s) for mate 2?
        :type q_mate_2: str
        :param annot_file: Which annotation file?
        :type q_mate_2: str
        :param cfg_file: Which configuration file?
        :type cfg_file: str
        :param map_label: A personalized name to the mapping.
        :type map_label: str
        :param barrier: When the pipeline will stop?
        :type barrier: int
        :param quiet: If True don't print anything besides errors.
        :type quiet: bool
        :raises: :class:WedringError

        """
        if self.wedr_barrier != 3:
            mp = mapper.lower()
            if mp in ("bowtie", "tophat"):
                if wedr_check_program(mp):
                    self.mapper = mp
            else:
                raise WedringError(134, "Invalid mapper name: %s" % mapper)
            if quiet:
                self.quiet = True
            if out_dir != "./wedr_out":
                self.out_dir = out_dir
                self.log_dir = path.join(out_dir, "log")
            else:
                self.log_dir = path.join(out_dir, "log")
            if cfg_file is not None:
                if wedr_check_path(cfg_file):
                    self.cfg_file = cfg_file
            if index is not None:
                if wedr_check_path(index + ".*"):
                    self.index = index
            else:
                raise WedringError(135, "You must provide Bowtie's BW index.")
            if lib_file is not None:
                lib_temp = [lf for lf in lib_file.split(',') if lf != '']
                if wedr_check_path(lib_temp):
                    self.lib_file = ','.join(lib_temp)
                    self._map_mode = 2
                if qual_file is not None:
                    qual_temp = [qf for qf in qual_file.split(',') if qf != '']
                    if wedr_check_path(qual_temp):
                        if len(lib_temp) != len(qual_temp):
                            raise WedringError(140, "Unbalanced number of library and quality files.")
                        self.qual_file = qual_file
                        self._map_mode += 5
                else:
                    self._map_mode += 6
            elif lib_mate_1 is not None and lib_mate_2 is not None:
                lib_1_temp = [l1 for l1 in lib_mate_1.split(',') if l1 != '']
                lib_2_temp = [l2 for l2 in lib_mate_2.split(',') if l2 != '']
                if wedr_check_path(lib_1_temp + lib_2_temp):
                    if len(lib_1_temp) != len(lib_2_temp):
                        raise WedringError(140, "Unbalanced number of pair-mate libraries.")
                    if q_mate_1 is not None and q_mate_2 is not None:
                        qual_1_temp = [q1 for q1 in q_mate_1.split(',') if q1 != '']
                        qual_2_temp = [q2 for q2 in q_mate_2.split(',') if q2 != '']
                        if wedr_check_path(qual_1_temp + qual_2_temp):
                            if (len(lib_1_temp) != len(qual_1_temp) or
                                len(lib_1_temp) != len(qual_2_temp)):
                                raise WedringError(140, "Unbalanced number of pair-mate libraries and its qualities.")
                            self.lib_mate_1 = ','.join(lib_1_temp)
                            self.lib_mate_2 = ','.join(lib_2_temp)
                            self.q_mate_1 = ','.join(qual_1_temp)
                            self.q_mate_2 = ','.join(qual_2_temp)
                            self._map_mode = 4
                    elif (q_mate_1 is None) ^ (q_mate_2 is None):
                        raise WedringError(135, "You must set both pair-mate quality files.")
                    self.lib_mate_1 = ','.join(lib_1_temp)
                    self.lib_mate_2 = ','.join(lib_2_temp)
                    self._map_mode = 5
            elif (lib_mate_1 is None) ^ (lib_mate_2 is None):
                raise WedringError(135, "You must set both pair-mate library files.")
            else:
                raise WedringError(135, "You must provide the library file(s).")
            if map_label is not None:
                self._mapper_out = path.join(self.out_dir, map_label)
                self._out_pref = map_label
            elif 7 != self._map_mode != 8:
                if ',' in self.lib_mate_1:
                    map_out_pref = (path.split(path.splitext(self.lib_mate_1[:self.lib_mate_1.index(',')])[0])[1],
                                    path.split(path.splitext(self.lib_mate_2[:self.lib_mate_2.index(',')])[0][1]))
                    if map_out_pref[0][:-2] == map_out_pref[1][:-2]:
                        self._mapper_out = (path.join(self.out_dir,
                                                      map_out_pref[0] +
                                                      "_andothers_vs_" +
                                                      path.split(self.index)[1]))
                        self._out_pref = map_out_pref[0]
                    else:
                        self._mapper_out = (path.join(self.out_dir,
                                                      map_out_pref[0][:-2] + '_' +
                                                      map_out_pref[1][:-2] +
                                                      "_andothers_vs_" +
                                                      path.split(self.index)[1]))
                        self._out_pref = (map_out_pref[0][:-2] + '_' +
                                         map_out_pref[1][:-2] + "_andothers_vs_")
                
                else:
                    map_out_pref = (path.split(path.splitext(self.lib_mate_1)[0])[1],
                                    path.split(path.splitext(self.lib_mate_2)[0])[1])
                    if map_out_pref[0][:-2] == map_out_pref[1][:-2]:
                        self._mapper_out = (path.join(self.out_dir,
                                                       map_out_pref[0][:-2] + "_vs_" +
                                                       path.split(self.index)[1]))
                        self._out_pref = map_out_pref[0][:-2]
                    else:
                        self._mapper_out = (path.join(self.out_dir,
                                                      map_out_pref[0][:-2] + '_' +
                                                      map_out_pref[1][:-2] + "_vs_" +
                                                      path.split(self.index)[1]))
                        self._out_pref = (map_out_pref[0][:-2] + '_' +
                                          map_out_pref[1][:-2])
            else:
                if ',' in self.lib_file:
                    map_out_pref = path.split(path.splitext(self.lib_file[:self.lib_file.index(',')])[0])[1]
                    self._mapper_out = (path.join(self.out_dir, map_out_pref +
                                                  "_andothers_vs_" +
                                                  path.split(self.index)[1]))
                    self._out_pref = map_out_pref + "_andothers_vs_"
                else:
                    map_out_pref = path.split(path.splitext(self.lib_file)[0])[1]
                    self._mapper_out = (path.join(self.out_dir, map_out_pref +
                                                  "_vs_" +
                                                  path.split(self.index)[1]))
                    self._out_pref = map_out_pref
            if annot_file is not None:
                if wedr_check_path(annot_file):
                    self.annot_file = annot_file
            else:
                raise WedringError(135, "You must provide the annotation file.")
        else:
            if annot_file is not None:
                if wedr_check_path(annot_file):
                    self.annot_file = annot_file
            else:
                raise WedringError(135, "You must provide the annotation file.")
            if cov_file is not None:
                if wedr_check_path(cov_file):
                    self.cov_file = cov_file
            else:
                raise WedringError(135, "You must provide the coverage file.")