diff mergeCols.py @ 0:28ca7552e884

Uploaded merge_cols tarball.
author devteam
date Tue, 04 Dec 2012 11:00:46 -0500
parents
children dd40b1e9eebe
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mergeCols.py	Tue Dec 04 11:00:46 2012 -0500
@@ -0,0 +1,37 @@
+import sys, re
+
+def stop_err( msg ):
+    sys.stderr.write( msg )
+    sys.exit()
+
+def __main__():
+    try:
+        infile =  open ( sys.argv[1], 'r')
+        outfile = open ( sys.argv[2], 'w')
+    except:
+        stop_err( 'Cannot open or create a file\n' )
+        
+    if len( sys.argv ) < 4:
+        stop_err( 'No columns to merge' )
+    else:
+        cols = sys.argv[3:]        
+
+    skipped_lines = 0
+
+    for line in infile:
+        line = line.rstrip( '\r\n' )
+        if line and not line.startswith( '#' ):
+            fields = line.split( '\t' )
+            line += '\t'
+            for col in cols:
+                try:
+                    line += fields[ int( col ) -1 ]
+                except:
+                    skipped_lines += 1
+                    
+            print >>outfile, line
+            
+    if skipped_lines > 0:
+        print 'Skipped %d invalid lines' % skipped_lines
+            
+if __name__ == "__main__" : __main__()
\ No newline at end of file