solution_merge.py 1.18 KB
Newer Older
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
# -*- 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')