Creative Commons License
This blog by Tommy Tang is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

My github papge

Tuesday, July 22, 2014

use python to change the header of a fasta file based on a dictionary in another file

I saw on SEQanwser, someone asked this question here, and I used python to resolve this problem.
I have not written in python for so long, I've been using R for a long time. This provide me a good practice opportunity.

The basic problem is to format some files, and I know it can be done by other tools like sed and awk ( see an awk solution below), but python just gives you much more flexibility in terms of what you can do and gives you more readability.
myfasta file

>comp1558_c0_seq1 len=204 path=[4976:0-203]
>comp8142_c0_seq1 len=222 path=[8835:0-221]
>comp8570_c0_seq1 len=232 path=[3344:0-232]

annotation file:

comp1558_c0_seq1 repressor protein
comp8142_c0_seq1 aspartate aminotransferase
comp8357_c0_seq1 l-xylulose reductase
comp8387_c0_seq1 succinyl- synthetase alpha
comp8570_c0_seq1 fatty acid synthase beta subunit dehydratase

see the code below:

>comp1558_c0_seq1 repressor protein
>comp8142_c0_seq1 aspartate aminotransferase
>comp8570_c0_seq1 fatty acid synthase beta subunit dehydratase

if the dictionary file has only two records for each line, this awk should work:
NR == FNR {
  rep[$1] = $2

    for (key in rep) {
      gsub(key, rep[key])

1 comment: