aboutsummaryrefslogtreecommitdiffstats
path: root/SNPGUI.sc
blob: 3027d81772193c2556318875cdd76d916843c700 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
SNPGUI {

  var <speakerSetup;//Array holding the chromosome numbers for each speaker
  var <mainView;// main view object
  var <speakerView;// main view object
  var <drawView;// main view object
  var <speakerDict;//Dictionary of Views (per speaker)
  var <textDict;//Dictionary of StaticText obbjects
  var <updateSNPTextTask;//task for updating the SNP texts of chromosomes
  var <snpTextFadeTasks; //Dictionary of tasks for SNP texts
  var <>textFadeTime = 0.5; // time (in seconds) it takes for a SNP text to fade out
  var <>textFadeSteps = 20; // amount of steps within textFadeTime in which to blend the color of a SNP text
  var <>speakerViewMargin = 4; // the margin between speakerView border and a text within it
  var <>speakerViewHeadMargin = 24; // the margin reserved for the speaker name
  var <intermediateTextDict; //Dictionary of texts for SNPs on chromosomes (intermediate)
  var <arcRadius = 200;//size of the arc, surrounded by speakers
  var <radiusOffset = 75;//size of the arc, surrounded by speakers
  var <mainWidth = 800;
  var <mainHeight = 800;
  var <speakerHeight = 70;
  var <speakerWidth = 70;
  var <lineHeight = 16;
  var <defaultFont = "Monaco";

  *new{
    arg setup;
    ^super.new.snpGUIInit(setup);
  }

  snpGUIInit{
    arg setup;
    speakerSetup = setup;
    this.createDicts;
    this.createViews;
    this.createTasks;
  }

  // initialize Dictionaries for all things
  createDicts{
    speakerDict = Dictionary.new(speakerSetup.size+1);
    intermediateTextDict = Dictionary.new(SNPInfo.chromosomesLength.size.asInt); // Dictionary of intermediate Strings to be used for the StaticText objects in textDict
    textDict = Dictionary.new(SNPInfo.chromosomesLength.size.asInt);//Dictionary holding all StaticText objects that can be modified
  }

  // creates all Views
  createViews{
    this.createMainView;
//    this.createDrawView;
    this.createSpeakerViews;
  }

  // create the main View (the main window)
  createMainView{
    mainView = View.new();
    mainView.minSize = mainWidth@mainHeight;
    mainView.userCanClose = true;
    mainView.enabled = true;
    mainView.visible = true;
    mainView.name = "The Sound Of People";
    mainView.background = Color.fromHexString("#DBDBDB");
    mainView.onResize = {
      if((drawView.notNil), {
        drawView.moveTo((mainView.absoluteBounds.width/2)-(drawView.bounds.width/2), (mainView.absoluteBounds.height/2)-(drawView.bounds.height/2));
      });
      speakerView.moveTo((mainView.absoluteBounds.width/2)-(speakerView.bounds.width/2), (mainView.absoluteBounds.height/2)-(speakerView.bounds.height/2));
    };
  }

  //creates a UserView, draws a circle
  createDrawView{
    drawView = UserView.new(mainView, Rect(0, 0, mainView.bounds.width, mainView.bounds.height));
    drawView.minSize = mainWidth@mainHeight;
    drawView.background = Color.fromHexString("#DBDBDB");
    drawView.background = Color.fromHexString("#e7ebe7");
    // center to mid of mainView on resize
    drawView.onResize = { drawView.moveTo(mainView.bounds.width/2-drawView.bounds.width/2, mainView.bounds.height/2-drawView.bounds.height/2)};
    // draw a circle
    drawView.drawFunc = {
      Pen.translate(drawView.bounds.width/2, drawView.bounds.height/2);
      Pen.strokeColor = Color.black;
      Pen.color = Color.black;
      Pen.addArc(0@0, arcRadius, 0, 2pi);
      Pen.stroke;
    };
  }

  // creates all speakerViews necessary for the setup defined in speakerSetup
  createSpeakerViews{
    speakerView = View.new(
      mainView,
      Rect(
        0,
        0,
        mainWidth, 
        mainHeight
      )
    );
    speakerView.minSize = mainWidth@mainHeight;
    // create a speakerView for each speaker in the setup
    speakerSetup.do({|speaker,i|
      // Adds a View with speaker information to the speakerDict
      speakerDict.add((i+1).asSymbol -> this.createSpeakerView(i, speakerSetup.size, speakerSetup[0].size));
      // iterate all chromosome information from original setup
      speaker.do({|chromosome, j|
        //add chromosome number -> replaceable SNP StaticText object to temporary Dictionary
        textDict.add(chromosome.asSymbol -> this.createChromosomeTexts(chromosome.asSymbol, (i+1).asSymbol, j));
        intermediateTextDict.add(chromosome.asSymbol -> "");
      });
    });
    // Adds a View for MT chromosome with speaker information to the speakerDict
    speakerDict.add(\all -> this.createSpeakerView(-1, speakerSetup.size, 1));
    // add chromosome text for MT to the speaker called all
    textDict.add(SNPInfo.chromosomesLength.size.asSymbol -> this.createChromosomeTexts(SNPInfo.chromosomesLength.size.asSymbol, \all, 0));
    intermediateTextDict.add(SNPInfo.chromosomesLength.size.asSymbol -> "");
  }

  //creates a View for one speaker, adds its name as StaticText object and returns itself
  createSpeakerView{
    arg number, total, chromosomesOnSpeaker;
    var mid, radius, radianOffset, radian, x, y, speakerContainer, speakerContainerInner, speakerText;
    mid = Point.new((mainWidth/2)-(speakerWidth/2),(mainHeight/2)-(speakerHeight/2));//mid of the circle
    radius = arcRadius+radiusOffset;//make a slightly bigger radius for the text objects
    radianOffset = 360/(total*2);// radian offset used to make speaker 1 be positioned front left
    radian = (360*(number/total))-90;//segment of the circle the speaker is placed in
    //Calculating x and y coordinates for the View to be created
    x = mid.x+(radius*cos(degrad(radian-radianOffset)));
    y = mid.y+(radius*sin(degrad(radian-radianOffset)));
    //setting up the border for a speaker
    speakerContainer = View.new(
      speakerView,
      Rect(
        x,
        y,
        speakerWidth,
        ((chromosomesOnSpeaker)*lineHeight)+(speakerViewMargin*2)+speakerViewHeadMargin
      )
    );
    speakerContainer.visible = true;
    speakerContainer.background = Color.fromHexString("#7f7f7f");
    //setting up the View container for a speaker
    speakerContainerInner = View.new(
      speakerContainer,
      Rect(
        (speakerViewMargin/2),
        (speakerViewMargin/2),
        speakerWidth-speakerViewMargin,
        ((chromosomesOnSpeaker)*lineHeight)+speakerViewMargin+speakerViewHeadMargin
      )
    );
    speakerContainerInner.visible = true;
    speakerContainerInner.background = Color.fromHexString("#EEEEEE");

    // add the speaker text (number of the speaker)
    speakerText = StaticText(
      speakerContainerInner,
      Rect(
        0,
        speakerViewMargin,
        speakerContainer.bounds.width,
        lineHeight
      )
    );
    speakerText.align = \center;
    speakerText.string = (number+1).asString;
    speakerText.stringColor = Color.fromHexString("#4A52C4");
    speakerText.font = Font(this.defaultFont,16,true);
    //add exception for no/ all speaker
    if(number<0,{
      // rename the speaker
      speakerText.string = "all";
      //recalculate radius and offset
      radius = radius+radiusOffset;
      radian = -90+((360/total)/2);
      //recalculate x and y coordinates to move the View
      x = mid.x+(radius*cos(degrad(radian-radianOffset)));
      y = mid.y+(radius*sin(degrad(radian-radianOffset)));
      // move speakerContainer to new coordinates (top center)
      speakerContainer.moveTo(x, y);
    });
    ^speakerContainer;
  }

  //create a StaticText for a chromosome in its speaker View, returns its replaceable component (for SNPs)
  createChromosomeTexts{
    arg chromosome, number, position;
    var snpText, chromosomeText;
    //create the name of the chromosome as StaticText
    chromosomeText = StaticText.new(
      speakerDict.at(number),
      Rect(
        speakerViewMargin,
        (lineHeight*position)+speakerViewHeadMargin,
        (speakerDict.at(number).bounds.width/2),
        lineHeight
      )
    );
    chromosomeText.align = \left;
    chromosomeText.string = if((chromosome.asInt < 10), {("0"++chromosome.asString++": ")}, {chromosome.asString++": "});
    chromosomeText.font = Font(this.defaultFont,13,true,true);

    //create the snpText (its string will be set with a Task later)
    snpText = StaticText.new(
      speakerDict.at(number),
      Rect((
        speakerDict.at(number).bounds.width/2)-speakerViewMargin,
        (lineHeight*position)+speakerViewHeadMargin,
        (speakerDict.at(number).bounds.width/2),
        lineHeight
      )
    );
    snpText.align = \right;
    snpText.string = "";
    snpText.font = Font(this.defaultFont,13,true);
    ^snpText;
  }

  //create all Tasks
  createTasks{
    this.createSNPTextFadeTasks(SNPInfo.chromosomesLength.size.asInt);
    this.createUpdateSNPTextTask;
  }

  // create Tasks to fade out the text of all SNPs and blend its color gradually
  createSNPTextFadeTasks{|size|
    snpTextFadeTasks = Dictionary.new(size);
    size.do({|item,i|
      var textTask;
      snpTextFadeTasks.put((i+1).asSymbol, textTask = Task({
          var snp = (i+1).asSymbol;
          var elapsed = 0.0;
          // reset the intermediateTextDict text
          this.setIntermediateText(snp, "");
          // blend the original color of the chromosome's SNP by the amount of steps defined in textFadeSteps with black. Each step's length is defined by textFadeTime
          this.textFadeSteps.do({|item,i|
            textDict.at(snp.asSymbol).alpha_(textDict.at(snp.asSymbol).alpha-(textFadeTime/this.textFadeSteps));
            textDict.at(snp.asSymbol).stringColor_(textDict.at(snp.asSymbol).stringColor.blend(Color.black(), textFadeTime/this.textFadeSteps));
            (textFadeTime/this.textFadeSteps).wait;
          });
          textDict.at(snp.asSymbol).string_("");
        })
      );
    });
  }

  //Starts a task to update texts for SNP of chromosomes on AppClock
  createUpdateSNPTextTask{
    updateSNPTextTask = Task.new({
      {
        //get all values from the dictionary and update the corresponding StaticText objects
        intermediateTextDict.keysValuesDo({|key, value|
          if((value.asString != ""),{
            //set the text and color for the SNP here,as doing this within the fade Task would delay it too much 
            textDict.at(key.asSymbol).string_(value.asString);
            // set different color for each base/ combination of bases
            textDict.at(key.asSymbol).stringColor_(Color.fromHexString(SNPInfo.getColorFromBase(value.asString)));
            //start or reset the snpTextTask belonging to this chromosome (to fade out its text)
            if((snpTextFadeTasks.at(key.asSymbol).isPlaying), {
              snpTextFadeTasks.at(key.asSymbol).reset;
            },{
              snpTextFadeTasks.at(key.asSymbol).start(AppClock);
            });
          });
        });
        0.02.wait;
      }.loop;
    });
    updateSNPTextTask.play(AppClock);
  }

  //set the text of a specific chromosome (aka.: which SNP is currently playing?)
  setIntermediateText{
    arg chromosome, text;
    intermediateTextDict.put(chromosome.asSymbol, text.asString);
  }

  // get the current SNP text of a chromosome
  getIntermediateText{
    arg chromosome;
    var text = intermediateTextDict.at(chromosome.asSymbol);
    ^text;
  }
}