-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_to_log2.py
More file actions
58 lines (44 loc) · 1.33 KB
/
Copy pathlinear_to_log2.py
File metadata and controls
58 lines (44 loc) · 1.33 KB
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
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
usage = """
Convert linear data to log2 data
Data to convert must be in the 3rd column of tab-separated file
@author: Caroline Esnault
Last update: 2018/09/23
"""
import os, sys
import argparse
import pandas as pd
import math
ap = argparse.ArgumentParser(usage=usage)
ap.add_argument('file_to_convert', help='File to convert')
ap.add_argument('--output', '-o', default='out', help='Output file, "out" by default')
args = ap.parse_args()
# input file format:
#chr1 1234 0.15
#chr1 1235 0.29
df = pd.read_table(args.file_to_convert, sep='\t')
values = df.iloc[:, 2]
def log2(df, values, outfile):
"""
Writes output file after converting the 'column' in 'file_to_convert'
Parameters
----------
df : DataFrame
Content of file to convert
values : list
list with the data to convert
outfile : str
path and name to the output file
"""
with open(outfile, 'w') as outputfile:
for i in range(len(values)):
if values[i]== 0:
logtwo = 0
else:
logtwo= math.log(values[i], 2)
outputfile.write("%s\t%s\t%s\n" %
(df.iloc[i][0], df.iloc[i][1], logtwo)
)
print(df.iloc[i][1])
log2(df, values, args.output)