22
33import calculations .DamageProfiler ;
44import calculations .Frequencies ;
5+ import com .google .gson .Gson ;
56import com .itextpdf .awt .PdfGraphics2D ;
67import com .itextpdf .text .*;
78import com .itextpdf .text .Font ;
89import com .itextpdf .text .pdf .PdfContentByte ;
910import com .itextpdf .text .pdf .PdfTemplate ;
1011import com .itextpdf .text .pdf .PdfWriter ;
12+ import org .apache .commons .math3 .stat .descriptive .DescriptiveStatistics ;
1113import org .apache .log4j .Logger ;
1214import org .jfree .chart .JFreeChart ;
1315import org .jfree .data .statistics .HistogramDataset ;
1416import org .jfree .data .xy .XYDataset ;
15- import org .yaml .snakeyaml .Yaml ;
1617
1718
1819import java .awt .*;
@@ -43,6 +44,7 @@ public class OutputGenerator {
4344 private int threshold ;
4445 private int length ;
4546 private String input ;
47+ private HashMap <String ,Object > json_map = new HashMap <>();
4648
4749
4850
@@ -65,6 +67,37 @@ public OutputGenerator(String outputFolder, DamageProfiler damageProfiler, Strin
6567 }
6668 }
6769
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+ }
68101
69102 /**
70103 * create tab-separated txt file with all read length, sorted according strand direction
@@ -73,12 +106,7 @@ public OutputGenerator(String outputFolder, DamageProfiler damageProfiler, Strin
73106 */
74107 public void writeLengthDistribution () throws IOException {
75108
76-
77109 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" );
82110 HashMap <Integer , Integer > map_forward = damageProfiler .getLength_distribution_map_forward ();
83111 HashMap <Integer , Integer > map_reverse = damageProfiler .getLength_distribution_map_reverse ();
84112
@@ -94,8 +122,6 @@ public void writeLengthDistribution() throws IOException{
94122 key_list .addAll (map_forward .keySet ());
95123 Collections .sort (key_list );
96124
97- HashMap <String ,HashMap > dump_fw_hm = new HashMap <>();
98- String sampleid = input .split ("/" )[input .split ("/" ).length -1 ];
99125 HashMap <Integer ,Integer > yaml_dump_fw = new HashMap <>();
100126
101127 if (key_list .size ()>0 ){
@@ -107,15 +133,13 @@ public void writeLengthDistribution() throws IOException{
107133 yaml_dump_fw .put (key , map_forward .get (key ));
108134 }
109135 }
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 );
112137
113138 key_list .clear ();
114139 key_list .addAll (map_reverse .keySet ());
115140 Collections .sort (key_list );
116141
117142 HashMap <Integer ,Integer > yaml_dump_rv = new HashMap <>();
118- HashMap <String ,HashMap > dump_rv_hm = new HashMap <>();
119143
120144
121145 if (key_list .size ()>0 ){
@@ -133,12 +157,7 @@ public void writeLengthDistribution() throws IOException{
133157
134158 }
135159
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 );
142161 lgdist .close ();
143162 }
144163
@@ -453,22 +472,10 @@ public void writeDamageFiles(double[] gToA_reverse, double[] cToT) throws IOExce
453472 BufferedWriter writer3Prime = new BufferedWriter (new FileWriter (this .outpath + "/3pGtoA_freq.txt" ));
454473 BufferedWriter writer5Prime = new BufferedWriter (new FileWriter (this .outpath + "/5pCtoT_freq.txt" ));
455474
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 ));
458478
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 );
472479
473480 writer3Prime .write ("# table produced by calculations.DamageProfiler\n " );
474481 writer3Prime .write ("# using mapped file " + input + "\n " );
@@ -502,6 +509,41 @@ private void writeDamagePattern(String title, double[] values, BufferedWriter wr
502509
503510 }
504511
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+
505547
506548
507549 public void writeMisincorporations (Frequencies frequencies , int threshold ) throws IOException {
0 commit comments