0

I have a big matrix in which the first row contains the variable_name and the first column contains the name of the objects (variables and objects are the same, it's comparison all vs all). Moreover, I have 2 different text files. The first (file1.txt) contains the object of interest (I should use this file to select the row of interest-based on the name object reported in the first column of the matrix) while the second file (list.txt)contains a shorter list of objects and I should use this list to select the relative columns of the matrix that starting with the name of the objects reported in the list. the aim is to use these two txt files to extract the relative scores in the matrix.

e.g. matrix.txt: ( Columns are separated by a tab)

Object  3mup_A_001_____ 3oz1_A_001_____ 456r_V_002_____ 23er_B_001_____ 87t5_C_001_____
23er_B_001_____ 0.5     0.3     0.87    1       0.9
456r_V_002_____ 0.2     0.7     0.65    0.63    0.2
87t5_C_001_____ 0.35    0.5     0.254   0.12    1
dret_A_009_____ 0.99    0.88    0.7     0.89    0.214
3mup_A_001_____ 0.12    0.15    0.87    0.17    0.87

file1.txt:

87t5_C_001_____

list.txt:

3mup_A_001_____
456r_V_002_____

I should use the file1.txt (87t5_C_001____) to select the object of interest in the first column (select the 4th row of the matrix) and obtained the score associated at the column (variable) that starting with the name of the objects including in the file list.txt.

output.txt

Object  3mup_A_001_____ 456r_V_002_____
87t5_C_001_____ 0.35    0.254   

As an alternative, if it's simpler, the output could have also in another format, for example reporting the score near the object in the list:

3mup_A_001_____ 0.35
456r_V_002_____ 0.254

I hope that it's clear and that someone could help me because I don't know how I can do this operation.

Thanks.

0

1 Answer 1

3
$ cat tst.awk
BEGIN { FS=OFS="\t" }
FILENAME == ARGV[1] {
    rowNames[$1]
    next
}
FILENAME == ARGV[2] {
    colNames[$1]
    next
}
FNR == 1 {
    rowNames[$1]
    colNames[$1]
    for (i=1; i<=NF; i++) {
        if ($i in colNames) {
            f[++numCols] = i
        }
    }
}
$1 in rowNames {
    for (colNr=1; colNr<=numCols; colNr++) {
        printf "%s%s", $(f[colNr]), (colNr<numCols ? OFS : ORS)
    }
}

$ awk -f tst.awk file1.txt list.txt matrix.txt
Object  3mup_A_001_____ 456r_V_002_____
87t5_C_001_____ 0.35    0.254
4
  • The scrip give me this error: script.sh: line 1: '$: command not found script.sh: line 2: BEGIN: command not found script.sh: line 3: FILENAME: command not found script.sh: line 4: rowNames[]: command not found script.sh: line 5: next: command not found script.sh: line 6: syntax error near unexpected token }' script.sh: line 6: }'' Mar 13, 2021 at 15:27
  • You'd have to tell me what script.sh contained if you'd like me to help debug it but my best guess is you saved the awk script in a file named script.sh and then tried to execute it as if it was a shell script instead of passing it to awk to interpret. Just run the command exactly as shown in my answer - store the awk script in a file named tst.awk and run it as awk -f tst.awk file1.txt list.txt matrix.txt.
    – Ed Morton
    Mar 13, 2021 at 17:15
  • Sorry I copied the script to a file .sh. Now I did as you say, but it gives me this error: awk: tst.awk:1: $ cat tst.awk awk: tst.awk:1: ^ syntax error Mar 15, 2021 at 8:33
  • Again, without seeing what you're trying to execute, I can't do much to help you debug it. Whatever it is you have in that file, it's not the script I show in my answer. Oh, hang on - did you put $ cat tst.awk at the top of the file? Don't do that - $ is my prompt and cat tst.awk is the shell command I ran to show you the contents of the file tst.awk. The awk script that should be in tst.awk starts with the line BEGIN { FS=OFS="\t" }.
    – Ed Morton
    Mar 15, 2021 at 12:43

You must log in to answer this question.

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