BedGraph value lookup

Coordinates of genomic regions are frequently stored in BED files, containing (at least) three columns:

  1. The chromosome name.
  2. The start position (inclusive).
  3. The end position (exclusive).

Positions in a BED file are 0-based. The bedGraph format is similar but adds a specific fourth column, containing some continuous-valued data, such as probability scores or transcriptome data (e.g. the number of reads mapped to each genomic region in an RNA-seq experiment).

An example of a few lines in a bedGraph file:

Chr1	1100	1120	50
Chr1	1143	1155	225
Chr1	1155	1156	229
Chr1	1241	1456	2
Chr2	10	100	454
Chr2	100	488	589

These bedGraph files are useful for data visualization and supported by standard bioinformatics tools such as the UCSC Genome Browser.

Assignment

Write a self-executable Bash script that takes three arguments:

  1. The path to a bedGraph file, e.g. example.bedgraph.
  2. A chromosome name, e.g. Chr1.
  3. A (non-negative) position, e.g. 1144.

The script outputs the score associated to that position on the given chromosome, as specified in the given bedGraph file. Some examples for the bedGraph records shown above:

$ ./getvalue.sh example.bedgraph Chr1 1144
225

$ ./getvalue.sh example.bedgraph Chr1 1155
229

$ ./getvalue.sh example.bedgraph Chr2 100
589

Some special cases to handle appropriately:

Important note: in case an error is produced, the script should have exit code 1 (not 0) so that it is clear that the script failed.