forked from albertwcheng/albert-bioinformatics-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddIndexToCol.py
executable file
·124 lines (95 loc) · 4.48 KB
/
addIndexToCol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
'''
addIndexToCol.py
Copyright 2010 Wu Albert Cheng <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
from optparse import OptionParser
from sys import *
from albertcommon import *
def printUsageAndExit(parser):
parser.print_help(stderr)
explainColumns(stderr)
exit()
if __name__=='__main__':
usage="usage: %prog [options] filename col"+\
"""
Add in index for values in specified column
"""
parser=OptionParser(usage,add_help_option=False)
parser.add_option("-h","--help",dest="help",default=False,action="store_true",help="show this help message and exit")
parser.add_option("--fs",default="\t",dest="fs",help="set field separator. Default is tab")
parser.add_option("--start-base",default="1",type="string",dest="startBase",help="set the starting index. Default is 1. Can use excel style. If use excel style, the --excel-style flag is auto set to on")
parser.add_option("--header-row",default=1,type="int",dest="headerRow",help="set the header row. Default is 1")
parser.add_option("--start-row",default=2,type="int",dest="startRow",help="set the starting row. Default is 2")
parser.add_option("--trim-name",default=False,dest="trimName",action="store_true",help="trim name such that spaces in prefix and suffix are removed")
parser.add_option("--name-index-separator",default=".",dest="nameIndexSeparator",help="set the separator (joiner) between the name and the index")
parser.add_option("--excel-style",default=False,dest="excelStyle",action="store_true",help="instead of number use excel style, from A to Z,then AA,...")
parser.add_option("--lowercase",default=False,dest="excelStyleLowercase",action="store_true",help="use lower case for excel style. only valid when --excel-style is set")
parser.add_option("--prepend",default="",dest="prefix",help="prepend value with a prefix. Default no prepending")
parser.add_option("--append",default="",dest="suffix",help="append value with a suffix. Default no appending")
parser.add_option("--only-idx-to-dup",default=False,action="store_true",dest="onlyIdxDup",help="only append a index to duplicates")
(options,args)=parser.parse_args()
if options.help:
printUsageAndExit(parser)
try:
filename,col=args
except:
printUsageAndExit(parser)
fs=replaceSpecialChar(options.fs)
header,prestarts=getHeader(filename,options.headerRow,options.startRow,fs)
col=getCol0ListFromCol1ListStringAdv(header,col)[0]
try:
options.startBase=int(options.startBase)
except ValueError:
#using excel base
options.startBase=excelIndxToCol0(options.startBase)
#if startbase is excel style, then turn on excelstyle display
options.excelStyle=True
#print >> stderr,"s=",options.startBase
itemCounter=dict()
lino=0
fil=open(filename)
for lin in fil:
lino+=1
lin=lin.rstrip("\r\n")
if lino<options.startRow:
print >> stdout,lin
continue
fields=lin.split(fs)
# set up a library
thisName=fields[col]
if options.trimName:
thisName=thisName.strip()
try:
thisItemCount=itemCounter[thisName]
itemCounter[thisName]+=1
except:
thisItemCount=0
itemCounter[thisName]=1
indxToDisplay=thisItemCount+options.startBase
if options.excelStyle:
indxToDisplay=excelColIndex(indxToDisplay)
if options.excelStyleLowercase:
indxToDisplay=indxToDisplay.lower()
if options.onlyIdxDup and thisItemCount==0:
sindxToDisplay=""
else:
sindxToDisplay=options.nameIndexSeparator+str(indxToDisplay)
fields[col]=options.prefix+thisName+sindxToDisplay+options.suffix
print >> stdout,fs.join(fields)
fil.close()