add same string to the certain lines in python
2
1
Entering edit mode
6.3 years ago
horsedog ▴ 60

Hi, a stupid question from a python beginner I'd like to rename the query of my fasta file, here is my sequence :

>WP_015529149.1
MSMTGIILAAVVVGGTGLFIGVFLGIAGKKFAVKVDEREEAILGVLPGNNCGGCGYAGCSGLAAAIVKGEAEVSGCPVGG
APVAAKIGDIMGVAAGTQERQTAFVKCAGTCEKAILDYDYTGIQDCTMASMMQNGGAKGCNSGCLGFGSCVAACPFDAIH
VVDGIAVVDKEACKACGKCIAACPKHLIELIPYEQKTFVRCNSNAKGKVQLTICQAGCIGCRLCEKNCEAGAITVTNFLA
HIDADKCTECGVCVEKCPRKIITLR
>WP_055172573.1
MSMTGIILAAVVVGGTGLFIGVFLGIAGKKFAVKVDEREEAILGVLPGNNCGGCGYAGCSGLAAAIVKGEAEVSGCPVGG
APVAAKIGEIMGVAAGTQERQTAFVKCAGTCEKAILDYDYTGIQDCTMASMMQNGGAKGCNSGCLGFGSCVAACPFDAIH
VVDGIAVVDKEACKACGKCIAACPKHLIELIPYEQKTFVRCNSNAKGKIQLTICQAGCIGCRICEKNCEAGAITVTNFLA
HIDADKCTECGVCVEKCPRKIITLR

I would like to add string "_ABCDE" to the line with ">"
to get" >WP_015529149.1_ABCDE" and" >WP_055172573.1_ABCDE", if I use python how to achieve this?

python • 1.0k views
ADD COMMENT
3
Entering edit mode
6.3 years ago
Hussain Ather ▴ 990
with open("input.txt") as file:
    for i in file.readlines():
        if i.startswith(">"):
            print(str(i)+"_ABCDE")
        else:
            print(str(i))
ADD COMMENT
1
Entering edit mode

Looking good, although for biiiiig files you shouldn't use .readlines() because you are reading everything in memory, while you can just iterate over the file:

with open("input.txt") as f:
    for i in f:
        if i.startswith(">"):
            print(str(i)+"_ABCDE"
        else:
            print(str(i))
ADD REPLY
3
Entering edit mode
6.3 years ago
Russ ▴ 500

Another way (for fun) using sed in Ubuntu:

sed '/>/ s/$/_ABCDE/' file.txt

The />/ matches the ">" pattern, while s/$/_ABCDE/ replaces the end of the line with _ABCDE.

ADD COMMENT

Login before adding your answer.

Traffic: 2632 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6