Question

Normally, if I have to perform an overlap between multiple files, I just execute:

python -c 'import sys;print "".join(sorted(set.intersection(*[set(open(a).readlines()) for a in sys.argv[1:]])))' File1 File2 File3 File4 ....

But if I have to get this through a parser, how can I do it?

A short example will be enough around this script, and after that I can play around. Actually I want the user to give two input: Files on which ioperation is to be performed and the the operation here it is intersection.

Kindly help, instead of voting the post to close.

I think I have to show what I am doing, and here is an example of that:

usage = "Usage: %prog -i <file1>[,<file2>,...] -o <out> -ops "
version = "%prog " + str(VERSION)
parser = OptionParser(version=version, usage=usage)
group1 = OptionGroup(parser, 'Optional')

parser.add_option("-i", 
                  dest="featurefile", 
                  help="Gene list", 
                  metavar="FILE")
parser.add_option("-o", 
                  dest="outfile", 
                  help="output file (type determined by extension)", 
                  metavar="FILE")
# Optional arguments
group1.add_option("--op", 
                  dest="operation", 
                  help="Intersection, Union & Uniques (default Intersection)", 
                  default=None,
                  type="string")

parser.add_option_group(group1)
(options, args) = parser.parse_args()

Now this set is parser thing (done), now the second step is to get the operation (intersection) and the files and perform them. At this step I am lost.

Thank you

Was it helpful?

Solution

Since you are using Python 2.7, use the argparse module instead of the older optparse:

import argparse
import itertools as IT

VERSION = 0.1

version = "%(prog)s " + str(VERSION)
operation = {
    'intersection': set.intersection,
    'union': set.union,
    'uniques': set.difference}

parser = argparse.ArgumentParser()
parser.add_argument('--version', action='version', version=version)
parser.add_argument('-i', '--featurefiles', nargs='+', help='Gene list',
                    metavar='FILE')
parser.add_argument('-o', '--outfile', help="output file (type determined by extension)",
                    metavar='FILE')
parser.add_argument('--op', dest="operation", 
                    help="Intersection, Union & Uniques (default Intersection)",
                    default='intersection',
                    # can't use type=operation.get. See http://bugs.python.org/issue16516
                    type=lambda x: operation[x])  

args = parser.parse_args()
print(args)

if args.outfile:
    with open(args.outfile, 'w') as f:
        for group_size in (len(args.featurefiles), 2):
            for group in IT.combinations(args.featurefiles, group_size): 
                result = ''.join(sorted(reduce(
                    args.operation,
                    (set(open(filename))
                     for filename in group))))
                f.write('{g}:\n{r}\n'.format(g=str(group), r=result))

You could call the script from the CLI like this:

script.py -i file1 file2 file3 --op uniques -o /tmp/out

References to tools used above:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top