gravatar for Joe

1 hour ago by

United Kingdom

This line is your problem:

            if "class:2" in record.description():

record.description is not a function, it is an object (a string), so you're getting the error that you cannot 'call' a string (which is correct).

You don't need the brackets.

There are a few other issues:

import sys 
import argparse 
import operator
import re
import itertools
from Bio import SeqIO

def main (argv):
    parser = argparse.ArgumentParser(description='find a location')
    parser.add_argument('infile', help='file to process')
    parser.add_argument('outfile', help='file to produce')
    args = parser.parse_args()

    with open(args.outfile,"w") as of:
        for record in SeqIO.parse(args.infile,"fasta"):
            if "class:2" in record.description():
                of.write(">"+record.id)
                of.write(record.seq)

if __name__ == "__main__":
    main(sys.argv)

You don't need to use sys.argv if you're using argparse - use one or the other, unless you have a very specific need to use both. Consequently, you don't need to pass sys.argv through main(), so the code would look better in general as:

import argparse 
from Bio import SeqIO

def parse_args():
    parser = argparse.ArgumentParser(description='find a location')
    parser.add_argument('infile', help='file to process')
    parser.add_argument('outfile', help='file to produce')

    return parser.parse_args()


def main():
    args = parse_args()

    with open(args.outfile,"w") as of:
        for record in SeqIO.parse(args.infile,"fasta"):
            if "class:2" in record.description:
                of.write(">"+record.id)
                of.write(record.seq)

if __name__ == "__main__":
    main()

link

modified 1 hour ago

written
1 hour ago
by

Joe16k



Source link