#! usr/bin/python import sys import os import re import csv def read_tabular(filename): table = [] with open(filename) as tsv: for line in csv.reader(tsv, delimiter = '\t'): table.append(line) return(table) """ jvenn_html_modder headers /pathto/table1 col1 /pathto/table2 col2 [/pathto/table3] [col3] [/pathto/table4] [col4] [/pathto/table5] [col5] [/pathto/table6] [col6] /pathto/galaxy /pathto/output script_name int string int string int [string] [int] [string] [int] [string] [int] [string] [int] string string 0 1 2 3 4 5 6 7 8 9 10 11 12 13 len-2 len-1 This script will read columns at given tables and insert them in the custom jvenn template html page. The lists will be processed by the javascript in the page and generate diagrams. """ argv = sys.argv #Path to the tool's original path, to be modified for a different location: res_dir = argv[len(argv)-2] + "/tools/MyTools/vennDiagram/" output_file = argv[len(argv)-1] #Import module from tool directory: sys.path.append(res_dir + "/HTML") import HTML assert(len(argv) >= 6), "Not enough arguments." assert(int(argv[1]) == 0 or int(argv[1]) == 1), "Headers argument (1) must be 0/1" #Boolean from argument 1: headers = True if(int(argv[1]) == 0): headers = False nb_lists = (len(argv)-4)/2 columns = [] colnames = [] tables = [] for a in range(1, nb_lists+1): #every even numbered argument after headers is a filename, every odd one is a column index assert(isinstance(argv[a*2], str)) assert(isinstance(int(argv[(a*2)+1]), int)) #Open file with system cut command and extract columns and split lines: tables.append(read_tabular(argv[a*2])) #Select column: col = [tables[a-1][i][int(argv[(a*2)+1])-1] for i in range(len(tables[a-1]))] #PFM if headers: #Extract header: colnames.append(col.pop(0)) #Rejoin elements of list with the newline character for fields in html plain text: columns.append( " ".join(col) ) del tables template = open( (res_dir + "/jvenn.html"), "r") output = open( output_file, "w") #Parse template html file: line = template.readline() #First line: while line != "": modded = False #Track modification to avoid useless operations. if headers: #Find line for the field where the header is to be changed (in the "value" argument of the input tag): for i in range(0, nb_lists): if not modded: if " ') #Find the line for the field where the elements are to be added: for i in range(0, nb_lists): if not modded: if "' + columns[i] + '' ) if not modded: m = re.match('(.*)jvenn_files\/(.*)', line) if m: modded = True #Modify path to get resources from the tool's install directory: output.write( m.group(1) + res_dir + "/jvenn_files/" + m.group(2) ) if not modded: output.write(line) #Print original line. line = template.readline() #Next line. template.close() #Preparation for compiled table: for c in range(0, len(columns)): columns[c] = columns[c].split(" ") #restore as list if headers: columns[c].insert(0, "" + colnames[c] + "") #restore headers #HTML conversion truncates to the shortest length, extend all lists to complete the matrix: maxlength = 0 for c in range(0, len(columns)): if len(columns[c]) > maxlength: #Find max length: maxlength = len(columns[c]) for c in range(0, len(columns)): #Extend lists by difference with placeholders: columns[c].extend([" "]*(maxlength - len(columns[c]))) columns = zip(*columns) #Transpose matrix for c in range(0, len(columns)): # Zip prdduces tuples, restore as lists. columns[c] = list(columns[c]) #Add compiled table to the html output: output.write("

Compiled table:


" + HTML.table(columns) + "
") output.close()