def save_selection_dialog(self, mode="selection"): """ Save selection in a single file. """ # Builds the selection. if mode == "selection": selection = self.get_selected_sequences() elif mode == "all": selection = self.get_all_sequences() else: raise KeyError("Unknown 'mode': %s" % mode) # Ask users if they want to include indels in the sequences to save. remove_indels_choice = False for e in selection: if "-" in e.my_sequence: remove_indels_choice = self.ask_to_remove_indels() break # Ask users to choose a directory where to save the file. filepath = asksaveasfile_qt("Save FASTA file", name_filter="*.fasta", parent=self.get_qt_parent()) if not filepath: return None dirpath = os.path.dirname(filepath) filename = os.path.basename(filepath) self.build_sequence_file(selection, filename, file_format="fasta", remove_indels=remove_indels_choice, same_length=remove_indels_choice, new_directory=dirpath, add_extension=False)
def save_session_from_main_menu(self): save_project_full_path = asksaveasfile_qt("Save PyMod Session file", name_filter="*.pmse", parent=self.get_qt_parent()) if not save_project_full_path: return None self.save_pymod_session(save_project_full_path)
def sequence_save_dialog(self, element): """ Save a single sequence to a file. """ # Ask to remove indels. if "-" in element.my_sequence: remove_indels_choice = self.ask_to_remove_indels() else: remove_indels_choice = False # Choose the file path. filepath = asksaveasfile_qt("Save FASTA file", name_filter="*.fasta", parent=self.get_qt_parent()) if not filepath: return None # Actually saves the file. The file extension will not be added automatically. dirpath = os.path.dirname(filepath) filename = os.path.basename(filepath) self.build_sequence_file([element], filename, file_format="fasta", remove_indels=remove_indels_choice, new_directory=dirpath, add_extension=False)
def save_to_event(self): """ Saves the table data to a .csv file. """ # Let the user select the filepath. filepath = asksaveasfile_qt("Save CSV file", name_filter="*.csv") if not filepath: return None try: # Writes a .csv file on that path. with open(filepath, 'w') as csv_fh: writer = csv.writer(csv_fh, delimiter=',', quoting=csv.QUOTE_MINIMAL) if self.table.row_labels is not None: writer.writerow([" "] + self.table.column_labels) else: writer.writerow(self.table.column_labels) for row_idx, row in enumerate(self.table.data): if self.table.row_labels is not None: writer.writerow([self.table.row_labels[row_idx]] + [str(v) for v in row]) else: writer.writerow([str(v) for v in row]) except Exception as e: print("- WARNING: could not write a csv file: %s" % str(e))
def save_to_csv_event(self): output_filepath = asksaveasfile_qt("Save to CSV file", parent=self.pymod.get_qt_parent(), name_filter="*.csv") if not output_filepath: return None try: self._save_to_csv_file(output_filepath) except Exception as e: print("- WARNING: could not write a csv file: %s" % str(e))
def alignment_save_dialog(self, alignment_element): """ Lets the user choose the path to which an alignment file is going to be saved, and saves an alignment file there. """ save_file_full_path = asksaveasfile_qt( "Save an alignment file", name_filter="*.fasta;;*.aln;;*.sto", parent=self.get_qt_parent()) if not save_file_full_path: return None alignment_file_name, extension = os.path.splitext( os.path.basename(save_file_full_path)) extension = extension.replace(".", "") # The get all the aligned elements. aligned_elements = alignment_element.get_children() # Saves a file with all the sequences in the project "Alignments" directory. if extension == "fasta": self.save_alignment_fasta_file(alignment_file_name, aligned_elements) elif extension == "aln": self.build_sequence_file(aligned_elements, alignment_file_name, file_format="clustal", remove_indels=False) elif extension == "sto": self.build_sequence_file(aligned_elements, alignment_file_name, file_format="stockholm", remove_indels=False) # If the user didn't provide a valid extension. else: title = "Format Error" if extension != "": message = "Unknown alignment file extension: '%s'." % ( extension) else: message = "No alignment file extension provided." message += " Please provide a valid extension. Example: .fasta (FASTA), .aln (Clustal) or .sto (Stockholm)" self.main_window.show_error_message(title, message) return # Moves the saved file to the path chosen by the user. try: old_path = os.path.join(self.alignments_dirpath, alignment_file_name + "." + extension) os.rename(old_path, save_file_full_path) except Exception as e: title = "File Error" message = "Could not save the alignment file to path '%s' for the following reason: %s." % ( save_file_full_path, e) self.main_window.show_error_message(title, message)
def save_modeling_session(self, modeling_session): """ Build a zip file of the modeling directory of a certain session. """ archive_path = asksaveasfile_qt("Save PyMod Session file", name_filter="*.zip", parent=self.get_qt_parent()) if not archive_path: return None try: pmos.zip_directory(directory_path=os.path.join(self.models_dirpath, os.path.basename(modeling_session.modeling_directory_path)), zipfile_path=archive_path) except: title = "File Error" message = "Could not save the modeling session file to path: %s" % (archive_path) self.main_window.show_error_message(title, message)
def save_to_png_event(self): output_filepath = asksaveasfile_qt("Save to PNG file", parent=self.pymod.get_qt_parent(), name_filter="*.png") if not output_filepath: return None try: from pymod_lib.pymod_plot.pyqtgraph.exporters import ImageExporter # Create an exporter instance, as an argument give it the whole plot. exporter = ImageExporter(self.graphWidget.plotItem) # Save to file. exporter.export(output_filepath) except Exception as e: print("- WARNING: could not write a pgn file: %s" % str(e))
def save_pdb_chain_to_file_dialog(self, pymod_element): """ Save a PDB single chain to a file. """ # Choose the file path. filepath = asksaveasfile_qt("Save PDB file for this chain", name_filter="*.pdb", parent=self.get_qt_parent()) if not filepath: return None # Actually saves the file. try: if os.path.isfile(filepath): os.remove(filepath) shutil.copy(pymod_element.structure.current_chain_file_path, filepath) except Exception as e: title = "File Error" message = "Could not save the PDB chain file to path '%s' for the following reason: %s." % (filepath, e) self.main_window.show_error_message(title, message)