forked from samuellab/mindcontrol-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowAnnotation.py
118 lines (99 loc) · 3.85 KB
/
showAnnotation.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
#
# Copyright 2010 Andrew Leifer et al <[email protected]>
# This file is part of MindControl.
#
# MindControl is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MindControl is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MindControl. If not, see <http://www.gnu.org/licenses/>.
#
# For the most up to date version of this software, see:
# http://github.com/samuellab/mindcontrol
#
# NOTE: If you use any portion of this code in your research, kindly cite:
# Leifer, A.M., Fang-Yen, C., Gershow, M., Alkema, M., and Samuel A. D.T.
# "Optogenetic manipulation of neural activity with high spatial resolution in
# freely moving Caenorhabditis elegans," Nature Methods, Submitted (2010).
#
#System to show annotations that were generated by annotate.py
#in combination with the Mind Control software
#Andrew Leifer
import string, sys, os
usage = """Usage: showAnnotation.py D:\\Path\\to\\index.yml expString [framenumber [numberAdditionaFrames] ]
Display the annotations associated with an experiment whose filename contains expString. Optionally display only annotations to the framenumber specified, and further optionally annotations contained in the subsequent additional number of frames.
"""
#Handle Command Line Arguments
if ( len(sys.argv)<3 ) or ( len(sys.argv)>5 ):
print("Error wrong number of arguments!")
print(usage)
exit()
indexfile = sys.argv[1]
searchStr = sys.argv[2]
framenum = 0
if len(sys.argv)>=4:
try:
framenum=int(sys.argv[3])
except:
print("ERROR: Expected frame number is not a valid integer!\n")
print(usage)
exit()
print('You want to find annotations after frame '+str(framenum))
try:
r=open(indexfile,'r')
except:
print('\t ERROR: Annotation file not found!\n')
exit()
contents = r.read()
#Location of the experiment of interest
loc=contents.find(searchStr)
experiment= contents[contents.rfind('---\nExperiment:\n',0,loc):contents.find('---',loc)]
#If the frame number was not specified
if framenum==0:
#Display all of the annotations of the experiment
print(experiment)
exit()
#Let's parse the annotation with the largest frame number in the experiment
key = '\t- Frame:'
largestFrameLoc=experiment.rfind(key)
LargestFrame = int( experiment[ largestFrameLoc+len(key):experiment.find('\n',largestFrameLoc)] )
def LocNextAnnotation(text,frame,max):
"""Find the location of the next frame with an annotation whose frame number lies between frame and max in the string text"""
if frame > max:
print('Error: the frame you are interested in is beyond the frame of the largest annotation in the experiment.')
return None
for k in range(frame, max):
value = experiment.find(key+' '+str(k)+'\n')
if value >0:
return value
return None
first=LocNextAnnotation(experiment,framenum,LargestFrame)
last=len(experiment)
if len(sys.argv)==5:
try:
NumFramesToDisplay=int(sys.argv[4])
last=LocNextAnnotation(experiment,framenum+NumFramesToDisplay,LargestFrame)
except:
print('ERROR: Expecting an integer value for the optional argument Number for which to display annotations')
last=len(experiment)
if last==None:
print(experiment[first:])
exit()
if first==None:
print(experiment)
exit()
#print('len(experiment=' +str(len(experiment)))
#print('last = ' +str(last))
if last>first: #if there is more than one annotation to view
print(experiment[first:last])
else: #if there is only one annotation to view
#only display that one
print(experiment[first:experiment.find('"\n',first)])