1

I have multiple text files like below:

file1.txt:

# Program:featureCounts v1.6.0; Command:"featureCounts" "-a" "/documents/gencode_Release27_GRCh38.p10_PRI/gencode.v27.primary_assembly.annotation_nochr.gtf" "-F" "GTF" "-p" "-s" "2" "-T" "8" "-o" "/read_counts/S100A.txt" "/documents/S100A.sorted.bam" 
Geneid  Chr     Start   End     Strand  Length  /path/to/documents/S100A.sorted.bam
ENSG00000223972.5       1;1;1;1;1;1;1;1;1       11869;12010;12179;12613;12613;12975;13221;13221;13453   12227;12057;12227;12721;12697;13052;13374;14409;13670   +;+;+;+;+;+;+;+;+       1735    0
ENSG00000227232.5       1;1;1;1;1;1;1;1;1;1;1   14404;15005;15796;16607;16858;17233;17606;17915;18268;24738;29534       14501;15038;15947;16765;17055;17368;17742;18061;18366;24891;29570       -;-;-;-;-;-;-;-;-;-;-   1351    0
ENSG00000278267.1       1       17369   17436   -       68      0
ENSG00000243485.5       1;1;1;1;1       29554;30267;30564;30976;30976   30039;30667;30667;31109;31097   +;+;+;+;+       1021    0
ENSG00000284332.1       1       30366   30503   +       138     0
ENSG00000237613.2       1;1;1;1;1       34554;35245;35277;35721;35721   35174;35481;35481;36073;36081   -;-;-;-;-       1219    0

file2.txt:

# Program:featureCounts v1.6.0; Command:"featureCounts" "-a" "/documents/gencode_Release27_GRCh38.p10_PRI/gencode.v27.primary_assembly.annotation_nochr.gtf" "-F" "GTF" "-p" "-s" "2" "-T" "8" "-o" "/read_counts/S106.txt" "/documents/S106.sorted.bam" 
Geneid  Chr     Start   End     Strand  Length  /path/to/documents/S106.sorted.bam
ENSG00000223972.5       1;1;1;1;1;1;1;1;1       11869;12010;12179;12613;12613;12975;13221;13221;13453   12227;12057;12227;12721;12697;13052;13374;14409;13670   +;+;+;+;+;+;+;+;+       1735    0
ENSG00000227232.5       1;1;1;1;1;1;1;1;1;1;1   14404;15005;15796;16607;16858;17233;17606;17915;18268;24738;29534       14501;15038;15947;16765;17055;17368;17742;18061;18366;24891;29570       -;-;-;-;-;-;-;-;-;-;-   1351    42
ENSG00000278267.1       1       17369   17436   -       68      12
ENSG00000243485.5       1;1;1;1;1       29554;30267;30564;30976;30976   30039;30667;30667;31109;31097   +;+;+;+;+       1021    0
ENSG00000284332.1       1       30366   30503   +       138     0
ENSG00000237613.2       1;1;1;1;1       34554;35245;35277;35721;35721   35174;35481;35481;36073;36081   -;-;-;-;-       1219    1

Like above I have more than 100 text files. I want to combine them into single text file which should look like below.

Output should look like:

Geneid            S100A   S106
ENSG00000223972.5   0       0
ENSG00000227232.5   0      42
ENSG00000278267.1   0      12
ENSG00000243485.5   0       0
ENSG00000284332.1   0       0
ENSG00000237613.2   0       1

For removing the first line in the text file I used this tail -n +2 S100A.txt. But I have to do this on every file individually. How to get the desired output with a code in linux.

2 Answers 2

1

Awk solution:

awk 'BEGIN{ head = "Geneid" }
     FNR == 2{ 
         gsub(/^.+documents\/|\.sorted\.bam$/, "", $NF);
         head = head "\t" $NF 
     }
     FNR > 2{ 
         genes[$1] = genes[$1] "\t" $NF;
         order[FNR-2] = $1
     }
     END{ 
         print head; 
         for (i = 1; i <= FNR-2; i++) print order[i] genes[order[i]]
     }' file*.txt
  • FNR - record number being read
  • $NF - the last field value (NF itself points to the total number of fields)
  • genes - array containing accumulated sequence of last field values per gene id; the array is indexed by gene ids
  • order - auxiliary array indexed with record numbers to keep the initial order of gene ids
2
  • It worked. thankyou. Could you please explain What is $NF and other.
    – beginner
    Apr 22, 2018 at 16:14
  • 1
    @raju, yes, see my explanation Apr 22, 2018 at 16:23
-1

You can also try:

ls -1  *featureCount.txt | parallel 'cat {} | sed '1d' | cut -f7 {} > {/.}_clean.txt' 
ls -1  *featureCount.txt | head -1 | xargs cut -f1 > genes.txt
paste genes.txt *featureCount_clean.txt > output.txt
1
  • 2
    Note that you cannot have single quotes inside single quotes. The only reason this is not a problem in your first command is that the sed command contains no whitespace internally; otherwise you would have word-splitting at that point.
    – AdminBee
    Oct 13, 2022 at 11:13

You must log in to answer this question.

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