Ejemplo n.º 1
0
 def delete_sra(self):
     """Delete the downloaded SRA files.
     """
     if (pe.deleteFileFromDisk(self.localSRAFilePath)):
         del self.localSRAFilePath
         return True
     return False
Ejemplo n.º 2
0
 def delete_fastq(self):
     """Delte the fastq files from the disk.
     The files are referenced by self.localfastqPath or self.localfastq1Path and self.localfastq2Path
     """
     if self.layout=='PAIRED':
         if pe.deleteMultipleFilesFromDisk(self.localfastq1Path,self.localfastq2Path):
             del self.localfastq1Path
             del self.localfastq2Path
             return True
     else:
         if pe.deleteFileFromDisk(self.localfastqPath):
             del self.localfastqPath
             return True
     return False
Ejemplo n.º 3
0
    def run_portcullisFull(self,
                           reference_fasta,
                           bam_file,
                           out_dir="",
                           delete_bam=False,
                           verbose=False,
                           quiet=False,
                           logs=True,
                           objectid="NA",
                           **kwargs):
        """
        run portculis full
        
        Parameters
        ----------
        reference_fasta: string
            Path to the reference fasta file
        bam_file: string
            Path to input bam file
        out_dir: string
            Path to the out put dir. current directory is not given.
        
        verbose: bool
            Print stdout and std error
        quiet: bool
            Print nothing
        logs: bool
            Log this command to pyrpipe logs
        objectid: str
            Provide an id to attach with this command e.g. the SRR accession. This is useful for debugging, benchmarking and reports.
        kwargs: dict
            Options to pass to trimgalore. This will override the existing options 
        
        """

        if not pu.check_files_exist(reference_fasta, bam_file):
            print("Please check input for portcullis.")
            return ""

        newOpts = {"--": (reference_fasta, bam_file)}
        mergedOpts = {**kwargs, **newOpts}
        #add out dir path
        if not out_dir:
            out_dir = os.path.join(os.getcwd(), "portcullis_out")

        mergedOpts = {**mergedOpts, **{"-o": out_dir}}

        status = self.run_portcullis("full",
                                     verbose=verbose,
                                     quiet=quiet,
                                     logs=logs,
                                     objectid=objectid,
                                     **mergedOpts)

        if not status:
            print("portcullis full failed for:" + bam_file)
            return ""

        #check if bam file exists
        if not pu.check_paths_exist(out_dir):
            return ""

        if delete_bam:
            if not pe.deleteFileFromDisk(bam_file):
                print("Error deleting bam file:" + bam_file)

        return out_dir
Ejemplo n.º 4
0
    def sam_to_bam(self,
                   sam_file,
                   out_dir="",
                   out_suffix="",
                   delete_sam=False,
                   verbose=False,
                   quiet=False,
                   logs=True,
                   objectid="NA",
                   **kwargs):
        """Convert sam file to a bam file. 
        Output bam file will have same name as input sam.
        
        out_suffix: string
            Suffix for the output sam file
        delete_sam: bool
            delete the sam file after conversion
        verbose: bool
            Print stdout and std error
        quiet: bool
            Print nothing
        logs: bool
            Log this command to pyrpipe logs
        objectid: str
            Provide an id to attach with this command e.g. the SRR accession. This is useful for debugging, benchmarking and reports.
        kwargs: dict
            Options to pass to trimgalore. This will override the existing options 

        :return: Returns the path to the bam file. Returns empty string if operation failed.
        :rtype: string
        """
        if not out_dir:
            out_dir = pu.get_file_directory(sam_file)
        else:
            if not pu.check_paths_exist(out_dir):
                pu.mkdir(out_dir)

        fname = pu.get_file_basename(sam_file)

        #output will be out_bam
        out_bam = os.path.join(out_dir, fname + out_suffix + '.bam')

        newOpts = {"--": (sam_file, ), "-o": out_bam, "-b": ""}
        mergedOpts = {**kwargs, **newOpts}

        status = self.run_samtools("view",
                                   verbose=verbose,
                                   quiet=quiet,
                                   logs=logs,
                                   objectid=objectid,
                                   **mergedOpts)

        if not status:
            print("Sam to bam failed for:" + sam_file)
            return ""

        #check if bam file exists
        if not pu.check_files_exist(out_bam):
            return ""

        #delete_sam_file
        if delete_sam:
            if not pe.deleteFileFromDisk(sam_file):
                print("Error deleting sam file:" + sam_file)

        #return path to file
        return out_bam
Ejemplo n.º 5
0
    def merge_bam(self,
                  *args,
                  out_file="merged",
                  out_dir="",
                  delete_bams=False,
                  verbose=False,
                  quiet=False,
                  logs=True,
                  objectid="NA",
                  **kwargs):
        """Merge multiple bam files into a single file
        
        Parameters
        ----------
        out_file: string
            Output file name to save the results. .bam will be added at the end.
        args:tuple
            Paths to bam files to combine
        out_dir: string
            Path where to save the merged bam file. Default path is the same as the first bam_file's
        verbose: bool
            Print stdout and std error
        quiet: bool
            Print nothing
        logs: bool
            Log this command to pyrpipe logs
        objectid: str
            Provide an id to attach with this command e.g. the SRR accession. This is useful for debugging, benchmarking and reports.
        kwargs: dict
            Options to pass to trimgalore. This will override the existing options 

        :return: Returns the path to the merged bam file.
        :rtype: string
        """

        if len(args) < 2:
            print("Please supply at least 2 files to merge")
            return ""

        if not out_dir:
            out_dir = pu.get_file_directory(args[0])
        else:
            if not pu.check_paths_exist(out_dir):
                pu.mkdir(out_dir)

        outMergedFile = os.path.join(out_dir, out_file + ".bam")

        newOpts = {"--": (outMergedFile, ) + args}

        mergedOpts = {**kwargs, **newOpts}

        status = self.run_samtools("merge",
                                   verbose=verbose,
                                   quiet=quiet,
                                   logs=logs,
                                   objectid=objectid,
                                   **mergedOpts)

        if not status:
            print("Bam merge failed for:" + outMergedFile)
            return ""

        #check if bam file exists
        if not pu.check_files_exist(outMergedFile):
            return ""

        if delete_bams:
            for bam_file in args:
                if not pe.deleteFileFromDisk(bam_file):
                    print("Error deleting sam file:" + bam_file)

        return outMergedFile
Ejemplo n.º 6
0
    def sort_bam(self,
                 bam_file,
                 out_dir="",
                 out_suffix="",
                 threads=None,
                 delete_bam=False,
                 verbose=False,
                 quiet=False,
                 logs=True,
                 objectid="NA",
                 **kwargs):
        """Sorts an input bam file. Outpufile will end in _sorted.bam
        bam_file: str
            Path to the input bam file
        out_dir: str
            Path to output directory
        out_suffix: str
            Output file suffix
        threads: int
            Number of threads. Default: Use self.threads initialized in init().
        delete_bam: bool
            Delete input bam_file
        verbose: bool
            Print stdout and std error
        quiet: bool
            Print nothing
        logs: bool
            Log this command to pyrpipe logs
        objectid: str
            Provide an id to attach with this command e.g. the SRR accession. This is useful for debugging, benchmarking and reports.
        kwargs: dict
            Options to pass to samtools. This will override the existing options 

        :return: Returns path to the sorted bam file. Returns empty string if operation failed.
        :rtype: string
        
        """
        if not out_dir:
            out_dir = pu.get_file_directory(bam_file)
        else:
            if not pu.check_paths_exist(out_dir):
                pu.mkdir(out_dir)

        fname = pu.get_file_basename(bam_file)
        #output will be out_bam
        outSortedbam_file = os.path.join(out_dir,
                                         fname + out_suffix + '_sorted.bam')

        #handle threads
        if not threads:
            threads = self.threads

        newOpts = {
            "--": (bam_file, ),
            "-o": outSortedbam_file,
            "-@": str(threads)
        }
        mergedOpts = {**newOpts, **kwargs}

        status = self.run_samtools("sort",
                                   verbose=verbose,
                                   quiet=quiet,
                                   logs=logs,
                                   objectid=objectid,
                                   **mergedOpts)

        if not status:
            print("Bam sort failed for:" + bam_file)
            return ""

        #check if bam file exists
        if not pu.check_files_exist(outSortedbam_file):
            return ""

        if delete_bam:
            if not pe.deleteFileFromDisk(bam_file):
                print("Error deleting sam file:" + bam_file)

        #return path to file
        return outSortedbam_file