Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using AWK to perform VLOOKUP-like command

    Hi all,

    I have a set of files with genotypic data, each divided into 3 columns of data, including: MARKER, ID, GENOTYPE.

    I would like to use AWK (without changing the order/sorting of the files) to perform a VLOOKUP-like command in order to join the data within the files into a single file as follows:

    File1:

    BIEC2-99962 HOR_233 G_G
    BIEC2-9997 HOR_233 A_G
    BIEC2-999748 HOR_233 C_C
    BIEC2-999848 HOR_233 G_G
    BIEC2-99989 HOR_233 A_A

    File2:

    BIEC2-9997 HOR_250 A_A
    BIEC2-999748 HOR_250 C_C
    BIEC2-99989 HOR_250 A_C

    File3:

    BIEC2-9997 HOR_615 A_G
    BIEC2-999748 HOR_615 A_C
    BIEC2-999848 HOR_615 A_G
    BIEC2-99989 HOR_615 A_C

    Expected result:

    BIEC2-99962 G_G NA NA
    BIEC2-9997 A_G A_A A_G
    BIEC2-999748 C_C C_C A_C
    BIEC2-999848 G_G NA A_G
    BIEC2-99989 A_A A_C A_C

    I would appreciate any help on this.

    Thanks!
    Last edited by sagi.polani; 02-19-2015, 12:25 AM.

  • #2
    Cross-posted on biostars.

    Comment


    • #3
      You could probably modify this script for your input sets:

      Code:
      #!/usr/bin/env python
      
      input_one = '''
      BIEC2-99962 HOR_233 G_G
      BIEC2-9997 HOR_233 A_G
      BIEC2-999748 HOR_233 C_C
      BIEC2-999848 HOR_233 G_G
      BIEC2-99989 HOR_233 A_A
      '''.strip().split()
      
      input_two = '''
      BIEC2-9997 HOR_250 A_A
      BIEC2-999748 HOR_250 C_C
      BIEC2-99989 HOR_250 A_C
      '''.strip().split()
      
      input_three = '''
      BIEC2-9997 HOR_615 A_G
      BIEC2-999748 HOR_615 A_C
      BIEC2-999848 HOR_615 A_G
      BIEC2-99989 HOR_615 A_C
      '''.strip().split()
      
      one_list = input_one[::3]
      one_dict = dict(zip(one_list, input_one[2::3]))
      two_dict = dict(zip(input_two[::3], input_two[2::3]))
      three_dict = dict(zip(input_three[::3], input_three[2::3]))
      
      print '\n'.join([' '.join([k, one_dict[k], two_dict.get(k, 'NA'), three_dict.get(k, 'NA')]) for k in one_list])
      The output looks like:

      Code:
      $ ./join_test.py
      BIEC2-99962 G_G NA NA
      BIEC2-9997 A_G A_A A_G
      BIEC2-999748 C_C C_C A_C
      BIEC2-999848 G_G NA A_G
      BIEC2-99989 A_A A_C A_C
      This seems to match your expected output.

      If you want to understand how the script works, use some print statements for each variable before the list comprehension, and then break the list comprehension down into smaller pieces.

      Ultimately, you would replace lists input_one, input_two and input_three with the results from reading in your input files with open() and readlines() methods.

      Remember to strip() and split() so that each element of the list is separated from the others, regardless of whether the delimiter is a space or newline — use print to investigate one of the sample input lists, if this requirement isn't clear.

      I'd second that awk is not really the ideal tool for this job, and I use it a great deal.

      Comment

      Latest Articles

      Collapse

      • seqadmin
        Advancing Precision Medicine for Rare Diseases in Children
        by seqadmin




        Many organizations study rare diseases, but few have a mission as impactful as Rady Children’s Institute for Genomic Medicine (RCIGM). “We are all about changing outcomes for children,” explained Dr. Stephen Kingsmore, President and CEO of the group. The institute’s initial goal was to provide rapid diagnoses for critically ill children and shorten their diagnostic odyssey, a term used to describe the long and arduous process it takes patients to obtain an accurate...
        12-16-2024, 07:57 AM
      • seqadmin
        Recent Advances in Sequencing Technologies
        by seqadmin



        Innovations in next-generation sequencing technologies and techniques are driving more precise and comprehensive exploration of complex biological systems. Current advancements include improved accessibility for long-read sequencing and significant progress in single-cell and 3D genomics. This article explores some of the most impactful developments in the field over the past year.

        Long-Read Sequencing
        Long-read sequencing has seen remarkable advancements,...
        12-02-2024, 01:49 PM

      ad_right_rmr

      Collapse

      News

      Collapse

      Topics Statistics Last Post
      Started by seqadmin, 12-17-2024, 10:28 AM
      0 responses
      32 views
      0 likes
      Last Post seqadmin  
      Started by seqadmin, 12-13-2024, 08:24 AM
      0 responses
      48 views
      0 likes
      Last Post seqadmin  
      Started by seqadmin, 12-12-2024, 07:41 AM
      0 responses
      34 views
      0 likes
      Last Post seqadmin  
      Started by seqadmin, 12-11-2024, 07:45 AM
      0 responses
      46 views
      0 likes
      Last Post seqadmin  
      Working...
      X