def print_file( type: ESiteType, file: isFilename[EFileMode.READ, __EXT_FASTA] ) -> EChanges: """ Prints a FASTA file in colour :param type: Type of sites to display. :param file: Path to FASTA file to display. """ text = file_helper.read_all_text( file ) pr.pr_information( cli_view_utils.colour_fasta_ansi( text, type ) ) return EChanges.NONE
def blastp(fasta: str) -> str: """ Uses protein blast to create the similarity matrix. """ file_helper.write_all_text("fasta.fasta", fasta) subprocess_helper.run_subprocess([ "blastp", "-query", "fasta.fasta", "-subject", "fasta.fasta", "-outfmt", "6", "-out", "blast.blast" ]) return file_helper.read_all_text("blast.blast")
def align_muscle(model: Model, fasta: str) -> str: """ Uses MUSCLE to align. """ ignore(model) file_helper.write_all_text("in_file.fasta", fasta) subprocess_helper.run_subprocess( ["muscle", "-in", "in_file.fasta", "-out", "out_file.fasta"]) return file_helper.read_all_text("out_file.fasta")
def __on_current_index_changed(self, _: int): data = self.ui.CMB_FILES.currentData(Qt.UserRole) if data: try: text = file_helper.read_all_text(data) self.ui.TXT_DATA.setText(text) except Exception as ex: self.ui.TXT_DATA.setText(str(ex)) else: self.ui.TXT_DATA.setText("(no selection)")
def tree_neighbor_joining(model: str, alignment: str) -> str: """ Uses PAUP to generate the tree using neighbour-joining. There are some major issues with Paup. Please see the troubleshooting section of Groot's readme. :param model: Format, a string `n` or `p` denoting the site type. :param alignment: Alignment in FASTA format. :return: The tree in Newick format. """ # TODO: Use an alternative that doesn't have the PAUP time-out problem. file_helper.write_all_text("in_file.fasta", alignment) script = """ toNEXUS format=FASTA fromFile=in_file.fasta toFile=in_file.nexus dataType=protein replace=yes; execute in_file.nexus; NJ; SaveTrees file=out_file.nwk format=Newick root=Yes brLens=Yes replace=yes; quit;""" if model == "n": site_type = "nucleotide" elif model == "p": site_type = "protein" else: raise SwitchError("model", model) script = script.format(site_type) file_helper.write_all_text("in_file.paup", script) txt = groot.run_subprocess(["paup", "-n", "in_file.paup"], collect=True, no_err=True) # The return code seems to have no bearing on Paup's actual output, so ignore it and look for the specific text. if "This version of PAUP has expired." in txt: raise ValueError( "'This version of PAUP has expired'. Please update your software or use a different method and try again." ) r = file_helper.read_all_text("out_file.nwk", details="the expected output from paup") if not r: raise ValueError("Paup produced an empty file.") return r
def supertree_clann(inputs: str) -> str: """ Uses CLANN to generate a supertree. :param inputs: Input trees in Newick format. :return: The consensus supertree in Newick format. """ file_helper.write_all_text("in_file.nwk", inputs) script = """ execute in_file.nwk; hs savetrees=out_file.nwk; quit """ subprocess_helper.run_subprocess(["clann"], stdin=script) result = file_helper.read_all_text("out_file.nwk") return result.split(";")[0]
def tree_maximum_likelihood(model: str, alignment: str) -> str: """ Uses Raxml to generate the tree using maximum likelihood. The model used is GTRCAT for RNA sequences, and PROTGAMMAWAG for protein sequences. """ file_helper.write_all_text("in_file.fasta", alignment) bio_helper.convert_file("in_file.fasta", "in_file.phy", "fasta", "phylip") if model == "n": method = "GTRCAT" elif model == "p": method = "PROTGAMMAWAG" else: raise SwitchError("model", model) groot.run_subprocess( "raxml -T 4 -m {} -p 1 -s in_file.phy -# 20 -n t".format(method).split( " ")) return file_helper.read_all_text("RAxML_bestTree.t", "the expected output from raxml")
def run_subprocess( *args, collect: bool = False, **kwargs ) -> Union[str, int]: """ Runs a subprocess as for `intermake.subprocess_helper.run_subprocess`, however stdout/stderr is sent to a file. :param args: Passed through to `intermake` :param collect: When `True` the contents of stdout/stderr are returned. When `False` the exit code is returned. :param kwargs: Passed through to `intermake` :return: """ with open( "temp.io", "w" ) as tmp: def tmpc( x ): tmp.write( x ) tmp.write( "\n" ) # The return code seems to have no bearing on Paup's actual output, so ignore it. exit_code = subprocess_helper.run_subprocess( *args, **kwargs, collect = tmpc ) if collect: return file_helper.read_all_text( "temp.io" ) else: return exit_code
def run_test(name: str) -> groot.EChanges: """ Runs a test case and saves the results to the global results folder. :param name: A name or path to the test case. If no full path is provided the "samples" folder will be assumed. The test case folder must contain: * The data (BLAST, FASTA) * A `tree.csv` file describing the expected results (in edge-list format) * A `groot.ini` file describing the parameters to use. :return: Nothing is returned, the results are saved to the global results folder. """ # Load sample file tdir = TestDirectory(name) # Define outputs file_helper.create_directory(tdir.r_folder, overwrite=True) # Check the requisite files exist if not os.path.isdir(tdir.t_folder): raise ValueError( "This is not a test case (it is not even a folder, «{}»).".format( tdir.t_folder)) if not os.path.isfile(tdir.t_tree): raise ValueError( "This is not a test case (it is missing the edge list file, «{}»)." .format(tdir.t_tree)) if not os.path.isfile(tdir.t_ini): raise ValueError( "This is not a test case (it is missing the INI file, «{}»).". format(tdir.t_ini)) # Read the test specs specs = io_helper.load_ini(tdir.t_ini) if "groot_test" not in specs: raise ValueError( "This is not a test case (it is missing the `groot_test` section from the INI file, «{}»)." .format(tdir.t_ini)) if not "groot_wizard" in specs: raise ValueError( "This is not a test case (it is missing the «wizard» section from the INI «{}»)." .format(tdir.t_ini)) wizard_params = specs["groot_wizard"] try: wiz_tol = int(wizard_params["tolerance"]) wiz_og = wizard_params["outgroups"].split(",") except KeyError as ex: raise ValueError( "This is not a test case (it is missing the «{}» setting from the «wizard» section of the INI «{}»)." .format(ex, tdir.t_ini)) # Copy the test files to the output folder for file in file_helper.list_dir(tdir.t_folder): shutil.copy( file, file_helper.format_path(file, tdir.r_folder + "/input_{N}{E}")) # Create settings walkthrough = groot.Wizard( new=True, name=tdir.r_model, imports=groot.sample_data.get_sample_contents(tdir.t_folder), pauses=set(), tolerance=wiz_tol, outgroups=wiz_og, alignment="", tree="maximum_likelihood", # "neighbor_joining", view=False, save=False, supertree="clann") try: # Execute the wizard (no pauses are set so this only requires 1 `step`) walkthrough.make_active() walkthrough.step() if not walkthrough.is_completed: raise ValueError("Expected wizard to complete but it did not.") # Add the original graph to the Groot `Model` in case we debug test_tree_file_data = groot.UserGraph(mgraph.importing.import_edgelist( file_helper.read_all_text(tdir.t_tree), delimiter="\t"), name="original_graph") groot.rectify_nodes(test_tree_file_data.graph, groot.current_model()) groot.current_model().user_graphs.append( groot.FixedUserGraph(test_tree_file_data.graph, "original_graph")) finally: # Save the final model regardless of whether the test succeeded groot.file_save(tdir.r_model) # Perform the comparison model = groot.current_model() differences = groot.compare_graphs(model.fusion_graph_clean, test_tree_file_data) q = differences.raw_data["quartets"]["match_quartets"] print("match_quartets: " + q) # Write the results--- # ---Summary io_helper.save_ini(tdir.r_summary, differences.raw_data) # ---Alignments groot.print_alignments(file=tdir.r_alignments) # ---Differences file_helper.write_all_text(tdir.r_comparison, differences.html, newline=True) differences.name = "test_differences" groot.current_model().user_reports.append(differences) # ---Model groot.file_save(tdir.r_model) # Done intermake.pr.printx( "<verbose>The test has completed, see «{}».</verbose>".format( tdir.r_comparison)) return groot.EChanges.MODEL_OBJECT