1

I have a file in the following format, where, columns 6 and 7 are allele A and B. All I need to do is make changes in column 9 and onwards based on the alleles in columns 6 and 7. If column 9 field value is 0 then replace it with column 6 and if it is 2 replace it with column 7. If it is -1 then it should be left as such and 1 should be marked as col 6/col 7. This I have to do repeatedly for all the fields in each until the end of each row. Below are a few such rows pasted for your reference. Each row has some 130 fields.

Probe Set ID    Affy SNP ID     Chromosome      Physical Position       Flank   Allele A        Allele B        probeset_id     SunOleic97R     NC94022 S1      S2      S3      S4      S9      S11     S14 
AX-147208720    Affx-152069361  Aradu.A01       5066618 TTTCTTGGCGGCATTGCTGATTTCTTATCATCCAA[A/G]CCATTCTTCTTTGTGTCAGGGTGGAATCTAAAATT     A       G       AX-147208720    0       2       0       0       2
AX-147209428    Affx-152065184  Aradu.A01       9154456 TAGCTGTTGACATGTCAATTGCTAAGGGAGAGTCC[C/T]TTGGAAAGCCCTACATCATTCATCAAATCATTCTC     T       C       AX-147209428    2       0       2       0       0
AX-147209429    Affx-152069061  Aradu.A01       9155638 TCAGCAAATGAACCTCTTAAGAAACCAATTCGGTC[A/G]TTGCTTATCACTAAGCTTTCAATCCCTTTCACTGG     A       G       AX-147209429    2       0       2       0       0
AX-147209430    Affx-152031763  Aradu.A01       9157305 CGGCGCTCTAAAATCCAGATAACAACTCCAACAAC[C/T]AAGAAAAAGGTTGCTGTGACAAACCACATCATTGG     T       C       AX-147209430    2       0       2       0       0
AX-147209432    Affx-152067683  Aradu.A01       9205209 CCCTTAATTGGGGAAGAGAGTTGTTCCACTGTGAG[A/G]ATTGATGTTAGGCTTGCAATGTAGCTTGAATTCAG     A       G       AX-147209432    2       0       2       0       0
AX-147209600    Affx-152035192  Aradu.A01       9873259 CTCCTTCTCTCGGTTTCCAAGACAAAAGAAAGACA[A/G]ATATCTTTTAAGATCTTCCTCAGTTTTGTTCTCCC     A       G       AX-147209600    2       0       0       0       0
AX-147209601    Affx-152067325  Aradu.A01       9873427 TGGCCACATTGGAACCACAACATACACAGTGAAGT[C/T]TTGCTTAGCTTTAATCTTGCTAACAATTTTAAGTG     T       C       AX-147209601    2       0       0       0       0
AX-147209615    Affx-152066978  Aradu.A01       9974460 AAAACTCACAATTCTTCTTTGATGATCTGAGTCCT[C/T]TCCATTTGACAATTTAGCATCCACCACCACAATCT     T       C       AX-147209615    0       2       2       2       2

I initially tried to replace the value of field in column 9 based on its values using:

awk '{if ($9 == 0) print $9 == $6}; 1' file.txt |less -S 

It did not do any change to the original file. Can anyone please help me!

  • Assuming you are the same person: FYI, you can edit posts, no need to delete your questions and then repost them. – phk Jan 1 '17 at 18:25
  • Yes, i am the same person! New to Unix.stackexchange! Will keep it in mind now on. – gaurav agarwal Jan 1 '17 at 18:39
  • == is a comparison operator. try {if $9 == 0) $9=$6} 1 – icarus Jan 1 '17 at 19:11
  • Thanks for the suggestion! It works. Could you please suggest me if I have to run it for other conditions like if ($9 == 2) $9=$7; esleif ($9==1) $9=$6/$7 until the end of each row. How do i use a loop here satisfying these conditions. – gaurav agarwal Jan 1 '17 at 20:34
  • unless you put @icarus in your comment I will not be notified so you will have to hope I will browse back here. – icarus Jan 3 '17 at 12:54
2

let's have it step by step

If column 9 ($i) field value is 0 then replace it with column 6

if ( $i == 0 ) $i=$6 ;

and if it is 2 replace it with column 7.

if ( $i == 2 ) $i=$7 ;

If it is -1 then it should be left as such

# 

and 1 should be marked as col 6/col 7.

if ( $i == 1 ) $i=$6 "/" $7 ;

This I have to do repeatedly for all the fields in each until the end of each row.

NR>1  {for(i=9;i<=NF;i++)
        {  } ## ifs ;
       ## for loop is done
       print ; ## print to be done at end of loop.
       } 

All together,

put all these line in a file a.awk

NR>1  {for(i=9;i<=NF;i++)
        { if ( $i == 0 ) $i=$6 ;
          if ( $i == 2 ) $i=$7 ;
          if ( $i == 1 ) $i=$6 "/" $7 ;
       } ## if ;
       ## for loop is done
       print ;
       } 

where

  • NF is a builtin for 'Number of Field'
  • print will print

With your sample data

awk -f a.awk sample.txt 
AX-147208720 Affx-152069361 Aradu.A01 5066618 TTTCTTGGCGGCATTGCTGATTTCTTATCATCCAA[A/G]CCATTCTTCTTTGTGTCAGGGTGGAATCTAAAATT A G AX-147208720 A G A A G
AX-147209428 Affx-152065184 Aradu.A01 9154456 TAGCTGTTGACATGTCAATTGCTAAGGGAGAGTCC[C/T]TTGGAAAGCCCTACATCATTCATCAAATCATTCTC T C AX-147209428 C T C T T
AX-147209429 Affx-152069061 Aradu.A01 9155638 TCAGCAAATGAACCTCTTAAGAAACCAATTCGGTC[A/G]TTGCTTATCACTAAGCTTTCAATCCCTTTCACTGG A G AX-147209429 G A G A A
AX-147209430 Affx-152031763 Aradu.A01 9157305 CGGCGCTCTAAAATCCAGATAACAACTCCAACAAC[C/T]AAGAAAAAGGTTGCTGTGACAAACCACATCATTGG T C AX-147209430 C T C T T
AX-147209432 Affx-152067683 Aradu.A01 9205209 CCCTTAATTGGGGAAGAGAGTTGTTCCACTGTGAG[A/G]ATTGATGTTAGGCTTGCAATGTAGCTTGAATTCAG A G AX-147209432 G A G A A
AX-147209600 Affx-152035192 Aradu.A01 9873259 CTCCTTCTCTCGGTTTCCAAGACAAAAGAAAGACA[A/G]ATATCTTTTAAGATCTTCCTCAGTTTTGTTCTCCC A G AX-147209600 G A A A A
AX-147209601 Affx-152067325 Aradu.A01 9873427 TGGCCACATTGGAACCACAACATACACAGTGAAGT[C/T]TTGCTTAGCTTTAATCTTGCTAACAATTTTAAGTG T C AX-147209601 C T T T T
AX-147209615 Affx-152066978 Aradu.A01 9974460 AAAACTCACAATTCTTCTTTGATGATCTGAGTCCT[C/T]TCCATTTGACAATTTAGCATCCACCACCACAATCT T C AX-147209615 T C C C C

Beware that awk will not edit original file. You have to use :

awk -f a.awk file1.txt > file2.txt
cp file2.txt file1.txt
rm file2.txt
  • Many thanks for the step wise explanation! It works. Could you please suggest me if I have to run it in a loop satisfying these conditions for all the fields in each row. I have over 100 such fields in the form of 0 2 and 1 in each row. Regards – gaurav agarwal Jan 1 '17 at 20:42
  • Thanks! I am trying to run a loop for over fields in each row where I can initiate the process from column 9 and run it until column 150. With the current method I will have to specify condition for each field (number of columns). Need to run a loop with a counter. Also, I am not able to understand $19 = $16. I think it should be $6. – gaurav agarwal Jan 2 '17 at 14:24
  • In order to get the above mentioned result I tried something like this but getting syntax errors:awk '{if(NR>1){for(i=9;i<=NF;i++){if($i == 0)$i=$6}} {($i==2)$i=$7} {($i==1)$i=$6"/"$7 print}}' S-pop_axiom_ara_annot_poly_call_mast.txt |less -S – gaurav agarwal Jan 2 '17 at 15:04
  • x.sh: line 6: syntax error near unexpected token (' x.sh: line 6: NR>1 {for(i=9;i<=NF;i++)' – gaurav agarwal Jan 2 '17 at 15:35
  • just remove i from $6 "/" $7i from your script. – gaurav agarwal Jan 2 '17 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.