<commit_before>#!/usr/bin/env python
from __future__ import print_function
import sys
import argparse
import csv
import os.path
from signal import signal, SIGPIPE, SIGINT, SIG_DFL
signal(SIGPIPE, SIG_DFL)
signal(SIGINT, SIG_DFL)

from Bio import SeqIO

version = '1.3.0'
date = 'December 20, 2016'

def eprint(*args, **kwargs):
    # print to STDERR; for progress messages
    print(*args, file=sys.stderr, **kwargs)

def extant_file(x):
    """
    'Type' for argparse - checks that file exists but does not open.
    """
    if not os.path.exists(x):
        # Argparse uses the ArgumentTypeError to give a rejection message like:
        # error: argument input: x does not exist
        raise argparse.ArgumentTypeError("{0} does not exist".format(x))
    return x

class PrintVersion(argparse.Action):
    def __init__(self, nargs=0, **kwargs):
        if nargs != 0:
            raise ValueError('nargs for PrintVersion must be 0; it is just a flag.')
        super(PrintVersion, self).__init__(nargs=nargs, **kwargs)
    def __call__(self, parser, values, namespace, option_string=None):
        eprint('{} version {}'.format(__file__,version))
        eprint('Submitted {} to github'.format(date))
        parser.exit()

parser = argparse.ArgumentParser(description='Extract sequence based on ID matching from a FASTA or Genbank file.')
parser.add_argument('infile',help='Sequence file (FASTA or Genbank) of interest.',type=extant_file)
parser.add_argument('--raw',help='List of locus tags, not file with list of locus tags',action='store_true',default=False)
parser.add_argument('listfiles',help='File or list (if --raw is invoked) of IDs of genes of interest to extract; \'all\' will print all records in file; - will take either filenames or list from STDIN',type=str,nargs='+')
parser.add_argument('--filetype',help='File type provided (default = auto). FASTQ parsing not recommended with this tool.',default='auto',choices=['FASTA','FASTQ','GBK','auto'])
parser.add_argument('--ffn',help='Print nucleotide sequence for each CDS of a genbank file (gbk to ffn conversion)',action='store_true',default=False)
parser.add_argument('--fna',help='Print nucleotide sequence for the source sequence of a genbank file (can select records using the VERSION ID as well) (gbk to fna conversion, use in conjunction with \'all\' positional argument)',action='store_true',default=False)
parser.add_argument('--gbk',help='Print genbank output from genbank file. Cannot convert FASTA to genbank format.',action='store_true',default=False)
parser.add_argument('--outname',help='Type of identifier to print to output from genbank file (default = locus_tag)',choices=['locus_tag','protein_id','gene'],default='locus_tag',type=str)
parser.add_argument('--searchname',help='Type of identifier to search in genbank file (default = locus_tag)',choices=['locus_tag','protein_id','gene'],default='locus_tag',type=str)
parser.add_argument('--us',help='Extract this length of sequence from the upstream region of a gene',type=int)
parser.add_argument('--ds',help='Extract this length of sequence from the downstream region of a gene',type=int)
parser.add_argument('--matchtype',help='Include exact (default) or inexact matches. Inexact matching takes longer.',choices=['exact','inexact'],default='exact',type=str)
parser.add_argument('-v','--verbose',help='Print progress messages. Helpful for debugging or double-checking output',action='store_true')
parser.add_argument('--nodesc',help='Print only sequence ID in output, not description (for FASTA files).',action='store_true')
parser.add_argument('-V','--version',help='Print version message and quit.',action=PrintVersion)
parser.add_argument('--gi',help='Print GI number for matches.',action='store_true')
args = parser.parse_args()
tags = {}
matches = {}
count = 0

def parse_opts(x, y):
    x.all = False
    if x.listfiles[0] == 'all':
        x.all = True
    else:
        if x.listfiles[0] == '-':
            x.listfiles = [z.strip() for z in sys.stdin.read().split()]
            x.listfiles = filter(None, x.listfiles)
        if x.raw == False:
            for listfile in x.listfiles:
                with open(listfile) as l:
                    for line in l:
                        line = line.rstrip('\n')
                        y[line] = 1
        else:
            for tag in args.listfiles:
                y[tag] = 1
    if x.filetype == 'auto':
        with open(x.infile, 'r') as f:
            line = f.readline().strip()
            if line[0] == '>':
                x.filetype = 'FASTA'
            elif line[0] == '@':
                x.filetype = 'FASTQ'
            elif 'LOCUS' in line:
                x.filetype = 'GBK'
            else:
                sys.exit('Unable to determine filetype of {}. Quitting...\n'.format(x.infile))


def parse_fasta(x, y):
    k = 0
    infile = SeqIO.parse(x, y)
    for record in infile:
        k += 1
        if args.all == True or args.matchtype == 'exact' and record.id in tags:
            matches[record.id] = 1
        elif args.matchtype == 'inexact':
            for tag in tags:
                if tag in record.id:
                    matches[record.id] = tag
                    break
        if record.id in matches:
            if args.nodesc:
                print('>' + record.id)
                print(record.seq)
            else:
                SeqIO.write(record, sys.stdout, y)
    return k

def parse_genbank(x):
    k = 0
    infile = SeqIO.parse(x, 'genbank')
    if not args.fna and not args.gbk:
        for record in infile:
            for feature in record.features:
                if feature.type == 'CDS':
                    k += 1
                    locus_tag = feature.qualifiers['locus_tag'][0]
                    try:
                        protein_id = feature.qualifiers['protein_id'][0]
                    except KeyError:
                        protein_id = 'NULL'
                    try:
                        gene = feature.qualifiers['gene'][0]
                    except KeyError:
                        gene = 'NULL'
                    outname = locus_tag
                    searchname = locus_tag
                    if args.searchname == 'protein_id':<commit_msg>Added check for pseudogenes when printing data from genbank file<commit_after>#!/usr/bin/env python
from __future__ import print_function
import sys
import argparse
import csv
import os.path
from signal import signal, SIGPIPE, SIGINT, SIG_DFL
signal(SIGPIPE, SIG_DFL)
signal(SIGINT, SIG_DFL)

from Bio import SeqIO

version = '1.3.0'
date = 'December 20, 2016'

def eprint(*args, **kwargs):
    # print to STDERR; for progress messages
    print(*args, file=sys.stderr, **kwargs)

def extant_file(x):
    """
    'Type' for argparse - checks that file exists but does not open.
    """
    if not os.path.exists(x):
        # Argparse uses the ArgumentTypeError to give a rejection message like:
        # error: argument input: x does not exist
        raise argparse.ArgumentTypeError("{0} does not exist".format(x))
    return x

class PrintVersion(argparse.Action):
    def __init__(self, nargs=0, **kwargs):
        if nargs != 0:
            raise ValueError('nargs for PrintVersion must be 0; it is just a flag.')
        super(PrintVersion, self).__init__(nargs=nargs, **kwargs)
    def __call__(self, parser, values, namespace, option_string=None):
        eprint('{} version {}'.format(__file__,version))
        eprint('Submitted {} to github'.format(date))
        parser.exit()

parser = argparse.ArgumentParser(description='Extract sequence based on ID matching from a FASTA or Genbank file.')
parser.add_argument('infile',help='Sequence file (FASTA or Genbank) of interest.',type=extant_file)
parser.add_argument('--raw',help='List of locus tags, not file with list of locus tags',action='store_true',default=False)
parser.add_argument('listfiles',help='File or list (if --raw is invoked) of IDs of genes of interest to extract; \'all\' will print all records in file; - will take either filenames or list from STDIN',type=str,nargs='+')
parser.add_argument('--filetype',help='File type provided (default = auto). FASTQ parsing not recommended with this tool.',default='auto',choices=['FASTA','FASTQ','GBK','auto'])
parser.add_argument('--ffn',help='Print nucleotide sequence for each CDS of a genbank file (gbk to ffn conversion)',action='store_true',default=False)
parser.add_argument('--fna',help='Print nucleotide sequence for the source sequence of a genbank file (can select records using the VERSION ID as well) (gbk to fna conversion, use in conjunction with \'all\' positional argument)',action='store_true',default=False)
parser.add_argument('--gbk',help='Print genbank output from genbank file. Cannot convert FASTA to genbank format.',action='store_true',default=False)
parser.add_argument('--outname',help='Type of identifier to print to output from genbank file (default = locus_tag)',choices=['locus_tag','protein_id','gene'],default='locus_tag',type=str)
parser.add_argument('--searchname',help='Type of identifier to search in genbank file (default = locus_tag)',choices=['locus_tag','protein_id','gene'],default='locus_tag',type=str)
parser.add_argument('--us',help='Extract this length of sequence from the upstream region of a gene',type=int)
parser.add_argument('--ds',help='Extract this length of sequence from the downstream region of a gene',type=int)
parser.add_argument('--matchtype',help='Include exact (default) or inexact matches. Inexact matching takes longer.',choices=['exact','inexact'],default='exact',type=str)
parser.add_argument('-v','--verbose',help='Print progress messages. Helpful for debugging or double-checking output',action='store_true')
parser.add_argument('--nodesc',help='Print only sequence ID in output, not description (for FASTA files).',action='store_true')
parser.add_argument('-V','--version',help='Print version message and quit.',action=PrintVersion)
parser.add_argument('--gi',help='Print GI number for matches.',action='store_true')
args = parser.parse_args()
tags = {}
matches = {}
count = 0

def parse_opts(x, y):
    x.all = False
    if x.listfiles[0] == 'all':
        x.all = True
    else:
        if x.listfiles[0] == '-':
            x.listfiles = [z.strip() for z in sys.stdin.read().split()]
            x.listfiles = filter(None, x.listfiles)
        if x.raw == False:
            for listfile in x.listfiles:
                with open(listfile) as l:
                    for line in l:
                        line = line.rstrip('\n')
                        y[line] = 1
        else:
            for tag in args.listfiles:
                y[tag] = 1
    if x.filetype == 'auto':
        with open(x.infile, 'r') as f:
            line = f.readline().strip()
            if line[0] == '>':
                x.filetype = 'FASTA'
            elif line[0] == '@':
                x.filetype = 'FASTQ'
            elif 'LOCUS' in line:
                x.filetype = 'GBK'
            else:
                sys.exit('Unable to determine filetype of {}. Quitting...\n'.format(x.infile))


def parse_fasta(x, y):
    k = 0
    infile = SeqIO.parse(x, y)
    for record in infile:
        k += 1
        if args.all == True or args.matchtype == 'exact' and record.id in tags:
            matches[record.id] = 1
        elif args.matchtype == 'inexact':
            for tag in tags:
                if tag in record.id:
                    matches[record.id] = tag
                    break
        if record.id in matches:
            if args.nodesc:
                print('>' + record.id)
                print(record.seq)
            else:
                SeqIO.write(record, sys.stdout, y)
    return k

def parse_genbank(x):
    k = 0
    infile = SeqIO.parse(x, 'genbank')
    if not args.fna and not args.gbk:
        for record in infile:
            for feature in record.features:
                if feature.type == 'CDS':
                    k += 1
                    locus_tag = feature.qualifiers['locus_tag'][0]
                    if 'pseudo' in feature.qualifiers:
                        eprint('{} is a pseudogene. Skipping.'.format(locus_tag))
                        continue
                    try:
                        protein_id = feature.qualifiers['protein_id'][0]
                    except KeyError:
                        protein_id = 'NULL'
                    try:
                        gene = feature.qualifiers['gene'][0]
                    except KeyError:
                        gene = 'NULL'
                    outname = locus_tag
                    searchname = locus_tag
                    if args.searchname == 'protein_id':
{'fraction_non_alphanumeric': 0.12416239653133622, 'fraction_numerical': 0.004414662987780844, 'mean_word_length': 2.618368511123788, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4354814', 'n_tokens_mistral': 3792, 'n_tokens_neox': 3622, 'n_words': 1078}