Answers.Body: <p>This answer assumes that you have a terminal that runs linux commands and python, and that you can install and run bedtools.</p>
  <p><strong>step 1:</strong> convert your input spreadsheet to <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwiN9KTU7sr9AhU3JjQIHWiHA5YQFnoECA4QAQ&amp;url=https%3A%2F%2Fgenome.ucsc.edu%2FFAQ%2FFAQformat.html&amp;usg=AOvVaw1YBDwZ04GPxtn2N95WP6Lr" rel="nofollow noreferrer">BED format</a>.
  There is probably an awk one liner that does this but something like this python script will do it based on csv input (assuming that your coordinates are 1-indexed):</p>
  <pre><code># writes BED format to standard output, capture it in input_file.bed
  # not actually run
  with open(&quot;input_file.csv&quot;) as infile:
    for line in infile:
      fields = line.split(&quot;,&quot;)
      coord = int(fields[2])
      outline = f&quot;chr{fields[0]}\t{coord - 1}\t{coord}\t{fields[1]}&quot;
      print(outline)
  </code></pre>
  <p><strong>step 2:</strong>
  sort the BED.</p>
  <pre><code>sort -k1,1 -k2,2n input_file.bed &gt; input_file.sorted.bed
  </code></pre>
  <p><strong>step 3:</strong>
  Get genome annotations. I am assuming that you are using the grch38 build of the human genome.</p>
  <pre><code>wget https://ftp.ebi.ac.uk/pub/databases/gencode/Gencode_human/release_43/gencode.v43.annotation.gff3.gz
  </code></pre>
  <p><strong>step 4:</strong>
  Get nearest gene for each snp.</p>
  <pre><code>bedtools closest -a input_file.sorted.bed -b gencode.v43.annotation.gff3.gz &gt; input_annotated_with_genes.bed
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-07T22:38:58.290Z
Answers.LastActivityDate: 2023-03-08T16:27:07.090Z
Answers.LastEditDate: 2023-03-08T16:27:07.090Z
Answers.UserDisplayName: Maximilian Press
Body: <p>I have data from a GWAS that provides me with the SNP, Chromosome, and base-pair. My data set has thousands of SNPs. What is the easiest way to find the nearest gene for each SNP using this information? My data looks like this:</p>
  <p><a href="https://i.sstatic.net/gQBFO.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/gQBFO.png" alt="enter image description here" /></a></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-02-23T00:11:08.677Z
Comments.Text: What language are you using? There are some approaches listed here: https://www.biostars.org/p/111225/ , do any of those answers suit?
Comments.UserDisplayName: jared_mamrot
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-02-23T00:13:45.377Z
Comments.Text: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Comments.UserDisplayName: Community
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-02-23T14:39:09.193Z
Comments.Text: Do you want the nearest gene or the gene(s) that overlap the position? What if the nearest gene is a few megabases away, would you still want that?
Comments.UserDisplayName: terdon
Tags: <gene><snp><gwas>
Title: Getting nearest gene from SNP data using SNP ID, CHR, and BP
ThreadText: # Getting nearest gene from SNP data using SNP ID, CHR, and BP
  
  (Asked by: username_0 on 2023-02-22)
  
  I have data from a GWAS that provides me with the SNP, Chromosome, and base-pair. My data set has thousands of SNPs. What is the easiest way to find the nearest gene for each SNP using this information? My data looks like this:
  
  [![enter image description here](https://i.sstatic.net/gQBFO.png)](https://i.sstatic.net/gQBFO.png)
  
  **Tags:** gene, snp, gwas
  
  > **Comments:**
  > **username_1**: What language are you using? There are some approaches listed here: https://www.biostars.org/p/111225/ , do any of those answers suit?  
  > **username_2**: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.  
  > **username_3**: Do you want the nearest gene or the gene(s) that overlap the position? What if the nearest gene is a few megabases away, would you still want that?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_4 on 2023-03-07. Score: 2
  
  This answer assumes that you have a terminal that runs linux commands and python, and that you can install and run bedtools.
  
  **step 1:** convert your input spreadsheet to [BED format](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwiN9KTU7sr9AhU3JjQIHWiHA5YQFnoECA4QAQ&url=https%3A%2F%2Fgenome.ucsc.edu%2FFAQ%2FFAQformat.html&usg=AOvVaw1YBDwZ04GPxtn2N95WP6Lr).
  There is probably an awk one liner that does this but something like this python script will do it based on csv input (assuming that your coordinates are 1-indexed):
  
  ```
  # writes BED format to standard output, capture it in input_file.bed
  # not actually run
  with open("input_file.csv") as infile:
    for line in infile:
      fields = line.split(",")
      coord = int(fields[2])
      outline = f"chr{fields[0]}\t{coord - 1}\t{coord}\t{fields[1]}"
      print(outline)
  
  ```
  
  **step 2:**
  sort the BED.
  
  ```
  sort -k1,1 -k2,2n input_file.bed > input_file.sorted.bed
  
  ```
  
  **step 3:**
  Get genome annotations. I am assuming that you are using the grch38 build of the human genome.
  
  ```
  wget https://ftp.ebi.ac.uk/pub/databases/gencode/Gencode_human/release_43/gencode.v43.annotation.gff3.gz
  
  ```
  
  **step 4:**
  Get nearest gene for each snp.
  
  ```
  bedtools closest -a input_file.sorted.bed -b gencode.v43.annotation.gff3.gz > input_annotated_with_genes.bed
  
  ```
  
  
  ---
  
  