6
|
1 #! /usr/bin/env python
|
|
2 #
|
|
3 # Copyright INRA-URGI 2009-2010
|
|
4 #
|
|
5 # This software is governed by the CeCILL license under French law and
|
|
6 # abiding by the rules of distribution of free software. You can use,
|
|
7 # modify and/ or redistribute the software under the terms of the CeCILL
|
|
8 # license as circulated by CEA, CNRS and INRIA at the following URL
|
|
9 # "http://www.cecill.info".
|
|
10 #
|
|
11 # As a counterpart to the access to the source code and rights to copy,
|
|
12 # modify and redistribute granted by the license, users are provided only
|
|
13 # with a limited warranty and the software's author, the holder of the
|
|
14 # economic rights, and the successive licensors have only limited
|
|
15 # liability.
|
|
16 #
|
|
17 # In this respect, the user's attention is drawn to the risks associated
|
|
18 # with loading, using, modifying and/or developing or reproducing the
|
|
19 # software by the user in light of its specific status of free software,
|
|
20 # that may mean that it is complicated to manipulate, and that also
|
|
21 # therefore means that it is reserved for developers and experienced
|
|
22 # professionals having in-depth computer knowledge. Users are therefore
|
|
23 # encouraged to load and test the software's suitability as regards their
|
|
24 # requirements in conditions enabling the security of their systems and/or
|
|
25 # data to be ensured and, more generally, to use and operate it in the
|
|
26 # same conditions as regards security.
|
|
27 #
|
|
28 # The fact that you are presently reading this means that you have had
|
|
29 # knowledge of the CeCILL license and that you accept its terms.
|
|
30 #
|
|
31 """Explode wig files into several files, one for each chromosome"""
|
|
32
|
|
33 import os, re, sys
|
|
34 from optparse import OptionParser
|
|
35
|
|
36
|
|
37 if __name__ == "__main__":
|
|
38
|
|
39 # parse command line
|
|
40 description = "Wig Exploder v1.0.1: Explode a big WIG file into several smaller WIG files (one per chromosome). [Category: Personal]"
|
|
41
|
|
42 parser = OptionParser(description = description)
|
|
43 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file [compulsory] [format: file in WIG format]")
|
|
44 parser.add_option("-o", "--output", dest="output", action="store", default=None, type="string", help="output directory [compulsory] [format: directory]")
|
|
45 parser.add_option("-s", "--strand", dest="strand", action="store", default=None, type="string", help="strand of the input WIG file (if any) [format: choice (+, -)]")
|
|
46 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
|
|
47 (options, args) = parser.parse_args()
|
|
48
|
|
49 inputFile = open(options.inputFileName)
|
|
50
|
|
51 files = {}
|
|
52 file = None
|
|
53 trackLine = None
|
|
54 strand = ""
|
|
55 if options.strand != None:
|
|
56 strand = options.strand
|
|
57
|
|
58 for line in inputFile:
|
|
59 line = line.strip()
|
|
60
|
|
61 if line.startswith("track"):
|
|
62 trackLine = line
|
|
63 continue
|
|
64
|
|
65 m1 = re.search(r"^\s*fixedStep\s+chrom=(\S+)\s+start=\d+\s+step=\d+\s*$", line)
|
|
66 m2 = re.search(r"^\s*fixedStep\s+chrom=(\S+)\s+start=\d+\s+step=\d+\s+span=\d+\s*$", line)
|
|
67 m3 = re.search(r"^\s*variableStep\s+chrom=(\S+)\s*$", line)
|
|
68 m4 = re.search(r"^\s*variableStep\s+chrom=(\S+)span=\d+\s*$", line)
|
|
69
|
|
70 m = None
|
|
71 if m1 != None:
|
|
72 m = m1
|
|
73 elif m2 != None:
|
|
74 m = m2
|
|
75 elif m3 != None:
|
|
76 m = m3
|
|
77 elif m4 != None:
|
|
78 m = m4
|
|
79
|
|
80 if m != None:
|
|
81 chromosome = m.group(1)
|
|
82
|
|
83 if chromosome in files:
|
|
84 file = files[chromosome]
|
|
85 else:
|
|
86 file = open("%s%s%s%s.wig" % (options.output, os.sep, chromosome, strand), "w")
|
|
87 files[chromosome] = file
|
|
88 if trackLine != None:
|
|
89 file.write("%s\n" % (trackLine))
|
|
90
|
|
91 if file == None:
|
|
92 sys.exit("Header is missing (current first line is '%s')! Aborting..." % (line))
|
|
93
|
|
94 file.write("%s\n" % (line))
|
|
95
|
|
96 inputFile.close()
|
|
97
|
|
98 for chromosome in files:
|
|
99 files[chromosome].close()
|