0

I have gtf files in more than 100 directories. Below I'm showing how they look.

SampleA
   |___________ SampleA.GRCh38.gtf
SampleB
   |___________ SampleB.GRCh38.gtf

Here I'm showing only two gtf files as an example.

SampleA.GRCh38.gtf looks like below:

# stringtie -e -B -p 8 -G /path/stringtie_output/stringtie_merged.gtf -o /path/SampleA.GRCh38.gtf /path/SampleA.sorted.bam
# StringTie version 1.3.3
chr1    StringTie       transcript      11594   191502  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; cov "0.0"; FPKM "0.000000"; TPM "0.000000";
chr1    StringTie       exon    11594   14829   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "1"; cov "0.0";
chr1    StringTie       exon    14970   15038   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "2"; cov "0.0";
chr1    StringTie       exon    15796   16765   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "3"; cov "0.0";
chr1    StringTie       exon    16858   17055   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "4"; cov "0.0";
chr1    StringTie       exon    17233   17742   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "5"; cov "0.0";
chr1    StringTie       exon    17915   18061   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "6"; cov "0.0";
chr1    StringTie       exon    18268   19364   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "7"; cov "0.0";
chr1    StringTie       exon    189836  191502  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "8"; cov "0.0";
chr1    StringTie       transcript      11594   195411  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; cov "0.0"; FPKM "0.000000"; TPM "0.000000";
chr1    StringTie       exon    11594   14829   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "1"; cov "0.0";
chr1    StringTie       exon    14970   15236   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "2"; cov "0.0";
chr1    StringTie       exon    185758  187287  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "3"; cov "0.0";
chr1    StringTie       exon    187376  187577  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "4"; cov "0.0";
chr1    StringTie       exon    187755  187890  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "5"; cov "0.0";
chr1    StringTie       exon    188130  188266  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "6"; cov "0.0";
chr1    StringTie       exon    188439  188584  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "7"; cov "0.0";
chr1    StringTie       exon    188791  188902  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "8"; cov "0.0";
chr1    StringTie       exon    195263  195411  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "9"; cov "0.0";
chr1    StringTie       transcript      11594   197912  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.5"; cov "0.0"; FPKM "0.000000"; TPM "0.000000";

And SampleB.GRCh38.gtf looks like below:

# stringtie -e -B -p 8 -G /path/stringtie_output/stringtie_merged.gtf -o /path/SampleB.GRCh38.gtf /path/SampleB.sorted.bam
# StringTie version 1.3.3
chr1    StringTie       transcript      11594   191502  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; cov "0.0"; FPKM "0.000000"; TPM "1.000000";
chr1    StringTie       exon    11594   14829   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "1"; cov "0.0";
chr1    StringTie       exon    14970   15038   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "2"; cov "0.0";
chr1    StringTie       exon    15796   16765   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "3"; cov "0.0";
chr1    StringTie       exon    16858   17055   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "4"; cov "0.0";
chr1    StringTie       exon    17233   17742   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "5"; cov "0.0";
chr1    StringTie       exon    17915   18061   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "6"; cov "0.0";
chr1    StringTie       exon    18268   19364   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "7"; cov "0.0";
chr1    StringTie       exon    189836  191502  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.2"; exon_number "8"; cov "0.0";
chr1    StringTie       transcript      11594   195411  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; cov "0.0"; FPKM "0.000000"; TPM "3.000000";
chr1    StringTie       exon    11594   14829   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "1"; cov "0.0";
chr1    StringTie       exon    14970   15236   .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "2"; cov "0.0";
chr1    StringTie       exon    185758  187287  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "3"; cov "0.0";
chr1    StringTie       exon    187376  187577  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "4"; cov "0.0";
chr1    StringTie       exon    187755  187890  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "5"; cov "0.0";
chr1    StringTie       exon    188130  188266  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "6"; cov "0.0";
chr1    StringTie       exon    188439  188584  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "7"; cov "0.0";
chr1    StringTie       exon    188791  188902  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "8"; cov "0.0";
chr1    StringTie       exon    195263  195411  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.6"; exon_number "9"; cov "0.0";
chr1    StringTie       transcript      11594   197912  .       -       .       gene_id "MSTRG.7542"; transcript_id "MSTRG.7542.5"; cov "0.0"; FPKM "0.000000"; TPM "0.000000";

I want to extract only transcript from 3rd column and transcript_id which is 10th column and TPM which is the last column. But TPM needs to be the Sample names.

I want the output to be looked like below:

Type        transcript_id      SampleA      SampleB
transcript   MSTRG.7542.2      0.000000     1.000000
transcript   MSTRG.7542.6      0.000000     3.000000
transcript   MSTRG.7542.5      0.000000     1.000000
2
0

You will need to extract the relevant records from each file and write the result to two new temporary files (possibly using awk), possibly sorting it (with sort) at the same time (the sample files say they are sorted, but maybe not on the correct key). Here's an example of processing one of the files:

awk '$3 == "transcript" {printf("%s %s %s ", $3, $10, $12, $18);}' SampleA.GRCh38.gtf | sort -k 2 > tf1

Then you can use join to merge the two temporary/intermediate files generated by awk so that each record has the two final columns from each file.

Here's an example of a join command you might use:

join -o 1.1,1.2,1.3,2.3 -1 2 -2 2 tf1 tf2

You may want to print a header line (e.g. using the printf command) before running join, and you may want to replace spaces in the join output with tabs (e.g. using sed), or use another awk script to format the output.

From these examples you should be able to put together a script that will process both files and produce your desired output (and clean up the temporary files, etc.).

Note that depending on the size of the data files you may even be able to do everything in one awk (or python or perl, etc.) program (i.e. can all the selected data from both files be held easily in memory at once).

0

You can just join the files and then awk out those with NF==4 as only those lines you are interested in have an 18th field. All the other lines will only have 2 fields

Also Making certain assumptions about calculating the path to SampleB, however you can amend that to suit....

while IFS= read -r -d '' f; do                             #read the list of SampleA
        g=$(echo "$f" | sed "s/pleA/pleB/g")               #calculate path to SampleB
        if [[ -f "$g" ]]; then                             #check SampleB exists
                echo "$f" | sed "s/.*pleA\.//g"            #print sample No
                echo "Type transcript_id SampleA SampleB"  #print header
                                                           #do the join
                join -j 12 -o 1.3 -o 1.12 -o 1.18 -o2.18 <(sort -k 12 "$f") <(sort -k 12 "$g") | awk 'NF==4'
        fi   | sed 's/[;"]//g'| column -t                  #make it pretty
done < <(find . -type f -iname "*SampleA*" -print0)        #NULL separated list of SampleA
0

Tried with Below command

Step1

awk '$3 ~ /transcript/{print $0}' file1|awk '{print $3,substr($12,2,12),substr($NF,2,8)}' > out1

STEP2

awk '$3 == "transcript" {print substr($NF,2,8)}' file2  > out2

STEP3

paste out out1.txt | awk 'BEGIN{print "Type        transcript_id      SampleA      SampleB"}{print $0}'



Output

Type       transcript_id SampleA    SampleB
transcript MSTRG.7542.2 0.000000    1.000000
transcript MSTRG.7542.6 0.000000    3.000000
transcript MSTRG.7542.5 0.000000    0.000000

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.