0

I have a file contains some positions,

head positions
142541687
142541814
142541910
142542976
142544533
142546366
142548674
142560386

I have another folder contains of 1800 files, each file belongs to one individual (1800 individuals in total). Column 2 is position and column 4 is individual ID:

 head NA20507.mapped.ILLUMINA.bwa.TSI.low_coverage.20130415.bam_dp
1   142541687   78 NA20507
1   142535975   79 NA20507
1   142535976   79 NA20507
1   142535977   77 NA20507
1   142535978   78 NA20507
1   142535979   77 NA20507

or example of another individual,

head NA20901.mapped.ILLUMINA.bwa.GIH.low_coverage.20120522.bam_dp
1   142541687   135 NA20901
1   142535975   135 NA20901
1   142535976   137 NA20901
1   142535977   138 NA20901
1   142535978   138 NA20901
1   142535979   136 NA20901
1   142535980   135 NA20901

For each individual file, I want to keep only those rows that have the positions from the position file (second column of individual file matches the position file), however I want to store it for each position separately!

   cat NA20507.mapped.ILLUMINA.bwa.TSI.low_coverage.20130415.bam_dp_match142541687
        1   142541687   78 NA20507

In the end I want to combine these files for all individuals: This is for example for position 142541687

head desired_pos142541687
1   142541687   78  NA20507
1   142541687   135 NA20901

or for the position 142542976;

desired_pos142542976

    1   142535976   79 NA20507
    1   142535976   137 NA20901
3
  • Do you actually need the intermediate .bam_dp_match142541687 file?
    – Kusalananda
    May 4, 2018 at 18:32
  • @ Kusalananda, no I do not, just wanted to show for better understanding.
    – Anna1364
    May 4, 2018 at 18:35
  • @ steeldriver, yes. just edited slightly It is there now
    – Anna1364
    May 4, 2018 at 18:36

1 Answer 1

1

This is more or less the same sort of answer as some that you've already had for similar questions: Read one file first (the positions), then parse the other files and extract the data.

awk 'NR == FNR { pos[$1]=1; next } $2 in pos { f="desired_pos" $2; print >>f; close(f) }' positions NA*.bam_dp

If given the two individual files in the question, and if positions included 142535975, then this would create desired_pos142535975 with the following contents:

1   142535975   79 NA20507
1   142535975   135 NA20901

This would work assuming that all the positions referred to chromosome 1 (or at least the same chromosome as in the individual files), since there is no chromosome name information other than the location in the positions file.

5
  • I am actually getting a syntax error. I have 340 positions, and I need to make the "desired_pos" output for each of these positions in the position file that match the position in the individual file.
    – Anna1364
    May 4, 2018 at 18:52
  • @Anna1364 What is the error you're getting?
    – Kusalananda
    May 4, 2018 at 18:53
  • awk: cmd. line:1: NR == FNR { pos[$1]=1; next } $2 in pos { f="desired_pos" $2; print >>f; close f } awk: cmd. line:1: ^ syntax error
    – Anna1364
    May 4, 2018 at 18:54
  • @Anna1364 Ah, I see now. You are using GNU awk and I'm not. I will fix this. Just a moment...
    – Kusalananda
    May 4, 2018 at 18:55
  • @Anna1364 Fixed. close f needed to be changed to be close(f).
    – Kusalananda
    May 4, 2018 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .