annotate src/transpose.py @ 0:e0b5980139d9

maaslin
author george-weingart
date Tue, 13 May 2014 22:00:40 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
1 #!/usr/bin/env python
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
2 #######################################################################################
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
3 # This file is provided under the Creative Commons Attribution 3.0 license.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
4 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
5 # You are free to share, copy, distribute, transmit, or adapt this work
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
6 # PROVIDED THAT you attribute the work to the authors listed below.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
7 # For more information, please see the following web page:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
8 # http://creativecommons.org/licenses/by/3.0/
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
9 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
10 # This file is a component of the SflE Scientific workFLow Environment for reproducible
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
11 # research, authored by the Huttenhower lab at the Harvard School of Public Health
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
12 # (contact Curtis Huttenhower, chuttenh@hsph.harvard.edu).
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
13 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
14 # If you use this environment, the included scripts, or any related code in your work,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
15 # please let us know, sign up for the SflE user's group (sfle-users@googlegroups.com),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
16 # pass along any issues or feedback, and we'll let you know as soon as a formal citation
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
17 # is available.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
18 #######################################################################################
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
19
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
20 """
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
21 Examples
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
22 ~~~~~~~~
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
23
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
24 ``data.pcl``::
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
25
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
26 a b
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
27 c d
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
28 e f
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
29
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
30 ``Examples``::
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
31
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
32 $ transpose.py < data.pcl
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
33 a c e
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
34 b d f
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
35
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
36 $ echo "a b c" | transpose.py
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
37 a
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
38 b
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
39 c
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
40
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
41 .. testsetup::
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
42
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
43 from transpose import *
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
44 """
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
45
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
46 import argparse
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
47 import csv
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
48 import sys
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
49
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
50 def transpose( aastrIn, ostm ):
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
51 """
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
52 Outputs the matrix transpose of the input tab-delimited rows.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
53
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
54 :param aastrIn: Split lines from which data are read.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
55 :type aastrIn: collection of string collections
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
56 :param ostm: Output stream to which transposed rows are written.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
57 :type ostm: output stream
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
58
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
59 >>> aastrIn = [list(s) for s in ("ab", "cd", "ef")]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
60 >>> transpose( aastrIn, sys.stdout ) #doctest: +NORMALIZE_WHITESPACE
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
61 a c e
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
62 b d f
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
63
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
64 >>> transpose( [list("abc")], sys.stdout ) #doctest: +NORMALIZE_WHITESPACE
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
65 a
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
66 b
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
67 c
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
68 """
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
69
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
70 aastrLines = [a for a in aastrIn]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
71 csvw = csv.writer( ostm, csv.excel_tab )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
72 for iRow in range( len( aastrLines[0] ) ):
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
73 csvw.writerow( [aastrLines[iCol][iRow] for iCol in range( len( aastrLines ) )] )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
74
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
75 argp = argparse.ArgumentParser( prog = "transpose.py",
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
76 description = """Transposes a tab-delimited text matrix.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
77
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
78 The transposition process is robust to missing elements and rows of differing lengths.""" )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
79 __doc__ = "::\n\n\t" + argp.format_help( ).replace( "\n", "\n\t" ) + __doc__
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
80
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
81 def _main( ):
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
82 args = argp.parse_args( )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
83 transpose( csv.reader( sys.stdin, csv.excel_tab ), sys.stdout )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
84
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
85 if __name__ == "__main__":
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
86 _main( )