root/galaxy-central/tools/filters/convert_characters.py

リビジョン 2, 1.0 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

行番号 
1#!/usr/bin/env python
2#By, Guruprasad Ananda.
3
4from galaxy import eggs
5import sys, re
6
7def stop_err(msg):
8    sys.stderr.write(msg)
9    sys.exit()
10   
11def main():
12    if len(sys.argv) != 4:
13        stop_err("usage: convert_characters infile from_char outfile")
14
15    try:
16        fin = open(sys.argv[1],'r')
17    except:
18        stop_err("Input file cannot be opened for reading.")
19   
20    from_char = sys.argv[2]
21   
22    try:
23        fout = open(sys.argv[3],'w')
24    except:
25        stop_err("Output file cannot be opened for writing.")
26   
27    char_dict = {'T':'\t','s':'\s','Dt':'\.','C':',','D':'-','U':'_','P':'\|','Co':':'}
28    from_ch = char_dict[from_char] + '+'    #making an RE to match 1 or more occurences.
29    skipped = 0
30   
31    for line in fin:
32        line = line.strip()
33        try:
34            fout.write("%s\n" %(re.sub(from_ch,'\t',line)))     
35        except:
36            skipped += 1
37           
38    if skipped:
39        print "Skipped %d lines as invalid." %skipped
40   
41if __name__ == "__main__":
42    main()
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。