Here is a solution in Python. I have tested it on a small fake file I made up. I think this will be acceptably fast for even a large file, because most of the work will be done by C code inside of Python. And I think this is a pleasant and easy to understand program; I prefer Python to Perl.
import sys
s_usage = """
Usage: csplit <filename>
Splits input file by columns, writes column 2 to file based on chromosome from column 4."""
if len(sys.argv) != 2 or sys.argv[1] in ("-h", "--help", "/?"):
sys.stderr.write(s_usage + "
")
sys.exit(1)
# replace these with the actual patterns, of course
lst_pat = [
a , b , c , d , e , f , g , h , i , j ,
k , l , m , n , o , p , q , r , s , t ,
u , v , w , x , y
]
d = {}
for s_pat in lst_pat:
# build a dictionary mapping each pattern to an open output file
d[s_pat] = open("my_out_file_" + s_pat, "wt")
if False:
# if the patterns are unsuitable for filenames (contain * , ? , etc.) use this:
for i, s_pat in enumerate(lst_pat):
# build a dictionary mapping each pattern to an output file
d[s_pat] = open("my_out_file_" + str(i), "wt")
for line in open(sys.argv[1]):
# split a line into words, and unpack into variables.
# use _ for a variable name to indicate data we don t care about.
# s_data is the data we want, and s_pat is the pattern controlling the output
_, s_data, _, s_pat, _ = line.split()
# use s_pat to get to the file handle of the appropriate output file, and write data.
d[s_pat].write(s_data + "
")
# close all the output file handles.
for key in d:
d[key].close()
EDIT: Here s a little more information about this program, since it seems you will be using it.
All of the error handling is implicit. If an error happens, Python will "raise an exception" which will terminate processing. For example, if one of the files fails to open, this program will stop executing and Python will print a backtrace showing which line of code caused the exception. I could have wrapped the critical parts with a "try/except" block, to catch errors, but for a program this simple, I didn t see any point.
It s subtle, but there is a check to see if there are exactly five words on each line of the input file. When this code unpacks a line, it does so into five variables. (The variable name "_" is a legal variable name, but there is a convention in the Python community to use it for variables you don t actually care about.) Python will raise an exception if there are not exactly five words on the input line to unpack into the five variables. If your input file can sometimes have four words on a line, or six or more, you could modify the program to not raise an exception; change the main loop to this:
for line in open(sys.argv[1]):
lst = line.split()
d[lst[3]].write(lst[1] + "
")
This splits the line into words, and then just assigns the whole list of words into a single variable, lst. So that line of code doesn t care how many words are on the line. Then the next line indexes into the list to get the values out. Since Python indexes a list using 0 to start, the second word is lst[1]
and the fourth word is lst[3]
. As long as there are at least four words in the list, that line of code won t raise an exception either.
And of course, if the fourth word on the line is not in the dictionary of file handles, Python will raise an exception for that too. That would stop processing. Here is some example code for how to use a "try/except" block to handle this:
for line in open(sys.argv[1]):
lst = line.split()
try:
d[lst[3]].write(lst[1] + "
")
except KeyError:
sys.stderr.write("Warning: illegal line seen: " + line)
Good luck with your project.
EDIT: @larelogio pointed out that this code doesn t match the AWK code. The AWK code has an extra for loop that I do not understand. Here is Python code to do the same thing:
for line in open(sys.argv[1]):
lst = line.split()
n = int(lst[1])
for i in range(n, n+53):
d[lst[3]].write(i + "
")
And here is another way to do it. This might be a little faster, but I have not tested it so I am not certain.
for line in open(sys.argv[1]):
lst = line.split()
n = int(lst[1])
s = "
".join(str(i) for i in range(n, n+53))
d[lst[3]].write(s + "
")
This builds a single string with all the numbers to write, then writes them in one chunk. This may save time compared to calling .write()
53 times.