-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tadast the threshold based on suggested logic from Daniel
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
return `${formatted} ${suffix}`; | ||
} | ||
|
||
export function get_seq_type(algorithm) { | ||
var SEQ_TYPES = { | ||
blastn: { | ||
|
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
There was a problem hiding this comment.
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!