Skip to content

Commit 74c43c4

Browse files
committed
Updates
1 parent c67405a commit 74c43c4

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

plugins/wiggle/src/MultiWiggleAdapter/MultiWiggleAdapter.test.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,121 @@ describe('MultiWiggleAdapter.getSources', () => {
359359
expect(sources1).toEqual(sources2)
360360
})
361361
})
362+
363+
describe('source uniqueness', () => {
364+
it('should keep sources unique when all are different', async () => {
365+
adapter = new MultiWiggleAdapter(configSchema.create({}))
366+
367+
adapter.getAdapters = jest.fn().mockResolvedValue([
368+
{
369+
source: 'source-1',
370+
type: 'BigWigAdapter',
371+
bigWigLocation: { uri: 'http://example.com/data/file1.bw' },
372+
dataAdapter: {} as any,
373+
},
374+
{
375+
source: 'source-2',
376+
type: 'BigWigAdapter',
377+
bigWigLocation: { uri: 'http://example.com/data/file2.bw' },
378+
dataAdapter: {} as any,
379+
},
380+
{
381+
source: 'source-3',
382+
type: 'BigWigAdapter',
383+
bigWigLocation: { uri: 'http://example.com/data/file3.bw' },
384+
dataAdapter: {} as any,
385+
},
386+
])
387+
388+
const sources = await adapter.getSources([])
389+
390+
expect(sources).toHaveLength(3)
391+
expect(sources[0]!.source).toBe('source-1')
392+
expect(sources[1]!.source).toBe('source-2')
393+
expect(sources[2]!.source).toBe('source-3')
394+
395+
// Verify all sources are unique
396+
const uniqueSourcesSet = new Set(sources.map(s => s.source))
397+
expect(uniqueSourcesSet.size).toBe(3)
398+
})
399+
400+
it('should make duplicate sources unique by appending counter', async () => {
401+
adapter = new MultiWiggleAdapter(configSchema.create({}))
402+
403+
adapter.getAdapters = jest.fn().mockResolvedValue([
404+
{
405+
source: 'duplicate',
406+
type: 'BigWigAdapter',
407+
bigWigLocation: { uri: 'http://example.com/data/file1.bw' },
408+
dataAdapter: {} as any,
409+
},
410+
{
411+
source: 'duplicate',
412+
type: 'BigWigAdapter',
413+
bigWigLocation: { uri: 'http://example.com/data/file2.bw' },
414+
dataAdapter: {} as any,
415+
},
416+
{
417+
source: 'duplicate',
418+
type: 'BigWigAdapter',
419+
bigWigLocation: { uri: 'http://example.com/data/file3.bw' },
420+
dataAdapter: {} as any,
421+
},
422+
])
423+
424+
const sources = await adapter.getSources([])
425+
426+
expect(sources).toHaveLength(3)
427+
expect(sources[0]!.source).toBe('duplicate')
428+
expect(sources[1]!.source).toBe('duplicate-1')
429+
expect(sources[2]!.source).toBe('duplicate-2')
430+
431+
// Verify all sources are unique
432+
const uniqueSourcesSet = new Set(sources.map(s => s.source))
433+
expect(uniqueSourcesSet.size).toBe(3)
434+
})
435+
436+
it('should handle mixed duplicate and unique sources', async () => {
437+
adapter = new MultiWiggleAdapter(configSchema.create({}))
438+
439+
adapter.getAdapters = jest.fn().mockResolvedValue([
440+
{
441+
source: 'unique1',
442+
type: 'BigWigAdapter',
443+
bigWigLocation: { uri: 'http://example.com/data/file1.bw' },
444+
dataAdapter: {} as any,
445+
},
446+
{
447+
source: 'duplicate',
448+
type: 'BigWigAdapter',
449+
bigWigLocation: { uri: 'http://example.com/data/file2.bw' },
450+
dataAdapter: {} as any,
451+
},
452+
{
453+
source: 'duplicate',
454+
type: 'BigWigAdapter',
455+
bigWigLocation: { uri: 'http://example.com/data/file3.bw' },
456+
dataAdapter: {} as any,
457+
},
458+
{
459+
source: 'unique2',
460+
type: 'BigWigAdapter',
461+
bigWigLocation: { uri: 'http://example.com/data/file4.bw' },
462+
dataAdapter: {} as any,
463+
},
464+
])
465+
466+
const sources = await adapter.getSources([])
467+
468+
expect(sources).toHaveLength(4)
469+
expect(sources[0]!.source).toBe('unique1')
470+
expect(sources[1]!.source).toBe('duplicate')
471+
expect(sources[2]!.source).toBe('duplicate-1')
472+
expect(sources[3]!.source).toBe('unique2')
473+
474+
// Verify all sources are unique
475+
const uniqueSourcesSet = new Set(sources.map(s => s.source))
476+
expect(uniqueSourcesSet.size).toBe(4)
477+
})
478+
})
362479
})

plugins/wiggle/src/MultiWiggleAdapter/MultiWiggleAdapter.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,19 @@ export default class MultiWiggleAdapter extends BaseFeatureDataAdapter {
155155
// something, but it is static for this particular multi-wiggle adapter type
156156
async getSources(_regions: Region[]) {
157157
const adapters = await this.getAdapters()
158+
const seenSources = new Map<string, number>()
159+
158160
return adapters.map(({ dataAdapter, source, name, type, ...rest }) => {
161+
const count = (seenSources.get(source) ?? 0) + 1
162+
seenSources.set(source, count)
163+
164+
// Append counter to duplicates to ensure uniqueness
165+
const uniqueSource = count > 1 ? `${source}-${count - 1}` : source
166+
159167
return {
160168
...rest,
161169
name: name || getFilenameFromFileLocation({ type, ...rest }) || source,
162-
source,
170+
source: uniqueSource,
163171
type,
164172
}
165173
})

0 commit comments

Comments
 (0)