Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getLines not recognized as an async function #92

Closed
Wanda-Sankey opened this issue Mar 30, 2020 · 2 comments
Closed

getLines not recognized as an async function #92

Wanda-Sankey opened this issue Mar 30, 2020 · 2 comments

Comments

@Wanda-Sankey
Copy link

Hello,

I'm trying to parse a simple vcf to count the number of instances in a specific range. I believe I have followed the ReadMe example fairly closely (forgive me if it is a simple mistake, I'm new to javascript). Here is my current code:

const {TabixIndexedFile} = require('@gmod/tabix')
const tbiIndexed = new TabixIndexedFile({ path: '/Users/Wanda.Sankey/Documents/ADSP/GenomeAnalysisTK-3.8-1-0-gf15c1c3ef/201_ch1_Filtered.vcf.gz' })


const lines = []
await tbiIndexed.getLines('chr1',13812,13813, (line, fileOffset) => lines.push(line))

var count = Object.keys(lines).length
console.log(count)

However I get the following error message

(base) FVFYXE28L410:Capstone Wanda.Sankey$ node js/test2.js 
/Users/Wanda.Sankey/OneDrive - Van Andel Institute/Capstone/js/test2.js:6
await tbiIndexed.getLines('chr1',13812,13813, (line, fileOffset) => lines.push(line))
^^^^^

SyntaxError: await is only valid in async function
    at Module._compile (internal/modules/cjs/loader.js:891:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11

Any help is appreciated, thank you!

@garrettjstevens
Copy link
Contributor

Hello, and thanks for using tabix-js! Sorry if the readme code isn't very clear. It's some example code, but isn't something that you can just run (although we should perhaps make it that way...). Anyway, the problem you're running into is that await must be used inside a function, it can't be used in "top-level" code. So something like this should work:

const {TabixIndexedFile} = require('@gmod/tabix')
const tbiIndexed = new TabixIndexedFile({ path: '/path/to/my.vcf.gz' })

async function myFunction() {
  const lines = []
  await tbiIndexed.getLines('chr1',13812,13813, (line, fileOffset) => lines.push(line))

  var count = lines.length
  console.log(count)
}

myFunction()

Also note, you just need lines.length since it's an array. Let us know if you run into more problems.

@Wanda-Sankey
Copy link
Author

Thank you, it works perfectly! Also on a side note, I've tried several VCF parsers for javascript (I usually code in python), and yours was the most intuitive for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants