Skip to content

Commit

Permalink
Fix: update logic and add spec
Browse files Browse the repository at this point in the history
  • Loading branch information
joko3ono committed Dec 5, 2024
1 parent 2870bd6 commit 91ab49d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion public/css/app.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/kablammo.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Graph {
constructor($svgContainer, props) {
this._zoom_scale_by = 1.4;
this._padding_x = 12;
this._padding_y = 50;
this._padding_y = 55;

this._canvas_height = $svgContainer.height();
this._canvas_width = $svgContainer.width();
Expand Down
5 changes: 2 additions & 3 deletions public/js/length_distribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ class Graph {
return `${d} ${suffixes[seq_type]}`;
} else {
const formatted = prefix(d);
const numericPart = Math.floor(parseFloat(formatted));
const suffix = formatted.replace(/[0-9.]/g, '');
return `${numericPart} ${suffix}${suffixes[seq_type]}`;

return `${Helpers.formatNumberUnits(formatted)}${suffixes[seq_type]}`;
}
};
}
Expand Down
28 changes: 28 additions & 0 deletions public/js/tests/visualisation_helpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Import the necessary functions
import { tick_formatter, formatNumberUnits } from '../visualisation_helpers';

describe('tick_formatter', () => {
test('formats tick for amino_acid sequence', () => {
const formatter = tick_formatter(null, 'amino_acid');
expect(formatter(1_000)).toBe('1 kaa');
expect(formatter(25_286_936)).toBe('25.2 Maa');
});

test('formats tick for nucleic_acid sequence', () => {
const formatter = tick_formatter(null, 'nucleic_acid');
expect(formatter(1_000)).toBe('1 kbp');
expect(formatter(25_965_000)).toBe('26 Mbp');
});
});

describe('formatNumberUnits', () => {
test('formats number with units correctly', () => {
expect(formatNumberUnits('1.9k')).toBe('1.9 k');
expect(formatNumberUnits('1.5k', 0.5)).toBe('2 k');
});

test('rounds up correctly based on threshold', () => {
expect(formatNumberUnits('2.95k')).toBe('3 k');
expect(formatNumberUnits('2.94k')).toBe('2.9 k');
});
});
28 changes: 24 additions & 4 deletions public/js/visualisation_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function getPrefix(str) {
}

/**
* Defines how ticks will be formatted.
* Defines how ticks will be formatted.
*
* Examples: 200 aa, 2.4 kbp, 7.6 Mbp.
*
Expand All @@ -43,13 +43,33 @@ export function tick_formatter(scale, seq_type) {

return function (d) {
const formatted = prefix(d);
const numericPart = Math.floor(parseFloat(formatted));
const suffix = formatted.replace(/[0-9.]/g, '');

return `${numericPart} ${suffix}${suffixes[seq_type]}`;
return `${formatNumberUnits(formatted)}${suffixes[seq_type]}`;
};
}

function extractSuffix(str) {
const suffix = str.replace(/[0-9.]/g, '');
const numericPart = d3.format('.2f')(parseFloat(str));

return [parseFloat(numericPart), suffix];
}

export function formatNumberUnits(number, threshold = 0.95) {
const [numericPart, suffix] = extractSuffix(number);
const integerPart = Math.floor(numericPart);
const fractionalPart = parseFloat((numericPart - integerPart).toFixed(2));

// when fractionalPart more than the threshold round up
// example:
// threshold 0.95
// 2.95 -> 3
// 2.94 -> 2.9
const formatted = fractionalPart >= threshold ? integerPart + 1 : Math.floor(numericPart * 10) / 10;

return `${formatted} ${suffix}`;
}

export function get_seq_type(algorithm) {
var SEQ_TYPES = {
blastn: {
Expand Down
3 changes: 2 additions & 1 deletion public/sequenceserver-report.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/sequenceserver-report.min.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion public/sequenceserver-search.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/sequenceserver-search.min.js.map

Large diffs are not rendered by default.

0 comments on commit 91ab49d

Please sign in to comment.