2
2
3
3
import calculations .DamageProfiler ;
4
4
import calculations .Frequencies ;
5
+ import com .google .gson .Gson ;
5
6
import com .itextpdf .awt .PdfGraphics2D ;
6
7
import com .itextpdf .text .*;
7
8
import com .itextpdf .text .Font ;
8
9
import com .itextpdf .text .pdf .PdfContentByte ;
9
10
import com .itextpdf .text .pdf .PdfTemplate ;
10
11
import com .itextpdf .text .pdf .PdfWriter ;
12
+ import org .apache .commons .math3 .stat .descriptive .DescriptiveStatistics ;
11
13
import org .apache .log4j .Logger ;
12
14
import org .jfree .chart .JFreeChart ;
13
15
import org .jfree .data .statistics .HistogramDataset ;
14
16
import org .jfree .data .xy .XYDataset ;
15
- import org .yaml .snakeyaml .Yaml ;
16
17
17
18
18
19
import java .awt .*;
@@ -43,6 +44,7 @@ public class OutputGenerator {
43
44
private int threshold ;
44
45
private int length ;
45
46
private String input ;
47
+ private HashMap <String ,Object > json_map = new HashMap <>();
46
48
47
49
48
50
@@ -65,6 +67,37 @@ public OutputGenerator(String outputFolder, DamageProfiler damageProfiler, Strin
65
67
}
66
68
}
67
69
70
+ /**
71
+ * writes all generated output statistics to YAML output file
72
+ * YAML has several key,value pairs (HashMaps) that all contain information created in DamageProfiler
73
+ * sample_name = the name of the sample
74
+ *
75
+ *
76
+ * @throws IOException
77
+ */
78
+ public void writeJSON (String version ) throws IOException {
79
+ //Add Sample Name to yaml
80
+ String sampleName = input .split ("/" )[input .split ("/" ).length -1 ];
81
+
82
+
83
+ //Add Metadata to JSON output
84
+ HashMap <String , Object > meta_map = new HashMap <>();
85
+ meta_map .put ("sample_name" , sampleName );
86
+ meta_map .put ("tool_name" , "DamageProfiler" );
87
+ meta_map .put ("version" , version );
88
+
89
+ json_map .put ("metadata" , meta_map );
90
+
91
+ Gson gson = new Gson ();
92
+
93
+ String json = gson .toJson (json_map );
94
+
95
+ FileWriter fw = new FileWriter (this .outpath + "/dmgprof.json" );
96
+ fw .write (json );
97
+ fw .flush ();
98
+ fw .close ();
99
+
100
+ }
68
101
69
102
/**
70
103
* create tab-separated txt file with all read length, sorted according strand direction
@@ -73,12 +106,7 @@ public OutputGenerator(String outputFolder, DamageProfiler damageProfiler, Strin
73
106
*/
74
107
public void writeLengthDistribution () throws IOException {
75
108
76
-
77
109
BufferedWriter lgdist = new BufferedWriter (new FileWriter (this .outpath + "/lgdistribution.txt" ));
78
- Yaml lgdistyaml_fw = new Yaml ();
79
- Yaml lgdistyaml_rv = new Yaml ();
80
- FileWriter writer_fw = new FileWriter (this .outpath + "/lgdistribution_fw.yaml" );
81
- FileWriter writer_rv = new FileWriter (this .outpath + "/lgdistribution_rv.yaml" );
82
110
HashMap <Integer , Integer > map_forward = damageProfiler .getLength_distribution_map_forward ();
83
111
HashMap <Integer , Integer > map_reverse = damageProfiler .getLength_distribution_map_reverse ();
84
112
@@ -94,8 +122,6 @@ public void writeLengthDistribution() throws IOException{
94
122
key_list .addAll (map_forward .keySet ());
95
123
Collections .sort (key_list );
96
124
97
- HashMap <String ,HashMap > dump_fw_hm = new HashMap <>();
98
- String sampleid = input .split ("/" )[input .split ("/" ).length -1 ];
99
125
HashMap <Integer ,Integer > yaml_dump_fw = new HashMap <>();
100
126
101
127
if (key_list .size ()>0 ){
@@ -107,15 +133,13 @@ public void writeLengthDistribution() throws IOException{
107
133
yaml_dump_fw .put (key , map_forward .get (key ));
108
134
}
109
135
}
110
- dump_fw_hm .put (sampleid ,yaml_dump_fw );
111
- lgdistyaml_fw .dump (dump_fw_hm ,writer_fw );
136
+ json_map .put ("lendist_fw" ,yaml_dump_fw );
112
137
113
138
key_list .clear ();
114
139
key_list .addAll (map_reverse .keySet ());
115
140
Collections .sort (key_list );
116
141
117
142
HashMap <Integer ,Integer > yaml_dump_rv = new HashMap <>();
118
- HashMap <String ,HashMap > dump_rv_hm = new HashMap <>();
119
143
120
144
121
145
if (key_list .size ()>0 ){
@@ -133,12 +157,7 @@ public void writeLengthDistribution() throws IOException{
133
157
134
158
}
135
159
136
- dump_rv_hm .put (sampleid ,yaml_dump_rv );
137
-
138
- lgdistyaml_rv .dump (dump_rv_hm ,writer_rv );
139
-
140
-
141
-
160
+ json_map .put ("lendist_rv" , yaml_dump_rv );
142
161
lgdist .close ();
143
162
}
144
163
@@ -453,22 +472,10 @@ public void writeDamageFiles(double[] gToA_reverse, double[] cToT) throws IOExce
453
472
BufferedWriter writer3Prime = new BufferedWriter (new FileWriter (this .outpath + "/3pGtoA_freq.txt" ));
454
473
BufferedWriter writer5Prime = new BufferedWriter (new FileWriter (this .outpath + "/5pCtoT_freq.txt" ));
455
474
456
- FileWriter writ3P = new FileWriter (this .outpath + "/3pGtoA_freq.yaml" );
457
- FileWriter writ5p = new FileWriter (this .outpath + "/5pCtoT_freq.yaml" );
475
+ //Add stuff to json output file
476
+ json_map .put ("dmg_5p" ,getSubArray (cToT , this .threshold ));
477
+ json_map .put ("dmg_3p" ,getSubArray (gToA_reverse ,this .threshold ));
458
478
459
- String sampleid = input .split ("/" )[input .split ("/" ).length -1 ];
460
-
461
- Yaml yml3p = new Yaml ();
462
- Yaml yml5p = new Yaml ();
463
-
464
- Map <String , double []> yml3p_map = new HashMap <String ,double []>();
465
- yml3p_map .put (sampleid ,getSubArray (cToT , this .threshold ));
466
- yml3p .dump (yml3p_map ,writ3P );
467
-
468
-
469
- Map <String , double []> yml5p_map = new HashMap <String ,double []>();
470
- yml5p_map .put (sampleid ,getSubArray (gToA_reverse ,this .threshold ));
471
- yml5p .dump (yml5p_map ,writ5p );
472
479
473
480
writer3Prime .write ("# table produced by calculations.DamageProfiler\n " );
474
481
writer3Prime .write ("# using mapped file " + input + "\n " );
@@ -502,6 +509,41 @@ private void writeDamagePattern(String title, double[] values, BufferedWriter wr
502
509
503
510
}
504
511
512
+ public void computeSummaryMetrics (){
513
+ HashMap <Integer ,Integer > forwardMap = damageProfiler .getLength_distribution_map_forward (); // key = length, value = occurences
514
+ HashMap <Integer , Integer >reverseMap = damageProfiler .getLength_distribution_map_reverse ();
515
+
516
+ //Create ArrayList<Integer> of read lengths
517
+ DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics ();
518
+
519
+ for (int key : forwardMap .keySet ()){
520
+ int occurences = forwardMap .get (key );
521
+ for (int i = 0 ; i <= occurences ; i ++) {
522
+ descriptiveStatistics .addValue (key );
523
+ }
524
+ }
525
+
526
+ for (int key : reverseMap .keySet ()){
527
+ int occurences = reverseMap .get (key );
528
+ for (int i = 0 ; i <= occurences ; i ++) {
529
+ descriptiveStatistics .addValue (key );
530
+ }
531
+ }
532
+
533
+ double mean = descriptiveStatistics .getMean ();
534
+ double std = descriptiveStatistics .getStandardDeviation ();
535
+ double median = descriptiveStatistics .getPercentile (50 );
536
+
537
+ HashMap <String , Double > summ_stats = new HashMap <>();
538
+
539
+ summ_stats .put ("mean_readlength" , mean );
540
+ summ_stats .put ("std" , std );
541
+ summ_stats .put ("median" , std );
542
+
543
+ json_map .put ("summary_stats" , summ_stats );
544
+
545
+ }
546
+
505
547
506
548
507
549
public void writeMisincorporations (Frequencies frequencies , int threshold ) throws IOException {
0 commit comments