-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (44 loc) · 1.57 KB
/
main.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
import csv
import os
import re
def naturalSort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanumericKey = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanumericKey)
def getFilePaths(basePath):
files = []
with os.scandir(basePath) as entries:
for entry in entries:
if entry.is_file():
files.append(entry.path)
return files
def getSampleName(filePath: str):
return filePath.split('/')[-1].split('_', 1)[0]
def getPeakAreasFromTsv(filePath):
peakAreas = []
with open(filePath, 'r') as tsvFile:
tsvReader = csv.reader(tsvFile, delimiter='\t')
areaRowIndex = -1
i = 0
for row in tsvReader:
if len(row) > 0 and row[0] == '[area]':
areaRowIndex = i
if areaRowIndex != -1 and i >= areaRowIndex + 2:
if len(row) == 0:
break
peakAreas.append((row[0], row[-2]))
i += 1
return peakAreas
basePath = 'data/H3_04_15_27'
files = getFilePaths(basePath)
results = {}
for filePath in files:
sampleName = getSampleName(filePath)
results[sampleName] = getPeakAreasFromTsv(filePath)
keys = naturalSort(list(results.keys()))
with open(basePath + '/results.tsv', 'w') as outputFile:
outputWriter = csv.writer(outputFile, delimiter='\t')
outputWriter.writerow(['sample', 'peptide', 'peak_area'])
for key in keys:
for peak in results[key]:
outputWriter.writerow([key, peak[0], peak[1]])