Skip to content

Instantly share code, notes, and snippets.

@terrycojones
Created June 5, 2014 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrycojones/2cdf9358aa1f91aa7f53 to your computer and use it in GitHub Desktop.
Save terrycojones/2cdf9358aa1f91aa7f53 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
from collections import defaultdict
def parseColors(colors):
"""
Parse read id color specification.
@param colors: A C{list}, each of whose elements is a C{str} such as
'green id1 id2', where each of id1, id2 etc. is either a read
identifier or the name of a file containing read identifiers one
per line.
@return: A C{dict} whose keys are colors and whose values are sets of
read identifiers.
"""
result = defaultdict(set)
for colorInfo in colors:
readIds = colorInfo.split()
color = readIds.pop(0)
for readId in readIds:
if os.path.isfile(readId):
with open(readId, 'r') as fp:
for line in fp:
result[color].add(line.rstrip())
else:
result[color].add(readId)
return result
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--color', action='append')
args = parser.parse_args()
for color, ids in parseColors(args.color).iteritems():
print '%s: %s' % (color, ids)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment