annotate MSMS_Extractor.py @ 0:093015b1b904 draft

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