Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 2x 3x 3x 2x 58x 58x 2x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 2x 6x 6x 1x 1x 1x 5x 5x 5x 6x 2x 11x 11x 11x 2x 2x 1x 1x 2x 2x 2x 11x 1x 1x 1x 1x 1x 8x 8x 11x 2x 13x 13x 13x 13x 13x 13x 13x 13x 2x 26x 26x 26x 8x 8x 8x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 2x 8x 8x 8x 8x 1x 1x 1x 7x 8x 1x 1x 1x 6x 6x 6x 6x 8x 6x 6x 8x 2x 24x 24x 24x 2x 56x 56x 56x 27x 27x 27x 29x 29x 29x 29x 29x 29x 29x 56x 2x 38x 38x 38x 38x 38x 38x 38x 38x 38x 21x 17x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 2x 38x 38x 38x 38x 12x 12x 12x 12x 12x | /**
* The **Bookmarks** card (spec §8.10): places worth coming back to.
*
* Under Manual annotations, and that ordering is the argument for the card
* existing at all. The marks above it are things a reader draws *on* a picture;
* these are pictures a reader wants *back*. Both are notes made while reading and
* neither is in a file, which is why they are in the same column — but a mark
* decorates where you are and a bookmark takes you somewhere, so the row's whole
* affordance is the arrow at the end of it.
*
* ## What pressing one does, and why it is not just a region
*
* A saved place opens the **whole chromosome**, zoomed to the location on the
* diagonal. Not `region=chrI:1200000-1260000`, which would cut a block at those
* coordinates and leave the reader in a box with paper outside it. The block is
* read whole and the *view* is placed, so the first thing anybody does on
* arriving — pan out a little to see what is either side — works. That
* distinction is carried by `ViewState.focus`, which is where the reasoning
* lives.
*
* ## Three ways in, and they are three different moments
*
* The **button beside `go`** is for the place you are looking at: you have found
* something and you want it back, and the round trip to a sidebar to retype
* coordinates you are already looking at is the whole cost of not having it.
* Typing into the card is for a coordinate you have from somewhere else — a
* paper, a table, a colleague. **BED** is for a list of them, which is what
* anybody who has called peaks or loops actually has in hand: forty places, and
* the point is to walk them one after another rather than to type any of them.
*/
import {
mintBookmarkId,
parseTypedRegion,
regionsFromBed,
type Bookmark,
type BookmarkStore,
} from '../annotations/bookmarks'
import { formatLocus, formatPosition } from '../genome/assembly'
import { KEYS, withKey } from './commands'
import { button, el } from './dom'
import { resolutionLabel } from '../units'
import { cardTitle, icon, iconButton, setFolded, startFolded } from './icons'
export interface BookmarksCardProps {
store: BookmarkStore
/**
* Take the reader to a saved place.
*
* The shell's job, not this card's: getting there is a navigation to a
* resolution, a chromosome and a window, and only the session knows whether the
* bin size asked for is one this file has.
*/
go(mark: Bookmark): void
/** `true` when there is a map on screen whose window could be saved. */
available(): boolean
/**
* The stretch of genome the map is showing, or `null` when it is not at one
* place — a whole-genome map framed across four chromosomes, a block of whole
* contigs, a picture that has not been read yet.
*
* A function rather than a value because the card outlives every view mounted
* under it: it is built once per file and the picture changes beneath it on
* every navigation.
*/
framed(): { contig: string; start: number; end: number; wrapped?: boolean } | null
/** The bin size the map is open at, in bp, to save alongside a place. */
binsize(): number | null
}
export class BookmarksCard {
readonly el: HTMLElement
private readonly list = el('div', { class: 'ann-list' })
private readonly typed = el('input', {
class: 'input',
type: 'text',
placeholder: 'II:120,000-180,000',
// The sentence every other locus box carries (§26.1), because this one is
// read as they are now — and was the one box that was not.
title:
'A window, or one position. Coordinates are 1-based and inclusive, as IGV and samtools ' +
'write them.',
})
private readonly here: HTMLButtonElement
private readonly note = el('p', { class: 'ann-note hidden' })
private readonly unsubscribe: () => void
constructor(private readonly props: BookmarksCardProps) {
// `save this view`, a verb: the three rows above this one on the Manual card
// pair `add` with `pick on map` or `draw on map`, and after three of those a
// button reading `this view` alone reads as a caption for the box beside it
// rather than as the second way to fill it.
this.here = button('save this view', () => this.saveFramed(), {
class: 'btn',
title: withKey('Save the stretch of genome on screen, at the resolution it is open at', KEYS.bookmark),
})
// Folded until it is asked for, as the Metadata annotations card above it
// is (§8.10): the last card in the column, opened when a reader has a place
// to keep, and a heading the rest of the time. The `save` button beside
// `go` fills it without it being open.
this.el = startFolded(
el(
'section',
{ class: 'card annotation-card' },
cardTitle('Bookmarks', 'bookmark'),
// The ways in above the list, on the Tracks card's rule: with forty peaks
// imported, the way to add a forty-first must not be below forty rows.
this.addForm(),
this.importRow(),
this.note,
this.list,
),
)
this.unsubscribe = props.store.subscribe(() => this.render())
this.render()
}
destroy(): void {
this.unsubscribe()
}
/**
* Enable or disable *this view* as views come and go — `ManualCard.refresh`.
*
* On {@link BookmarksCardProps.available} rather than on `framed()`, and the
* difference is a bug this had: the shell refreshes at the *moment* a view is
* mounted, and a map is read asynchronously, so a panel that will have a window
* in 200 ms has none right now. Disabling on the window left the button dead for
* the whole life of every view.
*/
refresh(): void {
this.here.disabled = !this.props.available()
}
// -------------------------------------------------------------------------
// Adding
// -------------------------------------------------------------------------
private addForm(): HTMLElement {
const add = button('add', () => this.saveTyped(), {
title: 'Save these coordinates at the resolution the map is open at',
})
this.typed.addEventListener('keydown', (event) => {
if (event.key === 'Enter') add.click()
})
return el(
'div',
{ class: 'manual-form' },
el('span', { class: 'manual-form-title', text: 'Save a place' }),
el('div', { class: 'manual-row' }, this.typed),
el('div', { class: 'manual-row' }, add, this.here),
)
}
private saveTyped(): void {
// The typed form, 1-based and inclusive, as the rows below are printed and
// every other locus box reads (§26.1).
const region = parseTypedRegion(this.typed.value)
if (region === null) {
this.typed.classList.add('is-bad')
return
}
this.typed.classList.remove('is-bad')
this.keep({ ...region, label: '' })
this.typed.value = ''
}
/**
* Save the window on screen.
*
* Public because the button beside `go` on the title line is the same act, and
* two implementations of it would be two things to keep in step (§8.10). The
* refusal is *said*, not swallowed: the two ways to reach this are both a press,
* and a press that appears to do nothing is the worst of the three answers.
*/
saveFramed(): void {
// Opened, when it was folded: the button beside `go` answers by the row
// appearing here (`Session.bookmarkButton`), and a card that starts folded
// would take the answer with it. Opened whatever the outcome, since the
// refusals below are said on the card's own line too.
setFolded(this.el, false)
const region = this.props.framed()
if (region === null) {
this.say(
this.props.available()
? 'This picture is not at one place — zoom into a single sequence to bookmark it.'
: 'No map is open to bookmark.',
)
return
}
// A window across a circular sequence's origin (§8.15) is one place and
// cannot be written down as one: a bookmark leaves here as a BED interval,
// and a BED interval whose end is below its start is not a BED interval. Said
// in those words rather than through the sentence above, which would tell a
// reader looking at a single sequence that they are not.
if (region.wrapped === true) {
this.say(
'This window runs across the sequence’s own origin, which a bookmark cannot be written ' +
'as — move off the junction, or bookmark the two halves.',
)
return
}
const { contig, start, end } = region
this.keep({ contig, start, end, label: '' })
}
/**
* One place, saved, with the grain it was found at.
*
* The bin size comes from the app rather than from the reader because it is not
* a choice being made here — it is a fact about the map that is open, and a
* bookmark's promise is to give back the picture the note was made against.
*
* A duplicate is reported rather than added. Pressing *this view* twice is what
* anybody does when they cannot remember whether the first press worked, and
* the useful answer to that is "yes, it is already there" and not a second
* identical row (`BookmarkStore.add`).
*/
private keep(mark: Omit<Bookmark, 'id' | 'binsize'>): void {
const binsize = this.props.binsize()
const added = this.props.store.add({
...mark,
id: mintBookmarkId(),
...(binsize === null ? {} : { binsize }),
})
this.say(added ? null : 'That place is already bookmarked.')
}
// -------------------------------------------------------------------------
// BED
// -------------------------------------------------------------------------
/**
* A file of places, in.
*
* BED because that is the file a reader has: peak calls, loop calls, a table of
* domains, the output of every tool that emits intervals. Only the first three
* columns are read and the fourth is taken as the name if it is there
* (`annotations/bookmarks.ts`), so a narrowPeak and a two-column-plus-header
* table from R both import without being asked what they are.
*
* Deliberately *not* the Tracks card's drop zone. Dropping a BED there draws it
* as a lane, which is the right answer to "show me these" — this is the answer
* to "walk me through these", and one gesture must not mean both. Hence a
* button that says which of the two it is, in the card whose list it fills.
*/
private importRow(): HTMLElement {
const input = el('input', { class: 'hidden-input', type: 'file' })
input.accept = '.bed,.txt,.tsv,.narrowPeak,.broadPeak'
input.addEventListener('change', () => {
const file = input.files?.[0]
// Cleared so choosing the same file twice fires `change` the second time.
input.value = ''
if (file !== undefined) void this.readBed(file)
})
return el(
'div',
{ class: 'manual-form' },
el('span', { class: 'manual-form-title', text: 'Import' }),
el(
'div',
{ class: 'manual-row' },
button('regions from BED…', () => input.click(), {
title:
'Every interval in the file becomes a bookmark. The first three columns are read, ' +
'and the fourth is its name if it has one.',
}),
input,
),
)
}
private async readBed(file: File): Promise<void> {
let text: string
try {
text = await file.text()
} catch {
this.say(`${file.name} could not be read.`)
return
}
const { regions, skipped } = regionsFromBed(text)
if (regions.length === 0) {
this.say(`No regions in ${file.name} — the first three columns should be name, start, end.`)
return
}
// The file's own grain, which is none: a BED says where and not how coarse,
// so these carry no bin size and open at whatever the map is at. Inventing
// the current one would put a claim in the bookmark that the file never made.
const added = this.props.store.addAll(
regions.map((region) => ({ ...region, id: mintBookmarkId() })),
)
const parts = [`${added} of ${regions.length} added`]
if (added < regions.length) parts.push('the rest were already saved')
if (skipped > 0) parts.push(`${skipped} ${skipped === 1 ? 'line' : 'lines'} had no region`)
this.say(`${file.name}: ${parts.join(', ')}.`)
}
/**
* One line under the list, or none. Cleared by the next thing that happens.
* Public for the one thing only the session knows: that a bookmark's place
* is set aside by the edit the map is drawn through (§27.7).
*/
say(message: string | null): void {
this.note.textContent = message ?? ''
this.note.classList.toggle('hidden', message === null)
}
// -------------------------------------------------------------------------
// The list
// -------------------------------------------------------------------------
private render(): void {
this.refresh()
const marks = this.props.store.marks
if (marks.length === 0) {
this.list.replaceChildren()
return
}
this.list.replaceChildren(
...marks.map((mark) => this.row(mark)),
button('remove all', () => this.props.store.clear(), {
class: 'btn btn-quiet btn-wide',
title: 'Forget every saved place on this file',
}),
)
}
/**
* One saved place: what it is, and an arrow.
*
* The whole row is the button, rather than a label with a small arrow beside
* it. A list of coordinates gives no clue that clicking one does anything, and
* a 14-pixel target for the card's only real action is a target readers miss —
* so the arrow is the *sign* and the row is the surface. The × stays a control
* of its own, outside the button, because forgetting a place and going to it are
* not gestures to put a pixel apart.
*/
private row(mark: Bookmark): HTMLElement {
const go = button('', () => this.props.go(mark), {
class: 'btn bookmark-go',
title: `Open ${mark.contig} zoomed to this place${
mark.binsize === undefined ? '' : `, at ${resolutionLabel(mark.binsize)} bins`
} — the rest of the chromosome stays behind it`,
})
go.replaceChildren(
el('span', { class: 'bookmark-where' }, ...whereOf(mark)),
...(mark.binsize === undefined
? []
: [el('span', { class: 'bookmark-res', text: resolutionLabel(mark.binsize) })]),
icon('arrow-right', { size: 14 }),
)
return el(
'div',
{ class: 'ann-row bookmark-row' },
el(
'div',
{ class: 'ann-head' },
go,
iconButton('x', 'Forget this place', () => this.props.store.remove(mark.id), {
class: 'btn-close',
size: 14,
}),
),
)
}
}
/**
* A saved place, as the reader should read it.
*
* Two lines' worth in one: the name if the file gave one, and the coordinates
* always. Named rows are what a BED of genes or loops produces and the name is
* what the reader is looking for — but the coordinates are what makes two rows
* called `peak_1` and `peak_1` tell themselves apart, so they are never dropped.
*
* The coordinates are §26.1's display form, through the two helpers that write
* it: a window as `II:120,001-180,000`, and a place of no width as the one base
* it stands at. They were the stored numbers printed as they are kept, 0-based,
* so a bookmark of the window on screen read a base earlier than the breadcrumb
* that window had just been saved from.
*/
function whereOf(mark: Bookmark): HTMLElement[] {
const coords =
mark.end > mark.start ? formatLocus(mark) : formatPosition(mark.contig, mark.start)
if (mark.label === '') return [el('span', { class: 'bookmark-coords', text: coords })]
return [
el('span', { class: 'bookmark-name', text: mark.label }),
el('span', { class: 'bookmark-coords', text: coords }),
]
}
|