From b716a7736de97012d2ae8fff48f02f003f68a098 Mon Sep 17 00:00:00 2001 From: Johannes Klein Agents Date: Fri, 27 Nov 2026 23:32:57 +0100 Subject: [PATCH] Move the ASTRAL and ModelTest-NG command builders out of Run.ts (#421) Co-Authored-By: Claude Fable 5.2 --- src/app/store/Run.ts | 78 ++++------------------- src/app/store/command/astral.test.ts | 17 +++++ src/app/store/command/astral.ts | 20 ++++++ src/app/store/command/index.ts | 42 ++++++++++++ src/app/store/command/modeltestNg.test.ts | 29 +++++++++ src/app/store/command/modeltestNg.ts | 35 ++++++++++ 6 files changed, 157 insertions(+), 64 deletions(-) create mode 100644 src/app/store/command/astral.test.ts create mode 100644 src/app/store/command/astral.ts create mode 100644 src/app/store/command/index.ts create mode 100644 src/app/store/command/modeltestNg.test.ts create mode 100644 src/app/store/command/modeltestNg.ts diff --git a/src/app/store/Run.ts b/src/app/store/Run.ts index 6b55b8ca..e80b5d03 100644 --- a/src/app/store/Run.ts +++ b/src/app/store/Run.ts @@ -7,6 +7,10 @@ import FinalAlignment from './FinalAlignment'; import AstralTree from './AstralTree'; import StoreBase, { runEvents } from './StoreBase'; import { host } from './host'; +import { commandText, settingsText } from './command'; +import type { CmdArg } from './command'; +import { astralArgs } from './command/astral'; +import { modeltestNgArgs } from './command/modeltestNg'; import { binaries, analysisOptions, @@ -56,8 +60,6 @@ interface RunParent { interface FilePathEntry { path: string; } -// A CLI argument: seeds and thread counts are pushed as numbers. -type CmdArg = string | number; class Run extends StoreBase { parent: RunParent; @@ -484,10 +486,10 @@ class Run extends StoreBase { get args(): CmdArg[][] { if ( this.usesAstral ) { - return this.astralArgs(); + return astralArgs( this, { windows } ); } if ( this.usesModeltestNg ) { - return this.modeltestNgArgs(); + return modeltestNgArgs( this, { windows } ); } if ( this.usesRaxmlNg ) { return this.raxmlNgArgs(); @@ -570,43 +572,6 @@ class Run extends StoreBase { return model; } - astralArgs = (): string[][] => { - const first: string[] = []; - if ( this.astralTree ) { - first.push( '-i', quote( this.astralTree.path, { windows } ) ); - } - first.push( - '-o', - quote( join( this.outputDir, `ASTRAL_${this.outputNameSafe}.tre` ), { windows } ), - ); - return [first]; - }; - - modeltestNgArgs = (): CmdArg[][] => { - const first: CmdArg[] = []; - // data type - if ( this.dataType === 'nucleotide' ) { - first.push( '-d', 'nt' ); - } else if ( this.dataType === 'protein' ) { - first.push( '-d', 'aa' ); - } - // alignment file - first.push( '-i', quote( this.finalAlignment.path, { windows } ) ); - // output file, modeltest errors if this file already exists - first.push( - '-o', - quote( join( this.outputDir, `RAxML_GUI_ModelTest_${this.outputNameSafe}` ), { windows } ), - ); - // modeltest throws errors if the output file already exists - first.push( '--force' ); - // Number of processors - first.push( '-p', this.numThreads.value ); - // TODO: in able to support partitioned modeltest we need to change the partition file text - // https://github.com/ddarriba/modeltest/wiki/Input-Data#partition-files - // partition scheme - return [first]; - }; - raxmlNgArgs = (): CmdArg[][] => { const first: CmdArg[] = []; const second: CmdArg[] = []; @@ -1303,32 +1268,17 @@ class Run extends StoreBase { }; get command(): string { - if ( this.usesAstral ) { - return this.args - .map( ( cmdArgs ) => `java -jar ${this.binary.value} ${cmdArgs.join( ' ' )}` ) - .join( ' &&\\\n' ); - } - return this.args - .map( ( cmdArgs ) => `${this.binary.value} ${cmdArgs.join( ' ' )}` ) - .join( ' &&\\\n' ); + return commandText( this.binary.value, this.args, this.usesAstral ); } get settingsFileContent(): string { - let text = `The analysis was run using raxmlGUI 2.0 (version ${this.version}) as follows: -Analysis: ${this.analysisOption.title} -Binary: ${this.binary.value} version ${this.binary.version} -Results saved to: ${this.outputDir} -`; - let argumentext = 'RAxML was called with these arguments:\n'; - this.args.map( - ( arg, index ) => ( argumentext += `${index + 1}.) ${arg.join( ' ' )}\n` ), - ); - text += argumentext; - // TODO: should we be more precise about what the single params mean? - // text += `To repeat the analysis use the following seeds: - // Bootstrap seed: ${this.seedBootstrap} - // Parsimony seed: ${this.seedParsimony}`; - return text; + return settingsText( { + appVersion: this.version, + analysisTitle: this.analysisOption.title, + binary: this.binary, + outputDir: this.outputDir, + args: this.args, + } ); } get settingsFilePath(): string { diff --git a/src/app/store/command/astral.test.ts b/src/app/store/command/astral.test.ts new file mode 100644 index 00000000..6de5d880 --- /dev/null +++ b/src/app/store/command/astral.test.ts @@ -0,0 +1,17 @@ +import { expect, it } from 'vitest'; + +import { astralArgs } from './astral'; + +it( 'astralArgs quotes paths on Windows', () => { + const run = { astralTree: { path: 'C:\\in\\genes.tre' }, outputDir: 'C:\\out', outputNameSafe: 'run1' }; + expect( astralArgs( run, { windows: true } ) ).toEqual( [ + ['-i', '"C:\\in\\genes.tre"', '-o', '"C:\\out\\ASTRAL_run1.tre"'], + ] ); +} ); + +it( 'astralArgs leaves paths bare off Windows', () => { + const run = { astralTree: { path: '/in/genes.tre' }, outputDir: '/out', outputNameSafe: 'run1' }; + expect( astralArgs( run, { windows: false } ) ).toEqual( [ + ['-i', '/in/genes.tre', '-o', '/out/ASTRAL_run1.tre'], + ] ); +} ); diff --git a/src/app/store/command/astral.ts b/src/app/store/command/astral.ts new file mode 100644 index 00000000..9f9ede31 --- /dev/null +++ b/src/app/store/command/astral.ts @@ -0,0 +1,20 @@ +import { join, quote } from '../../paths'; +import type { Platform } from './index'; + +export interface AstralInput { + outputDir: string; + outputNameSafe: string; + astralTree: { path: string } | null | undefined; +} + +export const astralArgs = ( run: AstralInput, { windows }: Platform ): string[][] => { + const first: string[] = []; + if ( run.astralTree ) { + first.push( '-i', quote( run.astralTree.path, { windows } ) ); + } + first.push( + '-o', + quote( join( run.outputDir, `ASTRAL_${run.outputNameSafe}.tre` ), { windows } ), + ); + return [first]; +}; diff --git a/src/app/store/command/index.ts b/src/app/store/command/index.ts new file mode 100644 index 00000000..40e960af --- /dev/null +++ b/src/app/store/command/index.ts @@ -0,0 +1,42 @@ +// A CLI argument: seeds and thread counts are pushed as numbers. +export type CmdArg = string | number; + +// Shaped like the second argument `quote` takes. +export interface Platform { + windows: boolean; +} + +export const commandText = ( binary: string, args: CmdArg[][], usesAstral: boolean ): string => { + if ( usesAstral ) { + return args + .map( ( cmdArgs ) => `java -jar ${binary} ${cmdArgs.join( ' ' )}` ) + .join( ' &&\\\n' ); + } + return args + .map( ( cmdArgs ) => `${binary} ${cmdArgs.join( ' ' )}` ) + .join( ' &&\\\n' ); +}; + +export const settingsText = ( { appVersion, analysisTitle, binary, outputDir, args }: { + appVersion: string | undefined; + analysisTitle: string; + binary: { value: string; version: string | undefined }; + outputDir: string; + args: CmdArg[][]; +} ): string => { + let text = `The analysis was run using raxmlGUI 2.0 (version ${appVersion}) as follows: +Analysis: ${analysisTitle} +Binary: ${binary.value} version ${binary.version} +Results saved to: ${outputDir} +`; + let argumentext = 'RAxML was called with these arguments:\n'; + args.map( + ( arg, index ) => ( argumentext += `${index + 1}.) ${arg.join( ' ' )}\n` ), + ); + text += argumentext; + // TODO: should we be more precise about what the single params mean? + // text += `To repeat the analysis use the following seeds: + // Bootstrap seed: ${this.seedBootstrap} + // Parsimony seed: ${this.seedParsimony}`; + return text; +}; diff --git a/src/app/store/command/modeltestNg.test.ts b/src/app/store/command/modeltestNg.test.ts new file mode 100644 index 00000000..14bb9bf6 --- /dev/null +++ b/src/app/store/command/modeltestNg.test.ts @@ -0,0 +1,29 @@ +import { expect, it } from 'vitest'; + +import { modeltestNgArgs } from './modeltestNg'; + +it( 'modeltestNgArgs quotes paths on Windows', () => { + const run = { + dataType: 'protein', + finalAlignment: { path: 'C:\\in\\aln.phy' }, + outputDir: 'C:\\out', + outputNameSafe: 'run1', + numThreads: { value: 4 }, + }; + expect( modeltestNgArgs( run, { windows: true } ) ).toEqual( [ + ['-d', 'aa', '-i', '"C:\\in\\aln.phy"', '-o', '"C:\\out\\RAxML_GUI_ModelTest_run1"', '--force', '-p', 4], + ] ); +} ); + +it( 'modeltestNgArgs leaves paths bare off Windows', () => { + const run = { + dataType: 'nucleotide', + finalAlignment: { path: '/in/aln.phy' }, + outputDir: '/out', + outputNameSafe: 'run1', + numThreads: { value: 2 }, + }; + expect( modeltestNgArgs( run, { windows: false } ) ).toEqual( [ + ['-d', 'nt', '-i', '/in/aln.phy', '-o', '/out/RAxML_GUI_ModelTest_run1', '--force', '-p', 2], + ] ); +} ); diff --git a/src/app/store/command/modeltestNg.ts b/src/app/store/command/modeltestNg.ts new file mode 100644 index 00000000..80a188b2 --- /dev/null +++ b/src/app/store/command/modeltestNg.ts @@ -0,0 +1,35 @@ +import { join, quote } from '../../paths'; +import type { CmdArg, Platform } from './index'; + +export interface ModeltestNgInput { + outputDir: string; + outputNameSafe: string; + dataType: string | undefined; + finalAlignment: { path: string }; + numThreads: { value: number }; +} + +export const modeltestNgArgs = ( run: ModeltestNgInput, { windows }: Platform ): CmdArg[][] => { + const first: CmdArg[] = []; + // data type + if ( run.dataType === 'nucleotide' ) { + first.push( '-d', 'nt' ); + } else if ( run.dataType === 'protein' ) { + first.push( '-d', 'aa' ); + } + // alignment file + first.push( '-i', quote( run.finalAlignment.path, { windows } ) ); + // output file, modeltest errors if this file already exists + first.push( + '-o', + quote( join( run.outputDir, `RAxML_GUI_ModelTest_${run.outputNameSafe}` ), { windows } ), + ); + // modeltest throws errors if the output file already exists + first.push( '--force' ); + // Number of processors + first.push( '-p', run.numThreads.value ); + // TODO: in able to support partitioned modeltest we need to change the partition file text + // https://github.com/ddarriba/modeltest/wiki/Input-Data#partition-files + // partition scheme + return [first]; +};