Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: reformat long seqs #806

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
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
24 changes: 12 additions & 12 deletions public/js/length_distribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ class Graph {
}

tick_formatter(seq_type) {
var ticks = this._scale_x.ticks();
var prefix = d3.format('~s');
var suffixes = {amino_acid: 'aa', nucleic_acid: 'bp'};
return function (d) {
if (d === 0) { return ; }
if (_.indexOf(ticks,d) >= 0) {
if (suffixes[seq_type] == 'aa') {
return `${d} ${suffixes[seq_type]}`;
} else {
return `${prefix(d)}${suffixes[seq_type]}`.replace(/([a-zA-Z]+)/, ' $1');
}
const ticks = this._scale_x.ticks();
const prefix = d3.format('~s');
const suffixes = { amino_acid: 'aa', nucleic_acid: 'bp' };

return (d) => {
if (d === 0 || !ticks.includes(d)) return;

if (suffixes[seq_type] === 'aa') {
return `${d} ${suffixes[seq_type]}`;
} else {
return ;
const formatted = prefix(d);

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.3 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')).toBe('1.5 k');
});

test('rounds up correctly based on threshold', () => {
expect(formatNumberUnits('2.95k')).toBe('3 k');
expect(formatNumberUnits('2.94k')).toBe('2.9 k');
});
});
20 changes: 15 additions & 5 deletions public/js/visualisation_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,32 @@ 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.
*
* Borrowed from Kablammo. Modified by Priyam based on https://github.com/mbostock/d3/issues/1722.
*/
export function tick_formatter(scale, seq_type) {
var prefix = d3.format('~s')
var suffixes = {amino_acid: 'aa', nucleic_acid: 'bp'};
const prefix = d3.format('~s')
const suffixes = {amino_acid: 'aa', nucleic_acid: 'bp'};

return function (d) {
return `${prefix(d)}${suffixes[seq_type]}`
.replace(/([a-zA-Z]+)/, ' $1')
const formatted = prefix(d);

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

export function formatNumberUnits(number) {
const suffix = number.replace(/[0-9.]/g, '');
const numericPart = parseFloat(number).toFixed(2);
// Round to 1 decimal place
const rounded = Math.round(numericPart * 10) / 10;

return `${rounded % 1 === 0 ? rounded.toFixed(0) : rounded} ${suffix}`;
}

export function get_seq_type(algorithm) {
var SEQ_TYPES = {
blastn: {
Expand Down
2 changes: 1 addition & 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.

2 changes: 1 addition & 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.

Loading