annotate tools/mytools/shuffleBed.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 simulate a random interval set that mimics the size and strand of a reference set
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 def inferSizeFromRefBed(filename,header):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 read reference interval set, get chrom size information
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 chrSize = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 f = open(filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 if header:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 header = f.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 for line in f:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 flds = line.strip().split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 if not chrSize.has_key(flds[0]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 chrSize[flds[0]] = int(flds[2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 elif chrSize[flds[0]] < int(flds[2]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 chrSize[flds[0]] = int(flds[2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 f.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 return chrSize
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 def getChrSize(filename):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 chrSize = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 f = open(filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 for line in f:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 flds = line.strip().split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 if len(flds) >1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 chrSize[flds[0]] = int(flds[1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 f.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 return chrSize
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 def makeWeightedChrom(chrSize):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 make a list of chr_id, the freq is proportional to its length
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 genome_len = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 for chrom in chrSize:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 chrom_len = chrSize[chrom]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 genome_len += chrom_len
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 weighted_chrom = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 for chrom in chrSize:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 weight = int(round(1000*float(chrSize[chrom])/genome_len))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 weighted_chrom += [chrom]*weight
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 return weighted_chrom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 def randomIntervalWithinChrom(infile,outfile,chrSize,header):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 fin = open(infile)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 if header:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 header = fin.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 fout = open(outfile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 n = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 for line in fin:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 n = n + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 flds = line.strip().split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 interval_size = int(flds[2]) - int(flds[1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 rstart = random.randint(0,chrSize[flds[0]]-interval_size)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 fout.write(flds[0]+'\t'+str(rstart)+'\t'+str(rstart+interval_size)+'\t'+str(n)+'\t0\t+\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 fin.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 fout.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 def randomIntervalAcrossChrom(infile,outfile,chrSize,weighted_chrom,header):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 fin = open(infile)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 if header:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 header = fin.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 fout = open(outfile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 n = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 for line in fin:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 n = n + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 flds = line.strip().split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 interval_size = int(flds[2]) - int(flds[1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 # find a random chrom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 flds[0] = weighted_chrom[random.randint(0, len(weighted_chrom) - 1)]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 # random start in the chrom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 rstart = random.randint(0,chrSize[flds[0]]-interval_size)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 fout.write(flds[0]+'\t'+str(rstart)+'\t'+str(rstart+interval_size)+'\t'+str(n)+'\t0\t+\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 fin.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 fout.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 import sys,random
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 # python random_interval.py test100.bed testout.bed across header human.hg18.genome
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 reference_interval_file = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 output_file = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 across_or_within_chrom = sys.argv[3] # within or across
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 if sys.argv[4] == 'header':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 header = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 header = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 if len(sys.argv) == 6:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 chrom_size_file = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 chrSize = getChrSize(chrom_size_file)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 chrSize = inferSizeFromRefBed(reference_interval_file,header)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 if across_or_within_chrom == 'within':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 randomIntervalWithinChrom(reference_interval_file,output_file,chrSize,header)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 randomIntervalAcrossChrom(reference_interval_file,output_file,chrSize,makeWeightedChrom(chrSize),header)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 main()