-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabundanceFromMetaBatDepth.py
159 lines (108 loc) · 4.2 KB
/
abundanceFromMetaBatDepth.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 19 16:11:27 2021
@author: boyang zhang [email protected]
"""
from pathlib import Path
import numpy as np
import pandas as pd
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-fadir", "--f", help=".fa file dir. e.g. /Users/Downloads/")
parser.add_argument("-depthdir", "--d", help="depth file dir. e.g. /Users/Result/")
args = parser.parse_args()
print( "fa file is in: {} depth file is in {} ".format(
args.f,
args.d
))
#mypath = '/Users/boyangzhang/Downloads/'
#myDepthPath = '/Users/boyangzhang/OneDrive - The Ohio State University/2021/Spring/demicResult/'
mypath = str(args.f)
myDepthPath = str(args.d)
count = 0
lineMax = 100
dictKmerToBin = {}
entries = Path(mypath)
for entry in entries.iterdir():
if entry.name[-2:] == "fa":
#print(entry.name)
curFaFile = entry.name
#print()
# Using readline()
file1 = open(mypath + curFaFile, 'r')
while True:
count += 1
# Get next line from file
line = file1.readline().strip()
if not line:
break
# if line is empty
# end of file is reached
if line[0] == ">":
#print("Line{}: {}".format(count, line))
dictKmerToBin[line[1:]] = curFaFile
file1.close()
#print(dictKmerToBin)
#print(dictKmerToBin)
def lineToAbundance(line, dictKmerToBin):
if line.split("\t")[0] not in dictKmerToBin:
#print("Cannot find bin for " + line.split("\t")[0])
return False, [], None, 0
print("find bin for " + line.split("\t")[0])
linesplited = line.split("\t")
lengthK = float(linesplited[1])
arrTemp = []
for i in range(3,len(linesplited), 1):
if i % 2 == 1:
#print(i)
arrTemp.append(float(linesplited[i]))
#print(arrTemp)
return True, arrTemp, dictKmerToBin[line.split("\t")[0]], lengthK
counter = 0
lineMax = 4
file2 = open(myDepthPath + "depth.txt", 'r')
dictBinLength = {}
dictSampleNames = []
dictAbund = {}
while True:
counter += 1
line = file2.readline()
if not line:
break
# Get next line from file
if counter == 1:
colNames = line.split("\t")
for colName in colNames:
#print(colName)
if colName[-3:] == "bam":
dictSampleNames.append(colName[:-4])
#print(dictAbund)
else:
#print(line.split("\t"))
findedBin, abundArr, bins, lengthOfK = lineToAbundance(line, dictKmerToBin)
if findedBin:
#print(abundArr)
#print(lengthOfK)
if bins not in dictBinLength:
dictBinLength[bins] = lengthOfK
else:
dictBinLength[bins] += lengthOfK
if bins not in dictAbund:
dictAbund[bins] = np.array(abundArr)*lengthOfK
else:
dictAbund[bins] += np.array(abundArr)*lengthOfK
for key, valueArr in dictAbund.items():
# print(key, '->', valueArr)
if key not in dictBinLength:
continue
valueArr /= dictBinLength[key]
dfNotScaled = pd.DataFrame.from_dict(dictAbund)
dfNotScaled.index=dictSampleNames
dfScaled = dfNotScaled.div(dfNotScaled.sum(axis=1), axis=0)
dfScaled.round(5).to_csv("relativeAbundance.csv", index=True)
# print(line)
# print(line.split("\t")[1])
# for col in line.split(" "):
# print(col)