forked from chunjie-sam-liu/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortChromosomeAndPosition.py
executable file
·54 lines (44 loc) · 1.47 KB
/
sortChromosomeAndPosition.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
#!/usr/bin/python
#-*- coding:utf-8 -*-
################################################
#File Name: sortChromosomeAndPosition.py
#Author: C.J. Liu
#Mail: [email protected]
#Created Time: Mon 16 May 2016 03:50:08 PM CST
################################################
import os ,sys
def usage():
if len(sys.argv) != 2 and len(sys.argv) != 4 :
print ("Description:")
print ("\tSort file by chromosome and position as chr1, chr2...")
print ("Usage:")
print ("\tpython %prog inputFile chromField positionField (default chrom and position is first two colunm)")
print ("Example:")
print ("\tpython %prog hello.world 1 2")
sys.exit(1)
def sortFile(f, chrom = 1, position = 2):
ofile = open(f + ".sortByChrom", 'a')
sortOrder = ["chr" + str(i) for i in range(1,23)] + ["chrX","chrY", "chrM"]
for c in sortOrder:
posList = list()
lineList = list()
with open(f, 'r') as foo:
for line in foo:
line = line.rstrip()
arr = line.split("\t")
if arr[chrom - 1] != c:continue
posList.append(int(arr[position - 1]))
lineList.append(line)
posListSort = sorted(range(len(posList)), key = lambda k : posList[k])
for index in posListSort:
ofile.write(lineList[index] + os.linesep)
def run():
usage()
if len(sys.argv) == 2:
sortFile(sys.argv[1])
else:
sortFile(sys.argv[1], chrom = int(sys.argv[2]),position = int(sys.argv[3]))
def main():
run()
if __name__ == "__main__":
main()