Commit 4baa096e authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

Add method to merge cams to csv file

parent 3118a54d
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
import glob
import csv
def merge_cams_to_csv(directory, csvfile='merged_cams.csv'):
"""Merge *cam.txt files from a directory to a csv."""
# Add dir separator to the end if not present
directory = directory + '/' if directory[-1] != '/' else directory
csv_data = list()
# Read all files in the given directory
for filename in glob.glob(directory + '*_cam.txt'):
#print(filename)
# Extract the formula from the filename
# ex:
# # ['./result/Whole NCI-PID database translated into CADBIOM formalism(and)', 'TGFB1', 'cam.txt']
formula = ''.join(filename.split('_')[1:-1])
# Read the content of the cam file & memorize this content
with open(filename) as fd:
# Add formula column, before each cam to futur csv file
csv_data.append([[formula] + [line.rstrip('\n')] for line in fd])
# Write the final csv
with open(directory + csvfile, 'w') as fd:
writer = csv.writer(fd, delimiter=';')
writer.writerows(*csv_data)
if __name__ == "__main__":
merge_cams_to_csv('result')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment