Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wrox
    Junior Member
    • Mar 2015
    • 2

    #1

    gene overlap

    Hello,
    I'm a newbie to awk and trying to teach myself to do basic lookups in sequencing data files from command line. I have a file with two columns which correspond to start and stop positions of genes. I want to print the lines (genes) that have overlap with other genes in the file based on the start and stop positions (columns 1 and 2) so that I can simply see which genes positions have overlap. Are there any suggestions for an awk based approach to quickly accomplish this?

    I tried the following with no luck

    awk '{prev=$0; b1=$1; b2=$2; getline if ($1 >= b1 && $2 <= b2) print prev}' filename

    Any insight or suggestions would be appreciated - I may have some syntax errors here to work through.
    Last edited by wrox; 03-02-2015, 07:02 PM.
  • dpryan
    Devon Ryan
    • Jul 2011
    • 3478

    #2
    The tldr version: use bedtools or R, they have functions for this sort of thing that are already debugged.

    The syntax error is due to not having a semicolon after getline:
    Code:
     awk '{prev=$0; b1=$1; b2=$2; getline if ($1 >= b1 && $2 <= b2) print prev}' filename
    Now with that aside, there are a couple implementational issues. Firstly, you're processing lines in batches of two, so if an even numbered line overlaps a subsequent odd numbered line then you'll miss that:

    Code:
    1	10
    11	20
    12	19
    21	30
    You'd want that to either print the "11 20" or the "12 19" line, but neither will get printed since the lines won't be compared (you're comparing line 1&2, then 3&4, etc.). Also, if you have an odd number of lines, then the last line will get printed, since the last call to "getline" will return the same line as the one you're on.

    Another issue is that you're only looking for regions where one is included completely in the other. If two regions actually just overlap, then they'll be missed. You can get around that with a more complicated comparison operation.

    A final issue is that if an entry overlaps more than one other entry you'll miss that. Getting around this with awk would be somewhat complicated.

    For this sort of thing you'd be best off not using awk (though if you're trying to learn what's possible then this is probably a good exercise).

    Comment

    • wrox
      Junior Member
      • Mar 2015
      • 2

      #3
      Thanks. I was afraid I was comparing only pairs of consecutive lines. Would a loop be best in this case? As part of this task, I'm basically trying to learn to do comparisons across all lines in the file, as opposed to column based comparisons which are more starlight forward in awk it seems. For instance, to assess non coding regions one could subtract the stop position for a given line in column 2, from the start position in column 1 of the next line (assuming the file is sorted based on position).

      Anyway, thanks for your reply. It was helpful.

      Comment

      • dpryan
        Devon Ryan
        • Jul 2011
        • 3478

        #4
        A loop might be the only way to do this with awk. Keep in mind that a loop is going to be quite slow. If you ever want to do this for more than a learning experience then use bedtools or similar. It implements these sorts of operations in a much better way than is reasonably doable with awk.

        Comment

        Latest Articles

        Collapse

        • SEQadmin2
          Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
          by SEQadmin2



          Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

          Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
          ...
          10-14-2026, 09:55 AM
        • SEQadmin2
          New Genomics Technologies Take Aim at Long-Standing Limits
          by SEQadmin2


          Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

          We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
          ...
          09-28-2026, 10:25 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by SEQadmin2, Yesterday, 11:13 AM
        0 responses
        17 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, Yesterday, 10:48 AM
        0 responses
        18 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-15-2026, 10:52 AM
        0 responses
        36 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-14-2026, 05:39 AM
        0 responses
        42 views
        0 reactions
        Last Post SEQadmin2  
        Working...