|
| 1 | +import { firstValueFrom } from 'rxjs' |
| 2 | +import { toArray } from 'rxjs/operators' |
| 3 | + |
| 4 | +import Adapter from './PairwiseIndexedPAFAdapter.ts' |
| 5 | +import MyConfigSchema from './configSchema.ts' |
| 6 | + |
| 7 | +function makeAdapter( |
| 8 | + pifFile: string, |
| 9 | + assemblyNames: [string, string], |
| 10 | + indexType: 'TBI' | 'CSI' = 'TBI', |
| 11 | +) { |
| 12 | + return new Adapter( |
| 13 | + MyConfigSchema.create({ |
| 14 | + pifGzLocation: { |
| 15 | + localPath: pifFile, |
| 16 | + locationType: 'LocalPathLocation', |
| 17 | + }, |
| 18 | + index: { |
| 19 | + indexType, |
| 20 | + location: { |
| 21 | + localPath: `${pifFile}.tbi`, |
| 22 | + locationType: 'LocalPathLocation', |
| 23 | + }, |
| 24 | + }, |
| 25 | + assemblyNames, |
| 26 | + }), |
| 27 | + ) |
| 28 | +} |
| 29 | + |
| 30 | +const pifInsPath = |
| 31 | + require.resolve('../../../../test_data/volvox/volvox_ins.pif.gz') |
| 32 | +const pifDelPath = |
| 33 | + require.resolve('../../../../test_data/volvox/volvox_del.pif.gz') |
| 34 | + |
| 35 | +describe('PairwiseIndexedPAFAdapter', () => { |
| 36 | + describe('coordinate extraction from PIF format', () => { |
| 37 | + it('fetches features from query assembly perspective (q-lines)', async () => { |
| 38 | + const adapter = makeAdapter(pifInsPath, ['volvox_ins', 'volvox']) |
| 39 | + |
| 40 | + const features = adapter.getFeatures({ |
| 41 | + refName: 'ctgA', |
| 42 | + start: 0, |
| 43 | + end: 60000, |
| 44 | + assemblyName: 'volvox_ins', |
| 45 | + }) |
| 46 | + |
| 47 | + const featuresArray = await firstValueFrom(features.pipe(toArray())) |
| 48 | + expect(featuresArray.length).toBe(1) |
| 49 | + |
| 50 | + const feature = featuresArray[0]! |
| 51 | + expect(feature.get('refName')).toBe('ctgA') |
| 52 | + expect(feature.get('assemblyName')).toBe('volvox_ins') |
| 53 | + expect(feature.get('start')).toBe(0) |
| 54 | + expect(feature.get('end')).toBe(54801) |
| 55 | + |
| 56 | + const mate = feature.get('mate') |
| 57 | + expect(mate.refName).toBe('ctgA') |
| 58 | + expect(mate.assemblyName).toBe('volvox') |
| 59 | + expect(mate.start).toBe(0) |
| 60 | + expect(mate.end).toBe(50001) |
| 61 | + }) |
| 62 | + |
| 63 | + it('fetches features from target assembly perspective (t-lines)', async () => { |
| 64 | + const adapter = makeAdapter(pifInsPath, ['volvox_ins', 'volvox']) |
| 65 | + |
| 66 | + const features = adapter.getFeatures({ |
| 67 | + refName: 'ctgA', |
| 68 | + start: 0, |
| 69 | + end: 60000, |
| 70 | + assemblyName: 'volvox', |
| 71 | + }) |
| 72 | + |
| 73 | + const featuresArray = await firstValueFrom(features.pipe(toArray())) |
| 74 | + expect(featuresArray.length).toBe(1) |
| 75 | + |
| 76 | + const feature = featuresArray[0]! |
| 77 | + expect(feature.get('refName')).toBe('ctgA') |
| 78 | + expect(feature.get('assemblyName')).toBe('volvox') |
| 79 | + expect(feature.get('start')).toBe(0) |
| 80 | + expect(feature.get('end')).toBe(50001) |
| 81 | + |
| 82 | + const mate = feature.get('mate') |
| 83 | + expect(mate.refName).toBe('ctgA') |
| 84 | + expect(mate.assemblyName).toBe('volvox_ins') |
| 85 | + expect(mate.start).toBe(0) |
| 86 | + expect(mate.end).toBe(54801) |
| 87 | + }) |
| 88 | + |
| 89 | + it('returns consistent coordinates regardless of query perspective', async () => { |
| 90 | + const adapter = makeAdapter(pifInsPath, ['volvox_ins', 'volvox']) |
| 91 | + |
| 92 | + const queryFeatures = await firstValueFrom( |
| 93 | + adapter |
| 94 | + .getFeatures({ |
| 95 | + refName: 'ctgA', |
| 96 | + start: 0, |
| 97 | + end: 60000, |
| 98 | + assemblyName: 'volvox_ins', |
| 99 | + }) |
| 100 | + .pipe(toArray()), |
| 101 | + ) |
| 102 | + |
| 103 | + const targetFeatures = await firstValueFrom( |
| 104 | + adapter |
| 105 | + .getFeatures({ |
| 106 | + refName: 'ctgA', |
| 107 | + start: 0, |
| 108 | + end: 60000, |
| 109 | + assemblyName: 'volvox', |
| 110 | + }) |
| 111 | + .pipe(toArray()), |
| 112 | + ) |
| 113 | + |
| 114 | + const qFeature = queryFeatures[0]! |
| 115 | + const tFeature = targetFeatures[0]! |
| 116 | + |
| 117 | + expect(qFeature.get('start')).toBe(tFeature.get('mate').start) |
| 118 | + expect(qFeature.get('end')).toBe(tFeature.get('mate').end) |
| 119 | + expect(qFeature.get('mate').start).toBe(tFeature.get('start')) |
| 120 | + expect(qFeature.get('mate').end).toBe(tFeature.get('end')) |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + describe('CIGAR handling', () => { |
| 125 | + it('uses pre-computed CIGAR from q-lines (D operation for query perspective)', async () => { |
| 126 | + const adapter = makeAdapter(pifInsPath, ['volvox_ins', 'volvox']) |
| 127 | + |
| 128 | + const features = await firstValueFrom( |
| 129 | + adapter |
| 130 | + .getFeatures({ |
| 131 | + refName: 'ctgA', |
| 132 | + start: 0, |
| 133 | + end: 60000, |
| 134 | + assemblyName: 'volvox_ins', |
| 135 | + }) |
| 136 | + .pipe(toArray()), |
| 137 | + ) |
| 138 | + |
| 139 | + const cigar = features[0]!.get('CIGAR') |
| 140 | + expect(cigar).toBe('31198M4800D18803M') |
| 141 | + }) |
| 142 | + |
| 143 | + it('uses pre-computed CIGAR from t-lines (I operation for target perspective)', async () => { |
| 144 | + const adapter = makeAdapter(pifInsPath, ['volvox_ins', 'volvox']) |
| 145 | + |
| 146 | + const features = await firstValueFrom( |
| 147 | + adapter |
| 148 | + .getFeatures({ |
| 149 | + refName: 'ctgA', |
| 150 | + start: 0, |
| 151 | + end: 60000, |
| 152 | + assemblyName: 'volvox', |
| 153 | + }) |
| 154 | + .pipe(toArray()), |
| 155 | + ) |
| 156 | + |
| 157 | + const cigar = features[0]!.get('CIGAR') |
| 158 | + expect(cigar).toBe('31198M4800I18803M') |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + describe('deletion test file', () => { |
| 163 | + it('fetches features with correct coordinates from del file', async () => { |
| 164 | + const adapter = makeAdapter(pifDelPath, ['volvox_del', 'volvox']) |
| 165 | + |
| 166 | + const queryFeatures = await firstValueFrom( |
| 167 | + adapter |
| 168 | + .getFeatures({ |
| 169 | + refName: 'ctgA', |
| 170 | + start: 0, |
| 171 | + end: 60000, |
| 172 | + assemblyName: 'volvox_del', |
| 173 | + }) |
| 174 | + .pipe(toArray()), |
| 175 | + ) |
| 176 | + |
| 177 | + expect(queryFeatures.length).toBe(1) |
| 178 | + const feature = queryFeatures[0]! |
| 179 | + expect(feature.get('start')).toBe(0) |
| 180 | + expect(feature.get('end')).toBe(45141) |
| 181 | + expect(feature.get('mate').start).toBe(0) |
| 182 | + expect(feature.get('mate').end).toBe(50001) |
| 183 | + }) |
| 184 | + }) |
| 185 | + |
| 186 | + describe('getRefNames', () => { |
| 187 | + it('returns query reference names for query assembly', async () => { |
| 188 | + const adapter = makeAdapter(pifInsPath, ['volvox_ins', 'volvox']) |
| 189 | + const refNames = await adapter.getRefNames({ |
| 190 | + regions: [{ assemblyName: 'volvox_ins' }], |
| 191 | + }) |
| 192 | + expect(refNames).toContain('ctgA') |
| 193 | + }) |
| 194 | + |
| 195 | + it('returns target reference names for target assembly', async () => { |
| 196 | + const adapter = makeAdapter(pifInsPath, ['volvox_ins', 'volvox']) |
| 197 | + const refNames = await adapter.getRefNames({ |
| 198 | + regions: [{ assemblyName: 'volvox' }], |
| 199 | + }) |
| 200 | + expect(refNames).toContain('ctgA') |
| 201 | + }) |
| 202 | + }) |
| 203 | + |
| 204 | + describe('getAssemblyNames', () => { |
| 205 | + it('returns assembly names from config', () => { |
| 206 | + const adapter = makeAdapter(pifInsPath, ['volvox_ins', 'volvox']) |
| 207 | + expect(adapter.getAssemblyNames()).toEqual(['volvox_ins', 'volvox']) |
| 208 | + }) |
| 209 | + |
| 210 | + it('returns assembly names from queryAssembly/targetAssembly config', () => { |
| 211 | + const adapter = new Adapter( |
| 212 | + MyConfigSchema.create({ |
| 213 | + pifGzLocation: { |
| 214 | + localPath: pifInsPath, |
| 215 | + locationType: 'LocalPathLocation', |
| 216 | + }, |
| 217 | + index: { |
| 218 | + location: { |
| 219 | + localPath: `${pifInsPath}.tbi`, |
| 220 | + locationType: 'LocalPathLocation', |
| 221 | + }, |
| 222 | + }, |
| 223 | + queryAssembly: 'query_asm', |
| 224 | + targetAssembly: 'target_asm', |
| 225 | + }), |
| 226 | + ) |
| 227 | + expect(adapter.getAssemblyNames()).toEqual(['query_asm', 'target_asm']) |
| 228 | + }) |
| 229 | + }) |
| 230 | +}) |
0 commit comments