def save_defaults(self, overwrite=False):
        """Saves species in the right display box to a user specified species text file."""
        desired_file = self.selected_default
        if not overwrite:
            print('should be asking for save file name')
            desired_file = fasta_lib.save_file(self.script_path, [('Text files', '*.txt')],
                                               default_file=os.path.split(self.selected_default)[1],
                                               title_string='Specify a default species file name')
        if desired_file:
            try:
                # write default species list to file
                items = self.tree_right.get_children()
                databases = [self.tree_right.item(item)['values'] for item in items]
                for database in databases:
                    database[-1] = database[-1].rstrip(r"""\'"*""") # seem to accumulate EOL characters
                
                # Remove duplicates
                db_set = set(tuple(x) for x in databases)
                databases = sorted([list(x) for x in db_set], key=lambda y: int(y[0])) # sort DBs by taxon

                with open(desired_file, "w") as defaults_txt:
                    self.selected_default = desired_file
                    for database in databases:
                        defaults_txt.write("{}\n".format(database))

                self.status_bar.config(text="Databases saved to species text file")
            except OSError:
                messagebox.showwarning("Invalid Filename!", "Cannot save species list to selected folder!")
Exemplo n.º 2
0
# control flags
KEEP_CONTAMS = False

# get the files, etc.
list_file_name = fasta_lib.get_file(os.getcwd(), [('Text files', '*.txt')],
                                    'Browse to accession list text file')
if not list_file_name: sys.exit()
results_location = os.path.split(list_file_name)[0]
database_name = fasta_lib.get_file(r'C:\Xcalibur\database',
                                   [('FASTA files', '*.fasta')],
                                   'Select the database')
if not database_name: sys.exit()
new_name = os.path.split(database_name)[1]
new_name = os.path.splitext(new_name)[0]
subset_DB_name = fasta_lib.save_file(results_location,
                                     [('FASTA files', '*.fasta')],
                                     default_file=new_name + '_subset.fasta',
                                     title_string='Name of subset database')
if not subset_DB_name: sys.exit()
if os.path.splitext(subset_DB_name)[1] == '':
    subset_DB_name += '.fasta'

# open the accession list file and save accessions in dictionary
print('=======================================================')
print(' make_subset_DB_from_list.py, Phil Wilmarth, OHSU 2018 ')
print('=======================================================')
print('Processing accessions file:', time.ctime())
IDs = {}
read = 0
for acc in open(list_file_name, 'r'):
    # this assumes accessions from PAW pipeline results files
    try:
            database = os.getcwd()

        print('Select the FASTA file with extra sequences')
        extra_file = fasta_lib.get_file(database,
                                        [('FASTA files', '*.fasta'), ('All files', '*.*')],
                                        'Select Extra Sequences (FASTA format)')
        if extra_file == '': sys.exit() # cancel button response

        extra_name = os.path.split(extra_file)[1]
        extra_name = extra_name.split('.fasta')[0]
        print('Select the main FASTA file')
        fasta_file = fasta_lib.get_file(database, [('FASTA files', '*.fasta'),
                                                   ('GZipped files', '*.gz'),
                                                   ('All files', '*.*')],
                                        'Select FASTA database file')
        if fasta_file == '': sys.exit() # cancel button response

        default = os.path.split(fasta_file)[0]
        fasta_name = os.path.split(fasta_file)[1]
        default_file = extra_name + '_' + fasta_name
        print('Select location and name for new FASTA database')
        output_file = fasta_lib.save_file(default, [('FASTA files', '*.fasta'),
                                                    ('All files', '*.*')],
                                          default_file, 'Output filename and location')
        if output_file == '': sys.exit() # cancel button response

    # call add extras and reverse function
    fasta_add_extras(extra_file, fasta_file, output_file)

# end