Beispiel #1
0
    def search_sra(self, path):
        """Search .sra file under a dir
        Return True if found otherwise False
        """
        #search files under the path

        sra_files = pe.find_files(path, "*.sra")

        if len(sra_files) < 1:
            return False

        if len(sra_files) > 1:
            pu.print_boldred(
                "Found multiple .sra files. Using the first entry...")
        sra_path = sra_files[0]
        #self.location=path
        self.srr_accession = pu.get_file_basename(sra_path)
        self.localSRAFilePath = sra_path
        self.sraFileSize = pu.get_file_size(self.localSRAFilePath)
        #test if file is paired or single end
        if pe.is_paired(self.localSRAFilePath):
            self.layout = "PAIRED"
        else:
            self.layout = "SINGLE"

        pu.print_green("Found .sra " + self.localSRAFilePath)
        return True
Beispiel #2
0
    def init_from_accession(self, srr_accession, location):
        """Create SRA object using provided srr accession and location to save the data
        """
        self.dep_list = ['prefetch', "fasterq-dump"]
        if not pe.check_dependencies(self.dep_list):
            raise Exception("ERROR: Please install missing programs.")

        if srr_accession is None:
            raise Exception("Please provide a valid accession")

        if location is None:
            location = os.getcwd()
        #pu.print_info("Creating SRA: "+srr_accession)
        self.srr_accession = srr_accession
        #create a dir named <srr_accession> and use as location
        self.location = os.path.join(location, self.srr_accession)

        #search for existing files in location
        #self.search_fastq(self.location)
        #scan path for sra
        #self.search_sra(self.location)

        #check SRA file
        if pu.check_files_exist(
                os.path.join(self.location, self.srr_accession + ".sra")):
            pu.print_green(self.srr_accession + ".sra exists.")
            self.localSRAFilePath = os.path.join(self.location,
                                                 self.srr_accession + ".sra")
            self.sraFileSize = pu.get_file_size(self.localSRAFilePath)
            #test if file is paired or single end
            if pe.is_paired(self.localSRAFilePath):
                self.layout = "PAIRED"
            else:
                self.layout = "SINGLE"

        #check fastq file
        self.search_fastq(self.location)
Beispiel #3
0
    def download_sra(self, verbose=False, quiet=False, logs=True, **kwargs):
        """This function downloads .sra file from NCBI SRA servers using the prefetch command.

        NCBI sra-toolkit 2.9 or higher must be installed on the system in order to use prefetch. 
        prefetch will create a folder with name same as <srr_accession> under the location (path) specified.
        The path of downloaded file is saved in the object as localSRAPath. This localSRAPath is then used
        by other functions to access the downloaded data. 
        The **kwargs is for passing arguments to the prefetch command.
        
        Parameters
        ----------
        
        kwargs: dict
            dict containing additional prefetch arguments

        :return: Return status of the prefetch command. True if successful download and False if failed.
        :rtype: bool

        Examples
        --------
        >>> object.download_sra()
        True
        """

        #store path to the downloaded sra file
        self.localSRAFilePath = os.path.join(self.location,
                                             self.srr_accession + ".sra")
        #check if already exists
        if pu.check_files_exist(self.localSRAFilePath):
            pu.print_green("File already exists:" + self.localSRAFilePath)
            #save file .sra file size
            self.sraFileSize = pu.get_file_size(self.localSRAFilePath)
            #test if file is paired or single end
            if pe.is_paired(self.localSRAFilePath):
                self.layout = "PAIRED"
            else:
                self.layout = "SINGLE"
            return True

        pu.print_info("Downloading " + self.srr_accession + " ...")

        #scan for prefetch arguments
        prefetchArgsList = [
            '-f', '-t', '-l', '-n', '-s', '-R', '-N', '-X', '-o', '-a',
            '--ascp-options', '-p', '--eliminate-quals', '-c', '-o', '-O',
            '-h', '-V', '-L', '-v', '-q'
        ]

        #ignore location and file name arguments if given
        if '-O' in kwargs:
            print("Ignoring -O flag." + " location is: " + self.location)
            #delete -O parameter
            del kwargs['-O']
        if '-o' in kwargs:
            print("Ignoring -o flag." + " File name is: " + self.srr_accession)
            #delete -o parameter
            del kwargs['-o']

        prefetch_Cmd = ['prefetch']
        prefetch_Cmd.extend(pu.parse_unix_args(prefetchArgsList, kwargs))
        prefetch_Cmd.extend(['-O', self.location])
        prefetch_Cmd.append(self.srr_accession)

        cmdStatus = pe.execute_command(prefetch_Cmd,
                                       objectid=self.srr_accession)
        if not cmdStatus:
            pu.print_boldred("prefetch failed for:" + self.srr_accession)
            return False

        #validate path exists
        if not pu.check_files_exist(self.localSRAFilePath):
            pu.print_boldred("Error downloading file. File " +
                             self.localSRAFilePath + " does not exist!!!")
            return False

        print("Downloaded file: " + self.localSRAFilePath +
              " {0} ".format(pu.get_file_size(self.localSRAFilePath)))
        #save file .sra file size
        self.sraFileSize = pu.get_file_size(self.localSRAFilePath)
        #test if file is paired or single end
        if pe.is_paired(self.localSRAFilePath):
            self.layout = "PAIRED"
        else:
            self.layout = "SINGLE"

        return True