Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions src/app/store/Alignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface AlignmentOwner {
substitutionRate: SettableOption<string>;
substitutionMatrix: SettableOption<string>;
alignments: Alignment[];
dataType: string;
dataType: string | undefined;
error: unknown;
afterRun: () => void;
removeAlignment: ( alignment: Alignment ) => void;
Expand Down Expand Up @@ -215,7 +215,8 @@ class Alignment extends InputFile<AlignmentOwner> {
case 'multistate':
return 'MULTI';
default:
return this.dataType;
// '' until the alignment is parsed.
return this.dataType ?? '';
}
}

Expand Down Expand Up @@ -292,15 +293,24 @@ class Alignment extends InputFile<AlignmentOwner> {
);
return;
}
const { dataType } = this;
// dataType check narrows for the compiler; modelTestCanRun already excludes undefined.
if ( !this.modelTestCanRun || dataType === undefined ) {
return;
}
const binaryName = this.run.modeltestName;
if ( binaryName === undefined ) {
throw new Error( 'No modeltest binary in the binaries list' );
}
this.modeltestLoading = true;
const { rendererBridge } = window;
await rendererBridge.startModelSelection( {
id: this.id,
filePath: this.path,
outputDir: this.run.outputDir,
dataType: this.dataType,
dataType,
numThreads: this.run.numThreads.value,
binaryName: this.run.modeltestName,
binaryName,
} );
};

Expand All @@ -320,14 +330,17 @@ class Alignment extends InputFile<AlignmentOwner> {
model = model.replace( 'TrN', 'TN93' );
model = model.replace( 'TPM1', 'K81' );
this.substitutionModel.setValue( model );
if ( /\+F[O|E]?/.test( raxmlNG ) ) {
this.ngStationaryFrequencies.setValue( /\+F[O|E]?/.exec( raxmlNG )[0] );
const frequencies = raxmlNG.match( /\+F[O|E]?/ );
if ( frequencies ) {
this.ngStationaryFrequencies.setValue( frequencies[0] );
}
if ( /\+IC?/.test( raxmlNG ) ) {
this.ngInvariantSites.setValue( /\+IC?/.exec( raxmlNG )[0] );
const invariantSites = raxmlNG.match( /\+IC?/ );
if ( invariantSites ) {
this.ngInvariantSites.setValue( invariantSites[0] );
}
if ( /\+GA?/.test( raxmlNG ) ) {
this.ngRateHeterogeneity.setValue( /\+GA?/.exec( raxmlNG )[0] );
const rateHeterogeneity = raxmlNG.match( /\+GA?/ );
if ( rateHeterogeneity ) {
this.ngRateHeterogeneity.setValue( rateHeterogeneity[0] );
}
if ( /\+ASC_LEWIS/.test( raxmlNG ) ) {
this.ngAscertainmentBias.setValue( '+ASC_LEWIS' );
Expand All @@ -350,7 +363,8 @@ class Alignment extends InputFile<AlignmentOwner> {
console.log( 'parsing matrix name...' );
const { options } = raxmlSettings.aminoAcidSubstitutionMatrixOptions;
const re = new RegExp( `(${options.join( '|' )})$` );
const matrixName = re.exec( model )[1];
// Null on an unlisted matrix throws a TypeError, as before; #403 owns that case.
const matrixName = re.exec( model )![1];
this.aaMatrixName = matrixName;
model = model.replace( re, '' );
}
Expand Down Expand Up @@ -396,11 +410,10 @@ class Alignment extends InputFile<AlignmentOwner> {
try {
const parsed = await rendererBridge.parseAlignment( this.id, this.path );
const { alignment, change } = parsed;
const newDataType = getFinalDataType(
this.run.alignments
.map( ( { dataType } ) => dataType )
.concat( alignment.dataType ),
);
const newDataType = getFinalDataType( [
...this.run.alignments.map( ( { dataType } ) => dataType ),
alignment.dataType,
] );
runInAction( () => {
if ( this.run.dataType !== newDataType ) {
this.run.substitutionMatrix.value =
Expand All @@ -422,8 +435,8 @@ class Alignment extends InputFile<AlignmentOwner> {
if ( change ) {
this.path = change.newFilePath;
this.convertedFrom = change.format;
this.converted = change.converted;
this.modified = change.modified;
this.converted = change.converted ?? false;
this.modified = change.modified ?? false;
this.modificationMessages = change.modificationMessages;
if ( change.modificationMessages.length > 0 ) {
this.showModifiedDialog = true;
Expand Down
4 changes: 2 additions & 2 deletions src/app/store/AlignmentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface RunLike {

interface AlignmentLike {
run: RunLike;
dataType: string;
dataType: string | undefined;
hasInvariantSites: boolean;
multistateNumber: { value: string };
}
Expand All @@ -35,7 +35,7 @@ class RaxmlNgAlignmentSubstitutionModel extends Option<string, RunLike> {
this.alignment = alignment;
}
get options(): Choice[] {
if ( !this.alignment.run.haveAlignments ) {
if ( !this.alignment.run.haveAlignments || this.alignment.dataType === undefined ) {
return [];
}
const modelSettings = ( raxmlNgModelOptions as Record<string, { options: readonly string[] }> )[this.alignment.dataType];
Expand Down
2 changes: 1 addition & 1 deletion src/app/store/InputFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface InputFileOwner {
}

class InputFile<Owner extends InputFileOwner = InputFileOwner> extends StoreBase {
run: Owner = null;
run: Owner;

constructor( run: Owner, path: string ) {
super();
Expand Down
4 changes: 2 additions & 2 deletions src/app/store/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class Option<Value = unknown, Owner = unknown> {
run: Owner;
defaultValue: Value;
title: string;
description: string;
hoverInfo: string;
description: string | undefined;
hoverInfo: string | undefined;
schema: Schema;
allowOnlyValidChange: boolean;
helperText: string;
Expand Down
7 changes: 5 additions & 2 deletions src/app/store/PartOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Choice {

interface AlignmentLike {
run: unknown;
dataType: string;
dataType: string | undefined;
partitionType: string;
length: number;
partition: { partToAdd: { type: { value: string } } };
Expand Down Expand Up @@ -85,6 +85,9 @@ class PartType extends PartBase<string> {
}
get options(): Choice[] {
const { dataType } = this.alignment;
if ( dataType === undefined ) {
return [];
}
let opts = [dataType];
if ( dataType === '' || dataType === 'mixed' ) {
opts = ['DNA', 'BIN', 'MULTI', 'protein'];
Expand Down Expand Up @@ -156,7 +159,7 @@ class PartEnd extends PartBase<number> {
name: 'part-end',
message: () => `Must be in interval [${this.min}, ${this.max}]`,
test: ( value ) => {
return value >= this.min && value <= this.max;
return value !== undefined && value >= this.min && value <= this.max;
},
} ),
allowOnlyValidChange: false,
Expand Down
7 changes: 4 additions & 3 deletions src/app/store/Partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface PartitionAlignment {
// Read by the part options, which this file hands each part to.
run: unknown;
partition: Partition;
dataType: string;
dataType: string | undefined;
partitionType: string;
aaMatrixName: string;
length: number;
Expand All @@ -30,7 +30,7 @@ interface PartValues {
codon: string;
}

const getPartitionType = ( dataType: string, aaMatrixName = 'BLOSUM62' ): string => {
const getPartitionType = ( dataType: string | undefined, aaMatrixName = 'BLOSUM62' ): string => {
// for old raxml
switch ( dataType ) {
case 'dna':
Expand All @@ -43,7 +43,8 @@ const getPartitionType = ( dataType: string, aaMatrixName = 'BLOSUM62' ): string
case 'multistate':
return 'MULTI';
default:
return dataType;
// '' until the alignment is parsed.
return dataType ?? '';
}
};

Expand Down
15 changes: 11 additions & 4 deletions src/app/store/Run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
MultifurcatingConstraintTree,
} from './RunOptions';
import type { AnalysisChoice } from './RunOptions';
import type { DataType } from '../../common/typecheckAlignment';
import { basename, join, quote } from '../paths';
import remove from 'lodash/remove';
import UserFixError from '../../common/errors';
Expand Down Expand Up @@ -207,10 +208,14 @@ class Run extends StoreBase {
}

get analysisOption(): AnalysisChoice {
return this.raxmlNgSwitch(
const option = this.raxmlNgSwitch(
raxmlNgAnalysisOptions.find( ( opt ) => opt.value === this.analysis.value ),
analysisOptions.find( ( opt ) => opt.value === this.analysis.value ),
);
if ( !option ) {
throw new Error( `No analysis option for analysis '${this.analysis.value}'` );
}
return option;
}

// Analysis params
Expand Down Expand Up @@ -362,7 +367,7 @@ class Run extends StoreBase {
: [];
}

get dataType(): string {
get dataType(): DataType | undefined {
return this.finalAlignment.dataType;
}

Expand Down Expand Up @@ -536,7 +541,7 @@ class Run extends StoreBase {
return null;
}
const convertedAlignments = this.alignments.filter( ( a ) => a.converted );
return convertedAlignments[0].convertedFrom;
return convertedAlignments[0].convertedFrom ?? null;
}

clearShowModified = (): void => {
Expand Down Expand Up @@ -567,7 +572,9 @@ class Run extends StoreBase {

astralArgs = (): string[][] => {
const first: string[] = [];
first.push( '-i', quote( this.astralTree?.path, { windows } ) );
if ( this.astralTree ) {
first.push( '-i', quote( this.astralTree.path, { windows } ) );
}
first.push(
'-o',
quote( join( this.outputDir, `ASTRAL_${this.outputNameSafe}.tre` ), { windows } ),
Expand Down
4 changes: 2 additions & 2 deletions src/app/store/RunOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface RunOwner {
finalAlignment: { hasInvariantSites: boolean };
taxons: string[];
haveAlignments: boolean;
dataType: string;
dataType: string | undefined;
usesRaxmlNg: boolean;
usesModeltestNg: boolean;
useBackboneConstraint: boolean;
Expand Down Expand Up @@ -528,7 +528,7 @@ export class SubstitutionMatrix extends Option<string, RunOwner> {
} );
}
get options(): Choice<string>[] {
if ( !this.run.haveAlignments ) {
if ( !this.run.haveAlignments || this.run.dataType === undefined ) {
return [];
}
const modelSettings = ( raxmlMatrixOptions as Record<string, { options: readonly string[] }> )[this.run.dataType];
Expand Down
2 changes: 1 addition & 1 deletion src/app/store/StoreBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class StoreBase {

unlisten = (): void => {
while ( this.eventStops.length ) {
this.eventStops.pop()();
this.eventStops.pop()?.();
}
};

Expand Down
18 changes: 11 additions & 7 deletions src/common/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import util from 'util';
import phylipParser from './phylipParser';
import fastaParser from './fastaParser';
import typecheckAlignment from './typecheckAlignment';
import type { TypecheckedAlignment } from './typecheckAlignment';
import type { FastaAlignment } from './fastaParser';
import type { PhylipAlignment } from './phylipParser';
import UserFixError from './errors';

interface AlignmentSequence {
Expand All @@ -16,7 +19,7 @@ interface Alignment {
length: number;
}

export const parseAlignment = async ( filePath: string ): Promise<Alignment> => {
export const parseAlignment = async ( filePath: string ): Promise<TypecheckedAlignment> => {
try {
await fs.promises.access( filePath );
} catch ( err: unknown ) {
Expand All @@ -32,7 +35,7 @@ export const parseAlignment = async ( filePath: string ): Promise<Alignment> =>
} );
const lines: string[] = [];
let error: Error | null = null;
let alignment: Alignment | null = null;
let alignment: FastaAlignment | PhylipAlignment | null = null;
rl.on( 'line', ( line ) => {
lines.push( line );
} ).on( 'close', () => {
Expand All @@ -55,17 +58,18 @@ export const parseAlignment = async ( filePath: string ): Promise<Alignment> =>
if ( !alignment || alignment.sequences.length === 0 ) {
return reject( new Error( `Couldn't parse any sequences from file ${filePath}` ) );
}
let typechecked: TypecheckedAlignment;
try {
typecheckAlignment( alignment );
typechecked = typecheckAlignment( alignment );
} catch ( err: unknown ) {
console.error( 'Error checking data type:', err );
return reject( err );
}
const alignmentRestricted = Object.assign( {}, alignment, {
sequences: alignment.sequences.slice( 0, 2 ),
const alignmentRestricted = Object.assign( {}, typechecked, {
sequences: typechecked.sequences.slice( 0, 2 ),
} );
console.log( 'Alignment with first two sequences:', alignmentRestricted );
resolve( alignment );
resolve( typechecked );
}
} );
} );
Expand All @@ -91,7 +95,7 @@ export const writeAlignment = async ( filePath: string, alignment: Alignment ):
};

interface Io {
parseAlignment: ( filePath: string ) => Promise<Alignment>;
parseAlignment: ( filePath: string ) => Promise<TypecheckedAlignment>;
writeAlignment: ( filePath: string, alignment: Alignment ) => Promise<void>;
}

Expand Down
23 changes: 23 additions & 0 deletions src/common/typecheckAlignment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect, it } from 'vitest';
import typecheckAlignment from './typecheckAlignment';

const alignment = ( ...codes: string[] ) => ( {
sequences: codes.map( ( code, i ) => ( { taxon: `t${i}`, code } ) ),
length: codes[0].length,
numSequences: codes.length,
fileFormat: 'FASTA',
} );

it( 'typecheckAlignment sets the data type, invariant sites and completion', () => {
expect( typecheckAlignment( alignment( 'ACGT', 'ACGA' ) ) ).toMatchObject( {
dataType: 'nucleotide',
hasInvariantSites: true,
typecheckingComplete: true,
} );
} );

it( 'typecheckAlignment throws when no sequence has a data type', () => {
expect( () => typecheckAlignment( alignment( 'TT-T', '----' ) ) ).toThrow(
'cannot determine data type of any of the 2 sequences',
);
} );
Loading
Loading