view convert_characters.py @ 2:8a53d7f02ce4 draft

planemo upload commit 00a7926c285bc4a339bd7deebf40b28f39c7d947-dirty
author devteam
date Tue, 21 Jul 2015 14:17:34 -0400
parents 64d46676a13e
children 042ed2d0a017
line wrap: on
line source

#!/usr/bin/env python
#By, Guruprasad Ananda.

from galaxy import eggs
import sys, re

def stop_err(msg):
    sys.stderr.write(msg)
    sys.exit()
    
def main():
    if len(sys.argv) != 4:
        stop_err("usage: convert_characters infile from_char outfile")

    try:
        fin = open(sys.argv[1],'r')
    except:
        stop_err("Input file cannot be opened for reading.")
    
    from_char = sys.argv[2]
    
    try:
        fout = open(sys.argv[3],'w')
    except:
        stop_err("Output file cannot be opened for writing.")
    
    char_dict = {'T':'\t','s':'\s','Dt':'\.','C':',','D':'-','U':'_','P':'\|','Co':':'}
    from_ch = char_dict[from_char] + '+'    #making an RE to match 1 or more occurences.
    skipped = 0
    
    for line in fin:
        line = line.strip()
        try:
            fout.write("%s\n" %(re.sub(from_ch,'\t',line)))     
        except:
            skipped += 1
            
    if skipped:
        print "Skipped %d lines as invalid." %skipped
    
if __name__ == "__main__": 
    main()