awk to update file based on 5 conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to update file based on 5 conditions
# 1  
Old 01-21-2017
awk to update file based on 5 conditions

I am trying to use awk to update the below tab-delimited file based on 5 different rules/conditions. The final output is also
tab-delimited and each line in the file will meet one of the conditions. My attemp is below as well though I am not very confident in it. Thank you Smilie.

Condition 1: The field Classification has a default value of "VUS" for all lines in file

Condition 2: The CLINSIG field updates Classification with the value in it if it hasa lenghth < 12, else it isConflicting is the result
- since it is possible for this field to have multiple strings in it I used the greatest single value "Likely Benign" and if the value in the field exceeds 12 characters
then "Conflicting" is the result, the multiple values are also separated by | symbol

Condition 3: If the Func.IDP.refGene = UTR then the value of Classification is Likely Benign,
unleess CLINSIG had a value already

Condition 4: If the PopFreqMax > .01 then If the Classification is Likely Benign else it is VUS,
unleess CLINSIG had a value already

Condition 5: If Func.IDP.refGene = spicing AND GeneDetail.IDP.refGene has +/- > 10
then the value of Classification is Likely Benign, unleess CLINSIG had a value already

Thank you Smilie.

file
Code:
R_Index    Chr    Start    End    Ref    Alt    Func.IDP.refGene    GeneDetail.IDP.refGene    AAChange.IDP.refGene    PopFreqMax    CLINSIG    CLNDBN    Classification    Quality
1    chr1    40562993    40562993    T    C    UTR5    NM_000310.3:c.-83A>G    .    0.9    .    .    .    15
2    chr5    125887685    125887685    C    T    splicing    NM_001201377.1:exon14:c.1233+28G>A    .    0.82    .    .    .    10
3    chr16    2105400    2105400    C    T    splicing    NM_000548.4:exon6:c.482-3C>T    .    0.21    not provided|not provided|not provided|not provided|other|Benign    TSC    .    25
4    chr16    2110805    2110805    G    A    exonic    .    TSC2:NM_000548.4:exon11:c.1110G>A:p.Q370Q    .004    Pathogenic    TSC    .    40

Descri[tion of fields
Code:
awk 'NR==1{for(i=1;i<=NF;i++){print "Number of field in terms of NF is--> NF-" NF-i", value is-->" $i}}' file
Number of field in terms of NF is--> NF-13, value is-->R_Index
Number of field in terms of NF is--> NF-12, value is-->Chr
Number of field in terms of NF is--> NF-11, value is-->Start
Number of field in terms of NF is--> NF-10, value is-->End
Number of field in terms of NF is--> NF-9, value is-->Ref
Number of field in terms of NF is--> NF-8, value is-->Alt
Number of field in terms of NF is--> NF-7, value is-->Func.IDP.refGene
Number of field in terms of NF is--> NF-6, value is-->GeneDetail.IDP.refGene
Number of field in terms of NF is--> NF-5, value is-->AAChange.IDP.refGene
Number of field in terms of NF is--> NF-4, value is-->PopFreqMax
Number of field in terms of NF is--> NF-3, value is-->CLINSIG
Number of field in terms of NF is--> NF-2, value is-->CLNDBN
Number of field in terms of NF is--> NF-1, value is-->Classification
Number of field in terms of NF is--> NF-0, value is-->Quality

Code:
# default classification to "VUS" 
awk -F'\t' -v OFS='\t' 'NR>1{$(NF-1)="VUS"} 1' file > vus

# check clinvar
awk -F'\t' -v OFS='\t' '{if ($(NF-3=length(<12)=$NF-3) else "Conflicting" 1' vus > clinvar

# UTR check
awk -F'\t' -v OFS='\t' '{if ($(NF-7="UTR")="Likely Benign") else $NF-3} 1' clinvar > utr

# check PopFreq
awk -F'\t' -v OFS='\t' '{if ($(NF-4 > .01)($(NF-1}="Likely Benign")} 1' utr > popfreq

# splicing check
awk -F'\t' -v OFS='\t' '{if ($(NF-7="splicing") AND ($(NF-6)=+/1) else $NF-3} 1' popfreq > final

desired output
Code:
R_Index    Chr    Start    End    Ref    Alt    Func.IDP.refGene    GeneDetail.IDP.refGene    AAChange.IDP.refGene    PopFreqMax    CLINSIG    CLNDBN    Classification    Quality
1    chr1    40562993    40562993    T    C    UTR5    NM_000310.3:c.-83A>G    .    0.9    .    .    Likely Benign    15
2    chr5    125887685    125887685    C    T    splicing    NM_001201377.1:exon14:c.1233+28G>A    .    0.82    .    .    Likely Benign    10
3    chr16    2105400    2105400    C    T    splicing    NM_000548.4:exon6:c.482-3C>T    .    0.21    not provided|not provided|not provided|not provided|other|Benign    TSC    Conflicting    25
4    chr16    2110805    2110805    G    A    exonic    .    TSC2:NM_000548.4:exon11:c.1110G>A:p.Q370Q    .004    Pathogenic    TSC    Pathogenic    40


Last edited by cmccabe; 01-21-2017 at 03:28 PM.. Reason: fixed format and added details
# 2  
Old 01-21-2017
Do you need the five intermediate files or is one awk script that reads file and just produces final sufficient?

When you say "My attemp(sic) is below as well though I am not very confident in it.", do you mean that you're getting syntax errors that you are unable to fix, or do you mean that you're getting output but you aren't confident that the results meet your specifications?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-21-2017
Just the final is sufficient. What I mean by my attempts is that when I run the first awk for condition 1, I get the desired result of all N$NF-1 fields defaulting to VUS. The other 4 awks do not run and have syntax errors in them.

The intermediate files were only to verify that everything was as expected. As I am not proficient and it seems to be good practice, I tend to write more code then is needed to make sure. Hopefully as I learn more and improve that will be less. Thank you Smilie.
# 4  
Old 01-21-2017
Conditions 3 through 5 all say: "unleess(sic) CLINSIG had a value already". Please explain what this means.

First: every field has a value (it might be an empty string, but that is still a value; but there are no empty fields in your sample input either).

Second: the only field that is assigned any value in your scripts and in your requirements is the Classification field, so to what does already refer? Did I miss something where one of these other fields is supposed to be assigned a value while running your scripts?

Third: Condition 1 unconditionally sets Classification to the string VUS. Condition 2 unconditionally sets Classification either to the value of the CLINSIG field or the string Likely Benign. Then Condition 3 may reset Classification to the string Likely Benign. Then Condition 4 sets Classification to the string Likely Benign or to the string VUS. And, finally, Condition 5 may reset Classification to the string Likely Benign. From this description of what your five conditions do, there is no need to perform Conditions 1, 2, or 3 (since Condition 4 always sets one of two values and Condition 5 might change the value Condition 4 set). What am I missing?
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-22-2017
If CLINSIG has a value in it of Benign, then for the particular line the Classification is Benign. So I guess if CLINSIG has a value in it then that is the Classification and none of the conditions are applied.

My actual dataset does have some fields with null values in it. but I filled in those with ., that is what I mean by there are no empty fields.

I am not sure what you mean by already, you are correct that only Classification is assigned a value.

The first condition does unconditionally set Classification to VUS. That seems to be working.

Condition 2 sets the Classification to the value of CLINSIG unless CLINSIG has multiple values in it like in line 3 of file (if there are multiple values in it they are separated by a |. If this is the case then Classification is Conflicting.

Condition 3 is only run on the lines where Func.IDP.refGene = UTR then the value of Classification is Likely Benign,
if CLINSIG is set to VUS, if it is any other value it is not changed.

Condition 4 is if the PopFreqMax > .01 then the Classification is Likely Benign unless there is a value other
then VUS there. If there is that means CLINSIG had a value already.

Condition 5 is only run if Func.IDP.refGene = spicing AND GeneDetail.IDP.refGene has +/- symbol in it AND the number
after it is > 10 then the value of Classification is Likely Benign, unless there is a value other
then VUS there. If there is that means CLINSIG had a value already.

Thank you Smilie.
# 6  
Old 01-23-2017
Quote:
Originally Posted by cmccabe
If CLINSIG has a value in it of Benign, then for the particular line the Classification is Benign. So I guess if CLINSIG has a value in it then that is the Classification and none of the conditions are applied.

My actual dataset does have some fields with null values in it. but I filled in those with ., that is what I mean by there are no empty fields.

I am not sure what you mean by already, you are correct that only Classification is assigned a value.

The first condition does unconditionally set Classification to VUS. That seems to be working.

Condition 2 sets the Classification to the value of CLINSIG unless CLINSIG has multiple values in it like in line 3 of file (if there are multiple values in it they are separated by a |. If this is the case then Classification is Conflicting.

Condition 3 is only run on the lines where Func.IDP.refGene = UTR then the value of Classification is Likely Benign,
if CLINSIG is set to VUS, if it is any other value it is not changed.

Condition 4 is if the PopFreqMax > .01 then the Classification is Likely Benign unless there is a value other
then VUS there. If there is that means CLINSIG had a value already.

Condition 5 is only run if Func.IDP.refGene = spicing AND GeneDetail.IDP.refGene has +/- symbol in it AND the number
after it is > 10 then the value of Classification is Likely Benign, unless there is a value other
then VUS there. If there is that means CLINSIG had a value already.

Thank you Smilie.
There are so many conflicting requirements here that I am completely confused. From two of your paragraphs above:
Quote:
If CLINSIG has a value in it of Benign, then for the particular line the Classification is Benign. So I guess if CLINSIG has a value in it then that is the Classification and none of the conditions are applied.

Condition 2 sets the Classification to the value of CLINSIG unless CLINSIG has multiple values in it like in line 3 of file (if there are multiple values in it they are separated by a |. If this is the case then Classification is Conflicting.
The 2nd paragraph quoted above says that if a line in the input file contains an | character in the CLINSIG in a line, the Classification field in the output for that line is to be set to Conflicting. Otherwise, the Classification field in the output for that line is to be set to the string that was in the CLINSIG field in that input line (even if that string was an empty string or just contains a period character (.). And, according to the last sentence in the 1st paragraph above, once this has been done Conditions 1, 3, 4, and 5 are always to be ignored. I'm sure that isn't what you mean, but it is what you have repeatedly stated.

You talked about empty fields above, but not in earlier posts. Whether or not a field is empty, it has a value. And, you have stated that if a field's value contains less than 12 characters (which certainly includes 0 if that field's value is an empty string or 1 if that field's value is a period), then that value becomes the Classification field's value.

Since I can't make any sense out of your stated requirements, let me see if I can restate your requirements in a way that makes sense to me, hoping that I capture what you intended to say.

Requirements:
Perform the following tests in sequence until the stated condition for a test evaluates to TRUE. For the 1st test whose condition evaluates to TRUE, set the value of the Classification field in that output line to the corresponding stated value:
  1. If the value of the CLINSIG field contains a | character, set the Classification to Conflicted.
  2. If the value of the CLINSIG field is not an empty string, is not the string ., and is not the string VUS; set the Classification field to the value of the CLINSIG field.
  3. If the value of the CLINSIG field is the string VUS and the value of the Func.IDP.refGene field is the string UTR, set the Classification field to the string Likely Benign.
  4. If the value of the CLINSIG field is the string VUS and the value of the PopFreqMax field is greater than .01, set the Classification field to the string Likely Benign.
  5. If the value of the CLINSIG field is the string VUS, the value of the Func.IDP.refGene is the string splicing or the string spicing, and the absolute value of the GeneDetail.IDP.refGene field is greater than 10; set the Classification field to the string Likely Benign.
  6. If none of the above tests succeeded, set the Classification field to the string VUS.
Would code written to meet the above restatement of your requirements do what you want.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-23-2017
Quote:
Requirements:
Perform the following tests in sequence until the stated condition for a test evaluates to TRUE. For the 1st test whose condition evaluates to TRUE, set the value of the Classification field in that output line to the corresponding stated value:
  1. If the value of the CLINSIG field contains a | character, set the Classification to Conflicted.
  2. If the value of the CLINSIG field is not an empty string, is not the string ., and is not the string VUS; set the Classification field to the value of the CLINSIG field.
  3. If the value of the CLINSIG field is the string VUS and the value of the Func.IDP.refGene field is the string UTR, set the Classification field to the string Likely Benign.
  4. If the value of the CLINSIG field is the string VUS and the value of the PopFreqMax field is greater than .01, set the Classification field to the string Likely Benign.
  5. If the value of the CLINSIG field is the string VUS, the value of the Func.IDP.refGene is the string splicing or the string spicing, and the absolute value of the GeneDetail.IDP.refGene field is greater than 10; set the Classification field to the string Likely Benign.
  6. If none of the above tests succeeded, set the Classification field to the string VUS.
Would code written to meet the above restatement of your requirements do what you want.
If the value of CLINSIG is nothing will the code execute? So if CLINSIG has no value and Func.IDP.refGene is UTR is CLASSIFICATION still Likely Benign?


CLINSIG may not have a value in it, if it doesnt will the code execute. Otherwise it is perfect. Thank you very much Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update file based on match in 3 fields

Trying to use awk to store the value of $5 in file1 in array x. That array x is then used to search $4 of file1 to find aa match (I use x to skip the header in file1). Since $4 can have multiple strings in it seperated by a , (comma), I split them and iterate througn each split looking for a match.... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to assign points to variables based on conditions and update specific field

I have been reading old posts and trying to come up with a solution for the below: Use a tab-delimited input file to assign point to variables that are used to update a specific field, Rank. I really couldn't find too much in the way of assigning points to variable, but made an attempt at an awk... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk to update value based on pattern match in another file

In the awk, thanks you @RavinderSingh13, for the help in below, hopefully it is close as I am trying to update the value in $12 of the tab-delimeted file2 with the matching value in $1 of the space delimeted file1. I have added comments for each line as well. Thank you :). awk awk '$12 ==... (10 Replies)
Discussion started by: cmccabe
10 Replies

4. Shell Programming and Scripting

awk to filter file based on seperate conditions

The below awk will filter a list of 30,000 lines in the tab-delimited file. What I am having trouble with is adding a condition to SVTYPE=CNV that will only print that line if CI= must be >.05 . The other condition to add is if SVTYPE=Fusion, then in order to print that line READ_COUNT must... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

awk to update field in file based of match in another

I am trying to use awk to match two files that are tab-delimited. When a match is found between file1 $1 and file2 $4, $4 in file2 is updated using the $2 value in file1. If no match is found then the next line is processed. Thank you :). file1 uc001bwr.3 ADC uc001bws.3 ADC... (4 Replies)
Discussion started by: cmccabe
4 Replies

6. Shell Programming and Scripting

awk to update field file based on match

If $1 in file1 matches $2 in file2. Then the value in $2 of file2 is updated to $1"."$2 of file2. The awk seems to only match the two files but not update. Thank you :). awk awk 'NR==FNR{A ; next} $1 in A { $2 = a }1' file1 file2 file1 name version NM_000593 5 NM_001257406... (3 Replies)
Discussion started by: cmccabe
3 Replies

7. Shell Programming and Scripting

Split File based on different conditions

I need to split the file Conditions: Ignore any record that either starts with 1 or 9 Split the file at position 404 , if position 404 is abc or def then write all the records in a file > File 1 , the remaining records should go in to a file > File 2 Further I want to split the... (7 Replies)
Discussion started by: protech
7 Replies

8. Shell Programming and Scripting

awk merging files based on 2 complex conditions

1. if the 1st row IDs of input1 (ID1/ID2.....) is equal to any IDNames of input2 print all relevant values together as defined in the output. 2. A bit tricky part is IDno in the output. All we need to do is numbering same kind of letters as 1 (aa of ID1) and different letters as 2 (ab... (4 Replies)
Discussion started by: ruby_sgp
4 Replies

9. Shell Programming and Scripting

using awk to count no of records based on conditions

Hi I am having files with date and time stamp as the folder names like 200906051400,200906051500,200906051600 .....hence everyday 24 files will be generated i need to do certain things on this 24 files daily file contains the data like 200906050016370 0 1244141195225298lessrv3 ... (13 Replies)
Discussion started by: aemunathan
13 Replies

10. Shell Programming and Scripting

validating a file based on conditions

i have a file in unix in which the records are like this aaa 123 233 aaa 234 222 aaa 242 222 bbb 122 111 bbb 122 123 ccc 124 222 In the output i want only the below records aaa ccc The validation logic is 1st column and 2nd column need to be considered if both columns values are... (8 Replies)
Discussion started by: trichyselva
8 Replies
Login or Register to Ask a Question