1dedc382703e2e5498d34ee86a1c65b1ba488489
[htsworkflow.git] / scripts / elandseq
1 #!/usr/bin/env python
2 import optparse
3 import os
4 import sys
5
6 def extract_sequence(instream, outstream, start, end):
7   for line in instream:
8     record = line.split()
9     if len(record) > 1:
10       result = [record[0], record[1][start:end]]
11     else:
12       result = [record[0][start:end]]
13     outstream.write("\t".join(result))
14     outstream.write(os.linesep)
15       
16
17 def make_parser():
18   usage = "usage: %prog [options] infile [outfile]"
19
20   parser = optparse.OptionParser(usage)
21   parser.add_option("-e", "--extract", dest="slice",
22     default=":",
23     help="provide a python slice operator to select a portion of an eland file")
24   return parser
25
26 def main(argv):
27   parser = make_parser()
28
29   (opt, args) = parser.parse_args(argv)
30
31   if len(args) not in (0, 1, 2):
32     parser.error('incorrect number of arguments')
33
34   # get our slice coordinates
35   start, end = opt.slice.split(':')
36   if len(start) > 0:
37     start = int(start)
38   else:
39     start = None
40   if len(end) > 0:
41     end = int(end)
42   else:
43     end = None
44
45   # open infile
46   if len(args) > 0:
47     instream = open(args[0],'r')
48   else:
49     instream = sys.stdin
50
51   if len(args) > 1:
52     outstream = open(args[1],'w')
53   else:
54     outstream = sys.stdout
55
56   extract_sequence(instream, outstream, start, end)
57
58 if __name__ == "__main__":
59     sys.exit(main(sys.argv[1:]))
60