-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathYAPMascot.py
206 lines (164 loc) · 6.93 KB
/
YAPMascot.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
########################################################################################
## This file is a part of YAP package of scripts. https://github.com/shpakoo/YAP
## Distributed under the MIT license: http://www.opensource.org/licenses/mit-license.php
## Copyright (c) 2011-2013 Sebastian Szpakowski
########################################################################################
#################################################
## A pipeline for finding DAT files in the mascot directory
## filtering which of these files match a pattern of IDs
## finally using a web crawler to cache and generate text report.
#################################################
import sys
from optparse import OptionParser, OptionGroup
from StepsLibrary import *
from StepsLibrary_EXP import *
from collections import defaultdict
from Queue import Queue
_author="Sebastian Szpakowski"
_date="2012/11/29"
_version="Version 1"
#################################################
## Classes
##
#################################################
### Iterator over input file.
###
class GeneralPurposeParser:
def __init__(self, file, skip=0, sep="\t"):
self.filename = file
self.fp = open(self.filename, "rU")
self.sep = sep
self.linecounter = 0
self.currline=""
def __iter__(self):
return (self)
def next(self):
otpt = dict()
for currline in self.fp:
currline = currline.strip().split(self.sep)
self.currline = currline
self.linecounter = self.linecounter + 1
return(currline)
self.end()
raise StopIteration
def end(self):
self.fp.close()
def getHeader(self,n=10):
header = n
otpt = list()
for currline in self.fp:
header -=1
currline = currline.strip().split(self.sep)
otpt.append(currline)
if header==0:
return (otpt)
self.end()
return otpt
def __str__(self):
return "%s [%s]\n\t%s" % (self.filename, self.linecounter, self.currline)
class InfoParser:
def __init__(self, filename, directory):
self.filename = filename
self.info = GeneralPurposeParser(filename, sep="\t")
self.patterns = [ "".join(k).strip() for k in self.info ]
self.patterns = [k for k in self.patterns if len(k)>5]
self.dir = directory
# tuples filename/serverpath/sampleid
self.q=Queue()
if len(self.patterns)>0:
self.findFiles()
def findFiles(self):
print "Searching for files..."
files = glob.glob("%s/2011*/*.dat" % (self.dir))
files.extend(glob.glob("%s/2012*/*.dat" % (self.dir)))
files.extend(glob.glob("%s/2013*/*.dat" % (self.dir)))
for file in files:
header = 100
serverfilepath = file[len(self.dir):]
filename = file.split("/")[-1]
try:
p = GeneralPurposeParser(file, sep="=")
for line in p.getHeader(n=10):
if line[0].strip() == "COM":
info = line[1]
info = info[info.find(" from ")+6: info.find(" by ")]
info = info.replace(" " , "-")
if filename in self.patterns:
print "[file]", serverfilepath, info
self.q.put((serverfilepath, info))
else:
for k in self.patterns:
#if info.find(k)>-1 and info.find("2TO20")>-1:
if info.find(k)>-1:
print "[patt]", serverfilepath, info
self.q.put((serverfilepath, info))
p.end()
except:
pass
def __iter__(self):
return (self)
def next(self):
if not self.q.empty():
return self.q.get()
else:
raise StopIteration
#################################################
## Functions
##
################################################
### Read in a file and return a list of lines
###
def loadLines(x):
try:
fp = open(x, "rU")
cont=fp.readlines()
fp.close()
#print "%s line(s) loaded." % (len(cont))
except:
cont=""
#print "%s cannot be opened, does it exist? " % ( x )
return cont
#################################################
## Arguments
##
parser = OptionParser()
group = OptionGroup(parser, "Required", description="Will not run without these !")
group.add_option("-P", "--PROJECT", dest="project", default="",
help="project code", metavar="#")
group.add_option("-E", "--EMAIL", dest="email", default="",
help="e-mail address", metavar="@")
group.add_option("-i", "--info", dest="fn_info", default="",
help="a file with patterns (IDs) that the .dat files in mascot directory will be searched for. One id/pattern per line. For example ASIRIW will match any file with a substring ASIRIW as in 2DLCT1D_ASIRIW_120928VP-L-2TO20. Patterns shorter than 5 characters are not allowed.", metavar="patterns.txt")
parser.add_option_group(group)
group = OptionGroup(parser, "Optional Configuration", description="parameters to alter if necessary")
group.add_option("-m", "--mascot", dest="dir_mascot", default="/local/pfgrc_mascot/data/",
help="directory that stores the mascot dat files\n[%default]", metavar="mascot.dat")
parser.add_option_group(group)
group = OptionGroup(parser, "Technical", description="could be useful sometimes")
group.add_option("-C", "--NODESIZE", dest="nodesize", default=30,
help="maximum number of grid node's CPUs to use\n[%default]", metavar="#")
parser.add_option_group(group)
(options, args) = parser.parse_args()
#################################################
## Begin
##
if options.email == "" or options.project == "":
parser.print_help()
sys.exit(1)
############################
IP = InfoParser(options.fn_info, options.dir_mascot)
init(options.project, options.email)
final = list()
for file, id in IP:
args = {"file": file, "id": id}
S1 = MascotReportLifter(args, [])
final.append(S1)
OutputStep("REPORTS", "txt", final)
OutputStep("PDFscreens", "pdf", final)
OutputStep("PNGscreens", "png", final)
OutputStep("LOGS", "log", final)
##########################################################################
#
#################################################
## Finish
#################################################