1

I have multiple samples named p1_500sc.fasta, p2_500sc.fasta... Each file loks like this:

>NODE_60_length_504_cov_1.580902
TATATCGCCGTAGATAGACGAATACGGGATTTTGAAATGACTGATATATTCAAGCACATC
CTGTTCAGTCCAAAATGACATCGGCTTCGATTTCGGGATTGCCCCCGCAAAGTTGTTACA
>NODE_61_length_503_cov_4.457447
GATATGGGCCGGTGAGCATGCTGTCACATTTTGGGCAGGTGCCGAGGATTATGAGCTCGT
CTTCCGGCGTCAACGCTTGTTCGTTGCGTCTGGTGATGTGTTCCAGAGCGGCATAGTCGT
>NODE_62_length_500_cov_4.037534
CTCGTTCTTGCACTCCTGAATGAAGCGAATCTCTGACGAGGGTATTCCATAATCCTCTAT
CAGCTTACGCTTGATTTCTG
>NODE_63_length_500_cov_1.718499
GGAGGATATCAATGCCCTTATACTGCTTCTCAACAGTAGGAGTAATGCGGATACCCAAAA
CGGTCTTAGGGTTCTCCAGCAACTTCATGGCATTCCAACGCTTCAAGTCATCCATGCGGA
AGCCTTCGGCT

As You see always there is a NODE phrase. For each file I would like to change "NODE" for the file indicator from the sample's name: p1/ p2/ p100...pn

The output should look like this for file p1_500sc.fasta:

>p1_60_length_504_cov_1.580902
TATATCGCCGTAGATAGACGAATACGGGATTTTGAAATGACTGATATATTCAAGCACATC
CTGTTCAGTCCAAAATGACATCGGCTTCGATTTCGGGATTGCCCCCGCAAAGTTGTTACA
>p1_61_length_503_cov_4.457447
GATATGGGCCGGTGAGCATGCTGTCACATTTTGGGCAGGTGCCGAGGATTATGAGCTCGT
CTTCCGGCGTCAACGCTTGTTCGTTGCGTCTGGTGATGTGTTCCAGAGCGGCATAGTCGT
>p1_62_length_500_cov_4.037534
CTCGTTCTTGCACTCCTGAATGAAGCGAATCTCTGACGAGGGTATTCCATAATCCTCTAT
CAGCTTACGCTTGATTTCTG
>p1_63_length_500_cov_1.718499
GGAGGATATCAATGCCCTTATACTGCTTCTCAACAGTAGGAGTAATGCGGATACCCAAAA
CGGTCTTAGGGTTCTCCAGCAACTTCATGGCATTCCAACGCTTCAAGTCATCCATGCGGA
AGCCTTCGGCT

For p123_500sc.fasta file

>p123_2_length_456_cov_4.453333
GATATGGGCCGGTGAGCATGCTGTCACATTTTGGGCAGGTGCCGAGGATTATGAGCTCGT
CTTCCGGCGTCAACGCTTGTTCGTTGCGTCTGGTGATGTGTTCCAGAGCGGCATAGTCGT
>p123_356_length_3_cov_4.037908
CTCGTTCTTGCACTCCTGAATGAAGCGAATCTCTGACGAGGGTATTCCATAATCCTCTAT
CAGCTTACGCTTGATTTCTG

The same for any other sample. Sample start from p1, and they can reach p150.

2

bash + sed solution:

for f in p[0-9]*.fasta; do
    pfx="${f%%_*}"
    sed -i "s/^>NODE/>$pfx/" "$f"
done

  • pfx="${f%%_*}" - truncate the right part of the filename till the last _ ; assign the result to variable pfx
  • sed -i "s/^>NODE/>$pfx/" - substitute >NODE substring with $pfx variable in-place
  • Should i create .sh script? How to run it. Sorry for asking but I am a begginer. – k_a_r_o_l Jun 7 '18 at 11:11
  • Place it into shell script for multiline version. For throw-away one-liner - just run for f in p[0-9]*.fasta; do pfx="${f%%_*}"; sed -i "s/^>NODE/>$pfx/" "$f"; done in the command line – RomanPerekhrest Jun 7 '18 at 11:19
1

With GNU awk, you can do it in a single command invocation:

gawk -i inplace -F_ -v OFS=_ '
  BEGINFILE {
    node = FILENAME
    sub(".*/", "", node)
    sub("_.*", "", node)
  }
  $1 == ">NODE" {$1 = ">" node}
  {print}' ./p*_*.fasta
  • some shorter one: gawk -i inplace -F_ -v OFS=_ 'BEGINFILE { node = FILENAME; gsub(/.*\/|_.*/, "", node) }$1 == ">NODE"{ $1 = ">" node }1' ./p*_*.fasta – RomanPerekhrest Jun 7 '18 at 11:24
0

This will loop through each file, assign the everything before the underscore in the filename to the variable sample, then subsitute >NODE for $sample in the file using sed:

for file in /path/to/files/*;do
  sample=$(echo "${file}" | sed s/_.*//)
  sed -i "s/^>NODE/${sample}/" ${file}
done

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.