Oct-04-2017, 08:25 PM
You open a bunch of files (two for every time through the for loop), but you only ever close the last two that you opened (you close them outside the for loop). Things like this are why there's a with block, so whatever you open is closed for you automatically when you can't use it anymore. But if you're using python2.7 as your path indicates, you might not have access to it.
But you do have access to the csv module, so you don't need to do things like split the line on tabs.
You can probably delete most of those blocks, if you generate the id of the line a little better. Instead of locusnumber, and newlocusnumber, turning it into something much simpler, it looks like the format is Cabther_[AB]NNNN, where A/B is determined by whether or not locusnumber is less than the magic number 2274. So something like this:
But you do have access to the csv module, so you don't need to do things like split the line on tabs.
You can probably delete most of those blocks, if you generate the id of the line a little better. Instead of locusnumber, and newlocusnumber, turning it into something much simpler, it looks like the format is Cabther_[AB]NNNN, where A/B is determined by whether or not locusnumber is less than the magic number 2274. So something like this:
def get_row_label(locusnumber): tag = "A" if locusnumber < 2274 else "B" return "Cabther_{0}{1:04.0f}".format(tag, locusnumber) # for file in files locusnumber = 0 for line in infile: label = get_row_label(locusnumber) elements = line.split("\t") if elements[0] == label: outfile.write(line) else: #etc outfile.write("\n" * difference + line)And that's it, no more of the same block 7 times in a row.