annotate edger_dge_table_to_bedgraph @ 92:86c3aaa205b5 draft

Added bedgraph exporting function for the DGE tables
author yhoogstrate
date Fri, 13 Mar 2015 05:45:09 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
92
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
1 #!/usr/bin/env python
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
2
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
3 import re,sys,os,os.path,argparse,textwrap,datetime
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
4
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
5 class GTF:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
6 def __init__(self,filename,features=["exon"],symbol="gene_id"):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
7 self.features = features
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
8 if(re.match("^[a-zA-Z0-9_\-]+$",symbol)):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
9 self.symbol = symbol
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
10 else:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
11 raise ValueError('False symbol matching symbol: '+str(symbol))
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
12 self.index = {}
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
13 self.parse(filename)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
14
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
15 def parse(self,filename):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
16 with open(filename) as infile:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
17 for line in infile:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
18 self.parse_line(line)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
19
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
20 def parse_line(self,line):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
21 line = line.strip().split("\t")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
22 if(len(line) == 9):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
23 if(line[2] in self.features):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
24 gene_id = self.parse_column_9(line[8])
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
25 if(gene_id):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
26 if(not self.index.has_key(gene_id)):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
27 self.index[gene_id] = []
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
28 self.index[gene_id].append([line[0],line[3],line[4]])
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
29
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
30 def parse_column_9(self,line):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
31 query = self.symbol+'[ =]+([^ ;]+)'
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
32 m = re.search(query, line)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
33
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
34 if(m):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
35 return m.group(1).strip("'").strip('"')
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
36 else:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
37 return None
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
38
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
39 def get(self,key):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
40 try:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
41 return self.index[key]
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
42 except:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
43 return False
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
44
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
45
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
46 class EdgeR_table:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
47 def __init__(self,table,gtf,columns=[3,7]):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
48 self.index = {}
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
49 self.gtf = gtf
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
50 self.columns = columns
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
51 self.parse(table)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
52
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
53 def parse(self,filename):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
54 i = 0
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
55 with open(filename) as infile:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
56 for line in infile:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
57 if(i == 0):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
58 self.parse_header(line)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
59 else:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
60 self.parse_line(line)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
61 i += 1
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
62
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
63 def parse_header(self,line):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
64 params = line.strip().split("\t")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
65 if(params[1].lower().find("genes") == -1):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
66 raise ValueError('False header in file - no "genes" in 2nd colum: '+line)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
67 if(params[2].lower().find("logfc") == -1):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
68 raise ValueError('False header in file - no "logfc" in 3rd colum: '+line)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
69 if(params[3].lower().find("logcpm") == -1):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
70 raise ValueError('False header in file - no "logcpm" in 4th colum: '+line)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
71 if(params[4].lower().find("lr") == -1):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
72 raise ValueError('False header in file - no "lr" in 5th colum: '+line)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
73 if(params[5].lower().find("pvalue") == -1):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
74 raise ValueError('False header in file - no "pvalue" in 6th colum: '+line)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
75 if(params[6].lower().find("fdr") == -1):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
76 raise ValueError('False header in file - no "fdr" in 7th colum: '+line)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
77
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
78 def parse_line(self,line):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
79 line = line.strip().split("\t")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
80
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
81 if(len(line) == 7):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
82 gene_id = line[1].strip('"').strip("'")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
83 column_data = {}
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
84 for column in self.columns:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
85 if(column in [6,7]):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
86 column_data[column] = str(1.0 - float(line[column-1]))
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
87 else:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
88 column_data[column] = line[column-1]
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
89
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
90 locations = self.gtf.get(gene_id)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
91 if(not locations):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
92 print "Warning: no location found for gene "+gene_id
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
93 else:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
94 for location in locations:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
95 self.insert(location,column_data)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
96
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
97 def insert(self,location,data):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
98 chrom = location[0]
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
99 start = location[1]
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
100 end = location[2]
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
101
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
102 if(not self.index.has_key(chrom)):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
103 self.index[chrom] = {}
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
104
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
105 if(not self.index[chrom].has_key(start)):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
106 self.index[chrom][start] = {}
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
107
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
108 if(not self.index[chrom][start].has_key(end)):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
109 self.index[chrom][start][end] = []
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
110
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
111 self.index[chrom][start][end].append(data)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
112
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
113 def export(self,filenames={3:"log_cpm.txt",7:"fdr.txt"}):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
114 for column in self.columns:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
115 fh = open(filenames[column],"w")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
116
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
117 buf = False
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
118
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
119 for chrom in sorted(self.index.keys()):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
120 for start in sorted(self.index[chrom].keys()):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
121 for end in sorted(self.index[chrom][start].keys()):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
122 fh.write(chrom+"\t"+start+"\t"+end+"\t"+self.index[chrom][start][end][0][column]+"\n")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
123
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
124 fh.close()
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
125
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
126 os.system("sort -k1,1V -k2,2g -k3,3g '"+filenames[column]+"' > '"+filenames[column]+".sorted'")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
127
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
128
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
129 def remove_overlap_in_bedgraph(bedgraph_file_dirty,bedgraph_file_clean):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
130 fh = open(bedgraph_file_clean,"w")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
131 buf = False
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
132
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
133 with open(bedgraph_file_dirty,"r") as f:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
134 for line in f:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
135 cur = line.strip().split("\t")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
136 cur[1] = int(cur[1])
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
137 cur[2] = int(cur[2])
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
138
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
139 if(not buf):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
140 buf = cur
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
141 else:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
142 if(cur[0] == buf[0] and cur[1] <= buf[2] ):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
143 if(buf[1] == cur[1]): #is subset
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
144 newscore = (float(buf[3])+float(cur[3]))/2
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
145 buf[2] = cur[2]
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
146 buf[3] = newscore
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
147 else:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
148 c1 = buf[1]
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
149 c2 = cur[1]
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
150 c3 = min(buf[2],cur[2])
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
151 c4 = max(buf[2],cur[2])
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
152
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
153 fh.write(buf[0]+"\t"+str(c1)+"\t"+str(c2-1)+"\t"+str(buf[3])+"\n")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
154
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
155 newscore = (float(buf[3])+float(cur[3]))/2
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
156 #fh.write(buf[0]+"\t"+str(c2+1)+"\t"+str(c3)+"\t"+str(newscore)+"\tp2\n")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
157 #buf = [buf[0], c3+1 , c4 , cur[3]]
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
158
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
159 buf = [buf[0], c2 , c4 , cur[3]]
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
160
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
161 # find if buf is a subset -> if so, merge and send to buffer
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
162 # or find the overlapping region
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
163
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
164 # if current is overlapping with buffer; merge:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
165 ## [ ] < buf
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
166 ## [ ] < cur
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
167 ##
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
168 ## [ ] < buf
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
169 ## [ ] < cur
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
170 ## 111112222333333 << write 1 and 2 and keep 3 in buf
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
171 else:
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
172 fh.write(buf[0]+"\t"+str(buf[1])+"\t"+str(buf[2])+"\t"+str(buf[3])+"\n")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
173 buf=cur
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
174
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
175 fh.write(buf[0]+"\t"+str(buf[1])+"\t"+str(buf[2])+"\t"+str(buf[3])+"\n")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
176 fh.close()
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
177
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
178
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
179
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
180 if __name__ == "__main__":
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
181 parser = argparse.ArgumentParser()
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
182
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
183 parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,epilog="For more info please visit:\n<https://github.com/yhoogstrate/fuma>")
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
184
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
185 parser.add_argument("-t",help="CPM table to extract columns from",nargs=1,required=True)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
186 parser.add_argument("-g",help="GTF file used to extract genomic location",nargs=1,required=True)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
187
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
188 parser.add_argument("-c3",help="Output (bedgraph) for column 3 (logFC)",nargs="?",required=False)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
189 parser.add_argument("-c4",help="Output (bedgraph) for column 4 (logCPM)",nargs="?",required=False)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
190 parser.add_argument("-c5",help="Output (bedgraph) for column 5 (LR)",nargs="?",required=False)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
191 parser.add_argument("-c6",help="Output (bedgraph) for column 6 (PValue)",nargs="?",required=False)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
192 parser.add_argument("-c7",help="Output (bedgraph) for column 7 (FDR)",nargs="?",required=False)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
193
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
194 args = parser.parse_args()
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
195
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
196 #files = {3:"VCAP_logFC.hg19.bedgraph",7:"VCAP_fdr.hg19.bedgraph"}
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
197 files = {}
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
198
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
199 if(args.c3):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
200 files[3] = args.c3
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
201 if(args.c4):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
202 files[4] = args.c4
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
203 if(args.c5):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
204 files[5] = args.c5
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
205 if(args.c6):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
206 files[6] = args.c6
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
207 if(args.c7):
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
208 files[7] = args.c7
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
209
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
210 print "Parsing GTF file"
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
211 g = GTF(args.g[0])
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
212
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
213 print "Parsing EdgeR table"
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
214 e = EdgeR_table(args.t[0],g,files.keys())
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
215
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
216 print "Exporting raw bedgraph(s)"
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
217 e.export(files)
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
218
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
219 print "Removing overlapping entries in bedgraph(s)"
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
220 for key in files.keys():
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
221 remove_overlap_in_bedgraph(files[key]+".sorted",files[key])
86c3aaa205b5 Added bedgraph exporting function for the DGE tables
yhoogstrate
parents:
diff changeset
222 os.system("rm '"+files[key]+".sorted'")