annotate Contra/scripts/split_chromosome.py @ 3:94362f37962e

Uploaded
author fcaramia
date Thu, 13 Sep 2012 02:43:53 -0400
parents 7564f3b1e675
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
1 # ----------------------------------------------------------------------#
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
2 # Copyright (c) 2011, Richard Lupat & Jason Li.
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
3 #
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
4 # > Source License <
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
5 # This file is part of CONTRA.
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
6 #
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
7 # CONTRA is free software: you can redistribute it and/or modify
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
8 # it under the terms of the GNU General Public License as published by
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
10 # (at your option) any later version.
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
11 #
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
12 # CONTRA is distributed in the hope that it will be useful,
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
15 # GNU General Public License for more details.
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
16 #
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
17 # You should have received a copy of the GNU General Public License
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
18 # along with CONTRA. If not, see <http://www.gnu.org/licenses/>.
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
19 #
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
20 #
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
21 #-----------------------------------------------------------------------#
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
22 # Last Updated : 03 Sep 2011 15:00PM
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
23
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
24
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
25 import os
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
26
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
27 def splitByChromosome(destFolder):
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
28
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
29 try:
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
30 os.mkdir(destFolder + "chr/")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
31 except:
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
32 print "folder exist"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
33
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
34 inputfile = destFolder + "sample.BEDGRAPH"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
35 outputfile = destFolder + "chr/chr1.txt"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
36 file = open(inputfile,"r")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
37 output = open(outputfile,"w")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
38 check = "1"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
39
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
40 for row in file:
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
41 if row[0] == '#':
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
42 continue
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
43
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
44 cols = row.split()
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
45 chr = cols[0].strip("chr")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
46 if (chr != check):
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
47 output.close()
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
48 check = chr
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
49 output = open(destFolder+ "chr/chr"+check+".txt","w")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
50 output.write(row)
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
51
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
52 output.close()
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
53
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
54 #JLMod
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
55 def splitByChromosome3(infile):
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
56 destFolder = os.path.dirname(infile)+"/"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
57
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
58 try:
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
59 os.mkdir(destFolder + "chr/")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
60 except:
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
61 print "folder exist"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
62
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
63 #inputfile = destFolder + "sample.BEDGRAPH"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
64 inputfile=infile
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
65 outputfile = destFolder + "chr/chr1.txt"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
66 file = open(inputfile,"r")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
67 output = open(outputfile,"w")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
68 check = "1"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
69
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
70 for row in file:
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
71 cols = row.split()
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
72 chr = cols[0].strip("chr")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
73 if (chr != check):
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
74 output.close()
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
75 check = chr
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
76 output = open(destFolder+ "chr/chr"+check+".txt","w")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
77 output.write(row)
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
78
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
79 output.close()
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
80
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
81 def splitByChromosome2(folder):
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
82
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
83 try:
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
84 os.mkdir(folder + "target/")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
85 except:
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
86 print "folder exist"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
87
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
88 inputfile = folder + "geneRefCoverage.txt"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
89 outputfile = folder + "target/chr1.txt"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
90 file = open(inputfile,"r")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
91 output = open(outputfile,"w")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
92 check = "1"
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
93
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
94 for row in file:
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
95 cols = row.split()
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
96 chr = cols[0].strip("chr")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
97 if (chr != check):
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
98 output.close()
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
99 check = chr
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
100 output = open(folder+ "target/chr"+check+".txt","w")
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
101 output.write(row)
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
102
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
103 output.close()
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
104
7564f3b1e675 Uploaded
fcaramia
parents:
diff changeset
105