forked from hassanfa/autoseq-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqdnaseq_to_bedgraph.py
executable file
·48 lines (42 loc) · 1.42 KB
/
qdnaseq_to_bedgraph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
from collections import OrderedDict
import pandas as pd
import begin
@begin.start(auto_convert=True)
def generate_wigs(qdnaseq_filename, copynumber_bedgraph_filename):
col_types = OrderedDict({
"chromosome": object,
"start": int,
"end": int,
"bases": float,
"gc": float,
"mappability": float,
"blacklist": float,
"residual": object,
"use": object,
"readcount": int,
"copynumber": object,
"segmented": object,
})
df = pd.read_table(qdnaseq_filename, dtype = col_types)
df_nonnull = df.loc[~(df["segmented"].isnull()),:]
curr_chrom = None
curr_start = None
curr_end = None
segments = []
curr_val = None
for index, row in df_nonnull.iterrows():
if curr_val != row["segmented"] or curr_chrom != row["chromosome"]:
if curr_start != None:
# Record previous:
segments.append((curr_chrom, curr_start, curr_end, curr_val))
# Start new:
curr_start = row["start"]
curr_chrom = row["chromosome"]
curr_val = row["segmented"]
curr_end = row["end"]
# Record final:
segments.append((curr_chrom, curr_start, curr_end, curr_val))
with open(copynumber_bedgraph_filename, 'w') as segfile:
for seg in segments:
print >> segfile, "\t".join([str(item) for item in seg])