Skip to content

Commit

Permalink
Ensure a sequence was found
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Nov 1, 2021
1 parent 46c8caf commit f40decb
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions products/jbrowse-desktop/public/generateFastaIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ export async function generateFastaIndex(fileDataStream: Readable) {
const entries = []
let possibleBadLine = undefined as [number, string] | undefined
let i = 0
let foundAny = false

for await (const line of rl) {
// assumes unix formatted files with only one '\n' newline
const currentLineBytes = line.length + 1
const currentLineBases = line.length
if (line[0] === '>') {
foundAny = true
if (possibleBadLine && possibleBadLine[0] !== i - 1) {
throw new Error(possibleBadLine[1])
}
Expand All @@ -78,15 +80,15 @@ export async function generateFastaIndex(fileDataStream: Readable) {
lineBytes = 0
refSeqLen = 0
lineBases = 0
refName = line.trim().slice(1)
refName = line.trim().slice(1).split(/\s+/)[0]
currOffset += currentLineBytes
refOffset = currOffset
} else {
if (lineBases && currentLineBases !== lineBases) {
possibleBadLine = [
i,
`Not all lines in file have same width, please check your FASTA file line ${i}: ${lineBases} ${currentLineBases}`,
] as [number, string]
]
}
lineBytes = currentLineBytes
lineBases = currentLineBases
Expand All @@ -102,5 +104,8 @@ export async function generateFastaIndex(fileDataStream: Readable) {
`${refName}\t${refSeqLen}\t${refOffset}\t${lineBases}\t${lineBytes}`,
)
}
if (!foundAny) {
throw new Error('No sequences found in file, ensure this is a FASTA file')
}
return entries.join('\n')
}

0 comments on commit f40decb

Please sign in to comment.