This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Creating Custom Bed File For Refgene (Beginning 500 Bp Upstream And Extending 2000 Bp Into The Gene)

I want to pick out the parts of the rn4 genome beginning 500 bp before a gene and extending 2000 bp into the gene.

Is this matter as simple as choosing the option

Create one BED record per: Whole Gene

and then changing the ouput by deducting 500 from the start position and changing the end position to start position + 2000?

E.g. changing the following BED line

chr1 100000 100500 NM_019248_up_500_chr1_134302140_r 0 -

into

chr1 99500 102000 NM_019248_up_500_chr1_134302140_r 0 -

?

Edit: if the gene is shorter than 2000 bp I should not change the end position.

ucsc

hi, I took the liberty of changing your example into one that is easier to read. This way, you will get more answers.

seems like your example doesn't match your words. if it extends 2Kb into the stram, shouldn't the 2nd number be 97500? and if you're using the strand information, then you'd want to use 200000 - 2000 to 2000000 + 500.

sorry brent, it was my fault, I've edited the question and made an error. It should be fixed now.

2 answers

Just editing Giovanni's answer (because),

  • there are only 6 columns so $7 not required
  • tabs should be inserted
  • and there is a need for strand specificity
  • plus we need to return the genes as it is, if gene length < 2000

modified script:

awk '{if ($3-$2>2000){if($6 =="+") print $1"\t"$2-500"\t"$2+2000"\t"$4"\t"$5"\t"$6; else print $1"\t"$2-2000"\t"$2+500"\t"$4"\t"$5"\t"$6} else print $0}' file.bed > out.bed

You need to note here, in case of -ve strand, the gene end (downstream) will be smaller, so I turned it up, to make it in compliance with the bed format and can be used with tools like bedtools.

Cheers

You can do it quite easily with awk.

awk '{print $1 $2-500 $2+2000 $4 $5 $6 $7}' > outputfile.txt

Log in to answer this question.