-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
337 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
packages/core/pluggableElementTypes/models/components/genbank.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { | ||
AbstractSessionModel, | ||
Feature, | ||
max, | ||
min, | ||
Region, | ||
} from '@jbrowse/core/util' | ||
import { getConf } from '@jbrowse/core/configuration' | ||
|
||
const coreFields = [ | ||
'uniqueId', | ||
'refName', | ||
'source', | ||
'type', | ||
'start', | ||
'end', | ||
'strand', | ||
'parent', | ||
'parentId', | ||
'score', | ||
'subfeatures', | ||
'phase', | ||
] | ||
|
||
function fmt(obj: unknown): string { | ||
if (Array.isArray(obj)) { | ||
return obj.map(o => fmt(o)).join(',') | ||
} else if (typeof obj === 'object') { | ||
return JSON.stringify(obj) | ||
} else { | ||
return `${obj}` | ||
} | ||
} | ||
|
||
function formatTags(f: Feature, parentId?: string, parentRef?: string) { | ||
return [ | ||
parentId ? `/Parent="${parentId}"` : '', | ||
...f | ||
.tags() | ||
.filter(tag => !coreFields.includes(tag)) | ||
.map(tag => [tag, fmt(f.get(tag))]) | ||
.filter(tag => !!tag[1]) | ||
.map(tag => ` /${tag[0]}="${tag[1]}"`), | ||
].filter(f => !!f) | ||
} | ||
|
||
function formatFeat(f: Feature, min: number) { | ||
// stringifies to undefined if none | ||
const type = `${f.get('type')}`.slice(0, 16) | ||
|
||
const loc = `${f.get('start') - min + 1}..${f.get('end') - min}` | ||
const locstrand = f.get('strand') === -1 ? `complement(${loc})` : loc | ||
return [` ${type.padEnd(16)}${locstrand}`, ...formatTags(f)] | ||
} | ||
|
||
export function formatFeatWithSubfeatures(f: Feature, min: number): string { | ||
const primary = formatFeat(f, min) | ||
const subfeatures = f.get('subfeatures') | ||
return [ | ||
...primary, | ||
...( | ||
subfeatures?.map(sub => formatFeatWithSubfeatures(sub, min)) || [] | ||
).flat(), | ||
].join('\n') | ||
} | ||
|
||
export async function stringifyGenbank({ | ||
features, | ||
assemblyName, | ||
session, | ||
}: { | ||
assemblyName: string | ||
session: AbstractSessionModel | ||
features: Feature[] | ||
}) { | ||
const today = new Date() | ||
const month = today.toLocaleString('en-US', { month: 'short' }).toUpperCase() | ||
const day = today.toLocaleString('en-US', { day: 'numeric' }) | ||
const year = today.toLocaleString('en-US', { year: 'numeric' }) | ||
const date = `${day}-${month}-${year}` | ||
|
||
const start = min(features.map(f => f.get('start'))) | ||
const end = max(features.map(f => f.get('end'))) | ||
const length = end - start | ||
const refName = features[0].get('refName') | ||
|
||
const l1 = [ | ||
`${'LOCUS'.padEnd(12)}`, | ||
`${refName}:${start + 1}..${end}`.padEnd(20), | ||
`${`${length} bp`}`.padEnd(15), | ||
`${'DNA'.padEnd(10)}`, | ||
`${'linear'.padEnd(10)}`, | ||
`${'UNK ' + date}`, | ||
].join('') | ||
const l2 = 'FEATURES Location/Qualifiers' | ||
const seq = await fetchSequence({ | ||
session, | ||
assemblyName, | ||
regions: [{ assemblyName, start, end, refName }], | ||
}) | ||
const contig = seq.map(f => f.get('seq') || '').join('') | ||
const lines = features.map(feat => formatFeatWithSubfeatures(feat, start)) | ||
const seqlines = ['ORIGIN', `\t1 ${contig}`, '//'] | ||
return [l1, l2, ...lines, ...seqlines].join('\n') | ||
} | ||
|
||
async function fetchSequence({ | ||
session, | ||
regions, | ||
signal, | ||
assemblyName, | ||
}: { | ||
assemblyName: string | ||
session: AbstractSessionModel | ||
regions: Region[] | ||
signal?: AbortSignal | ||
}) { | ||
const { rpcManager, assemblyManager } = session | ||
const assembly = assemblyManager.get(assemblyName) | ||
if (!assembly) { | ||
throw new Error(`assembly ${assemblyName} not found`) | ||
} | ||
|
||
const sessionId = 'getSequence' | ||
return rpcManager.call(sessionId, 'CoreGetFeatures', { | ||
adapterConfig: getConf(assembly, ['sequence', 'adapter']), | ||
regions: regions.map(r => ({ | ||
...r, | ||
refName: assembly.getCanonicalRefName(r.refName), | ||
})), | ||
sessionId, | ||
signal, | ||
}) as Promise<Feature[]> | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/core/pluggableElementTypes/models/components/gff3.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Feature } from '@jbrowse/core/util' | ||
|
||
const coreFields = [ | ||
'uniqueId', | ||
'refName', | ||
'source', | ||
'type', | ||
'start', | ||
'end', | ||
'strand', | ||
'parent', | ||
'parentId', | ||
'score', | ||
'subfeatures', | ||
'phase', | ||
] | ||
|
||
const retitle = { | ||
id: 'ID', | ||
name: 'Name', | ||
alias: 'Alias', | ||
parent: 'Parent', | ||
target: 'Target', | ||
gap: 'Gap', | ||
derives_from: 'Derives_from', | ||
note: 'Note', | ||
description: 'Note', | ||
dbxref: 'Dbxref', | ||
ontology_term: 'Ontology_term', | ||
is_circular: 'Is_circular', | ||
} as { [key: string]: string } | ||
|
||
function fmt(obj: unknown): string { | ||
if (Array.isArray(obj)) { | ||
return obj.map(o => fmt(o)).join(',') | ||
} else if (typeof obj === 'object') { | ||
return JSON.stringify(obj) | ||
} else { | ||
return `${obj}` | ||
} | ||
} | ||
|
||
function formatFeat(f: Feature, parentId?: string, parentRef?: string) { | ||
return [ | ||
f.get('refName') || parentRef, | ||
f.get('source') || '.', | ||
f.get('type') || '.', | ||
f.get('start') + 1, | ||
f.get('end'), | ||
f.get('score') || '.', | ||
f.get('strand') || '.', | ||
f.get('phase') || '.', | ||
(parentId ? `Parent=${parentId};` : '') + | ||
f | ||
.tags() | ||
.filter(tag => !coreFields.includes(tag)) | ||
.map(tag => [tag, fmt(f.get(tag))]) | ||
.filter(tag => !!tag[1]) | ||
.map(tag => `${retitle[tag[0]] || tag[0]}=${tag[1]}`) | ||
.join(';'), | ||
].join('\t') | ||
} | ||
export function formatMultiLevelFeat( | ||
f: Feature, | ||
parentId?: string, | ||
parentRef?: string, | ||
): string { | ||
const fRef = parentRef || f.get('refName') | ||
const fId = f.get('id') | ||
const primary = formatFeat(f, parentId, fRef) | ||
const subs = | ||
f.get('subfeatures')?.map(sub => formatMultiLevelFeat(sub, fId, fRef)) || [] | ||
return [primary, ...subs].join('\n') | ||
} | ||
|
||
export function stringifyGFF3(feats: Feature[]) { | ||
return ['##gff-version 3', ...feats.map(f => formatMultiLevelFeat(f))].join( | ||
'\n', | ||
) | ||
} |
Oops, something went wrong.