-
Notifications
You must be signed in to change notification settings - Fork 0
/
transposeM.py
executable file
·75 lines (59 loc) · 2.72 KB
/
transposeM.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""Transpose a matrix. Convert columns going from left to right to rows going from top to bottom in such a way that column headers (i.e. the first row of the original matrix) shall be the first column in the transposed matrix. The input file must be in tab- or comma-separated values (csv or tsv) format."""
__author__ = "Ray Stefancsik"
__version__ = "01"
######################################################################
# Import command-line parsing module from the Python standard library.
######################################################################
import argparse
# assign initial value for input filename
fname = 'filename?'
parser = argparse.ArgumentParser( description='Transpose matrix. Convert columns going from left to right to rows going from top to bottom and print the results to standard output.', formatter_class=argparse.RawTextHelpFormatter )
parser.add_argument( 'input_file', help='Path to your input file.' )
# Optional, but mutually exclusive user options:
group = parser.add_mutually_exclusive_group()
group.add_argument('-t', '--tab', help='Use tab as column separator (default).', action='store_true')
group.add_argument('-c', '--csv', help='Use comma as column separator.', action='store_true')
args = parser.parse_args()
#################################################################
# Read in a data file.
#################################################################
# input filename
fname = args.input_file
### Input file format:
if args.csv:
delimiter = ',' #csv file format
else:
delimiter = '\t' #tsv file format
# check if you can open mandatory input file
try:
fhand1 = open( fname )
except:
print('fileERR:', fileERR)
raise SystemExit
###############################################################################
# Transpose input matrix counterclockwise. Return the results as a new matrix.
###############################################################################
def transposeM(your_matrix):
transposed_matrix = [ [your_matrix[j][i] for j in range(len(your_matrix))] for i in range(len(your_matrix[0])) ]
return(transposed_matrix)
##############################################################################
######################################
##### Read in process data ###########
######################################
# initialise matrix variable
#m = [ [] ]
m = [ ]
for line in fhand1:
line = line.rstrip()
m.append(line.split(delimiter))
# # strip whitespace
# refA = columns[0].strip() # reference allele sequence
# altA = columns[1].strip() # variant allele sequence
#m = [['a',1,2],['b',3,4],['c',5,6]]
for row in transposeM(m):
#for row in m:
print(row)
# close file
fhand1.close()