0
|
1 #
|
|
2 # Developed by Praveen Kumar
|
|
3 # Galaxy-P Team (Griffin's Lab)
|
|
4 # University of Minnesota
|
|
5 #
|
|
6 #
|
2
|
7 #
|
0
|
8
|
|
9 def main():
|
|
10 from pyteomics import mzml
|
|
11 import os
|
|
12 import sys
|
|
13 import shutil
|
|
14 import subprocess
|
|
15 import re
|
|
16 import pandas as pd
|
|
17 from operator import itemgetter
|
|
18 from itertools import groupby
|
2
|
19 import random
|
|
20
|
|
21 if len(sys.argv) >= 7:
|
0
|
22 # Start of Reading Scans from PSM file
|
|
23 # Creating dictionary of PSM file: key = filename key = list of scan numbers
|
2
|
24
|
|
25 removeORretain = sys.argv[5].strip()
|
|
26 randomScans = int(sys.argv[6].strip())
|
|
27
|
0
|
28 ScanFile = sys.argv[2]
|
|
29 spectrumTitleList = list(pd.read_csv(ScanFile, "\t")['Spectrum Title'])
|
|
30 scanFileNumber = [[".".join(each.split(".")[:-3]), int(each.split(".")[-2:-1][0])] for each in spectrumTitleList]
|
|
31 scanDict = {}
|
|
32 for each in scanFileNumber:
|
|
33 if scanDict.has_key(each[0]):
|
|
34 scanDict[each[0]].append(int(each[1]))
|
|
35 else:
|
|
36 scanDict[each[0]] = [int(each[1])]
|
|
37 # End of Reading Scans from PSM file
|
|
38
|
|
39 inputPath = sys.argv[1]
|
|
40 ##outPath = "/".join(sys.argv[3].split("/")[:-1])
|
|
41 outPath = sys.argv[3]
|
|
42 ##outFile = sys.argv[3].split("/")[-1]
|
|
43 allScanList = []
|
|
44 # Read all scan numbers using indexedmzML/indexList/index/offset tags
|
|
45 for k in mzml.read(inputPath).iterfind('indexedmzML/indexList/index/offset'):
|
|
46 if re.search("scan=(\d+)", k['idRef']):
|
|
47 a = re.search("scan=(\d+)", k['idRef'])
|
|
48 allScanList.append(int(a.group(1)))
|
|
49 # End of Reading mzML file
|
|
50
|
|
51 fraction_name = sys.argv[4]
|
|
52 if scanDict.has_key(fraction_name):
|
2
|
53 scansInList = scanDict[fraction_name]
|
0
|
54 else:
|
2
|
55 scansInList = []
|
|
56 scansNotInList = list(set(allScanList) - set(scansInList))
|
0
|
57
|
2
|
58 if removeORretain == "remove":
|
|
59 scan2retain = scansNotInList
|
|
60 scan2retain.sort()
|
|
61 scansRemoved = scansInList
|
|
62 # scan2retain contains scans that is to be retained
|
|
63
|
|
64 elif removeORretain == "retain":
|
|
65 # Randomly select spectra
|
|
66 random_scans = list(map(lambda _: random.choice(scansNotInList), range(randomScans)))
|
|
67
|
|
68 scan2retain = random_scans + scansInList
|
|
69 scan2retain.sort()
|
|
70 scansRemoved = list(set(allScanList) - set(scan2retain))
|
|
71 # scan2retain contains scans that is to be retained
|
|
72
|
0
|
73 # Print Stats
|
|
74 print >> sys.stdout,"Total number of Scan Numbers: %d" % len(list(set(allScanList)))
|
|
75 print >> sys.stdout,"Number of Scans retained: %d" % len(scan2retain)
|
|
76 print >> sys.stdout,"Number of Scans removed: %d" % len(scansRemoved)
|
|
77
|
|
78
|
|
79 # Identifying groups of continuous numbers in the scan2retain and creating scanString
|
|
80 scanString = ""
|
|
81 for a, b in groupby(enumerate(scan2retain), lambda(i,x):i-x):
|
|
82 x = map(itemgetter(1), b)
|
|
83 scanString = scanString + "["+str(x[0])+","+str(x[-1])+"] "
|
|
84 # end identifying
|
|
85
|
|
86 # start create filter file
|
|
87 filter_file = open("filter.txt", "w")
|
|
88 filter_file.write("filter=scanNumber %s\n" % scanString)
|
|
89 filter_file.close()
|
|
90 # end create filter file
|
|
91
|
|
92 # Prepare command for msconvert
|
|
93 inputFile = fraction_name+".mzML"
|
|
94 os.symlink(inputPath,inputFile)
|
|
95 outFile = "filtered_"+fraction_name+".mzML"
|
|
96 # msconvert_command = "msconvert " + inputFile + " --filter " + "\"scanNumber " + scanString + " \" " + " --outfile " + outFile + " --mzML --zlib"
|
|
97 msconvert_command = "msconvert " + inputFile + " -c filter.txt " + " --outfile " + outFile + " --mzML --zlib"
|
|
98
|
|
99
|
|
100 # Run msconvert
|
|
101 try:
|
|
102 subprocess.check_output(msconvert_command, stderr=subprocess.STDOUT, shell=True)
|
|
103 except subprocess.CalledProcessError as e:
|
|
104 sys.stderr.write( "msconvert resulted in error: %s: %s" % ( e.returncode, e.output ))
|
|
105 sys.exit(e.returncode)
|
|
106 # Copy output to
|
|
107 shutil.copyfile(outFile, outPath)
|
|
108
|
|
109 else:
|
|
110 print "Please contact the admin. Number of inputs are not sufficient to run the program.\n"
|
|
111
|
|
112 if __name__ == "__main__":
|
|
113 main()
|
|
114
|
|
115
|
|
116
|
|
117
|