-
Notifications
You must be signed in to change notification settings - Fork 62
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
9 changed files
with
391 additions
and
188 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
186 changes: 186 additions & 0 deletions
186
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,186 @@ | ||
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', | ||
] | ||
|
||
const blank = ' ' | ||
|
||
const retitle = { | ||
name: 'Name', | ||
} as { [key: string]: string | undefined } | ||
|
||
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, parentType?: string) { | ||
return [ | ||
parentId && parentType ? `${blank}/${parentType}="${parentId}"` : '', | ||
f.get('id') ? `${blank}/name=${f.get('id')}` : '', | ||
...f | ||
.tags() | ||
.filter(tag => !coreFields.includes(tag)) | ||
.map(tag => [tag, fmt(f.get(tag))]) | ||
.filter(tag => !!tag[1] && tag[0] !== parentType) | ||
.map(tag => `${blank}/${retitle[tag[0]] || tag[0]}="${tag[1]}"`), | ||
].filter(f => !!f) | ||
} | ||
|
||
function rs(f: Feature, min: number) { | ||
return f.get('start') - min + 1 | ||
} | ||
function re(f: Feature, min: number) { | ||
return f.get('end') - min | ||
} | ||
function loc(f: Feature, min: number) { | ||
return `${rs(f, min)}..${re(f, min)}` | ||
} | ||
function formatFeat( | ||
f: Feature, | ||
min: number, | ||
parentType?: string, | ||
parentId?: string, | ||
) { | ||
const type = `${f.get('type')}`.slice(0, 16) | ||
const l = loc(f, min) | ||
const locstrand = f.get('strand') === -1 ? `complement(${l})` : l | ||
return [ | ||
` ${type.padEnd(16)}${locstrand}`, | ||
...formatTags(f, parentType, parentId), | ||
] | ||
} | ||
|
||
function formatCDS( | ||
feats: Feature[], | ||
parentId: string, | ||
parentType: string, | ||
strand: number, | ||
min: number, | ||
) { | ||
const cds = feats.map(f => loc(f, min)) | ||
const pre = `join(${cds})` | ||
const str = strand === -1 ? `complement(${pre})` : pre | ||
return feats.length | ||
? [` ${'CDS'.padEnd(16)}${str}`, `${blank}/${parentType}="${parentId}"`] | ||
: [] | ||
} | ||
|
||
export function formatFeatWithSubfeatures( | ||
feature: Feature, | ||
min: number, | ||
parentId?: string, | ||
parentType?: string, | ||
): string { | ||
const primary = formatFeat(feature, min, parentId, parentType) | ||
const subfeatures = feature.get('subfeatures') || [] | ||
const cds = subfeatures.filter(f => f.get('type') === 'CDS') | ||
const sansCDS = subfeatures.filter( | ||
f => f.get('type') !== 'CDS' && f.get('type') !== 'exon', | ||
) | ||
const newParentId = feature.get('id') | ||
const newParentType = feature.get('type') | ||
const newParentStrand = feature.get('strand') | ||
return [ | ||
...primary, | ||
...formatCDS(cds, newParentId, newParentType, newParentStrand, min), | ||
...sansCDS | ||
.map(sub => | ||
formatFeatWithSubfeatures(sub, min, newParentId, newParentType), | ||
) | ||
.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.