This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extract the targeted txt using python

Hi, I'm beginner to python, here I have a very basic question about extracting targeted text. I have thousands of strings like this :

>ref|WP_070076791.1| iron-sulfur protein [Acinetobacter proteolyticus]

Here I only need WP_070076791.1, so I write a script in python:

data = open("data.fasta").read()

import re

for line in data:

 start = line.startswith(">ref|")

 end = line.endswith("| ")

 number = re.search(r'start(.*?)end',line)

print(number)

But it gives me "none", does anybody have idea?

python

I added code markup to your post for increased readability. You can do this by selecting the text and clicking the 101010 button. When you compose or edit a post that button is in your toolbar, see image below:

101010 Button

3 answers

Do you only need what would be in the position of each header in the fasta, that WP_070076791.1 is in?

with open('data.fasta', 'r') as f:
    for line in f:
        if line.startswith('>'):
            print line.strip().split('|')[1]

if this isn't an assignment, and you can use other options:

grep -e '>' data.fasta | cut -f 2 -d '|'

If you don't need to use Python, you can use grep with awk:

$ grep '^>' data.fasta | awk -v FS="|" '{ print $2; }' > result.txt

If you have to use Python:

#!/usr/bin/env python

import sys

for line in sys.stdin:
    if line.startswith('>'):
        line = line.strip()
        elems = line.split('|')
        sys.stdout.write("%s\n" % (elems[1]))

You could use it like so:

$ ./filter.py < data.fasta > result.txt
start = line.startswith(">ref|")

startsWith returns a boolean not an index/integer: https://docs.python.org/2/library/stdtypes.html

I think you're looking for find

Log in to answer this question.