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 2 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';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the tests!


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');
});
});
33 changes: 28 additions & 5 deletions public/js/visualisation_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,45 @@ 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]}`;
};
}

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this custom rounding logic threshold? Am I right in thinking that we could instead round up to 1 decimal place, then remove ".0" at the end if it's a whole number? I think it would achieve the same result with a much easier to understand and simpler code, or am I missing something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tadast the threshold based on suggested logic from Daniel

divide these numbers by 1000 and round to the nearest decimal place

19,299 bp = 19.3 kbp
19,990 bp = 20.0 kbp
divide these numbers by 1000000 and round to the nearest decimal place

199,929,999 bp = 199.9 Mbp
199,990,000 bp = 200.0 Mbp

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what I said in the previous comment still works. We just want to round up to the nearest decimal place and remove /\.0$/


return `${formatted} ${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