-
Notifications
You must be signed in to change notification settings - Fork 1
/
WIG to CSV (Try 2)
35 lines (35 loc) · 1.92 KB
/
WIG to CSV (Try 2)
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
"""
Switch if/elif/else:This program will encode a wig file 'GSM730535_WT_NoPi_Nucleosome.wig' to a list with (position, data_value, chromosome number) as separate elements
Data from Zhou Xu, 2011 (https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSM730535)
"""
data=[]
update_line=["position", "data_value", "chromosome number"]
with open('GSM730535_WT_NoPi_Nucleosome.wig', 'r') as f: # Assign f as opening the wig file
for line in f:
try:
if int(line[0]) > -1:
# for loop to split the position and data_value information from line
position = ""
m=[]
for i in range(len(line)):
try: # get the position seperated
int(line[i])
position = position + line[i]
except ValueError: # run when hits the first space
data_value = line[i+1:(-1)] # get the data_value
break
update_line[0] = position
update_line[1] = data_value
m = update_line + m
data.append(m) # add a line (position/data_value, chromosome number) to list "data"
except ValueError:
if "name" in line:
continue # remove the track line/definition line
else:
#if "chr1" in line or "chr2" in line or "chr3" in line or "chr4" in line or "chr5" in line or "chr6" in line or "chr7" in line or "chr8" in line or "chr9" in line or "chr10" in line or "chr11" in line or "chr12" in line or "chr13" in line or "chr14" in line or "chr15" in line or "chr16" in line:
num = line[19:24] # assign num as "chr# " or "chr##"
update_line[2]= num # update the second element of the list "update_list" as the chromosome number
import csv
with open('practice.csv', 'w', newline = '') as h:
writer = csv.writer(h)
writer.writerows(data)