-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
1435 lines (1220 loc) · 58.6 KB
/
Main.java
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Assignment2;
import Assignment1.Gene;
import Assignment1.GtfRow;
import Assignment1.Transcript;
import Utils.FileUtils;
import org.apache.commons.math3.distribution.NormalDistribution;
import java.io.*;
import java.util.*;
public class ReadSimulator3 {
static HashMap<String, ArrayList<Integer>> transcriptFragments = new HashMap<>();
static HashMap<String, Double> transcript_tot_len = new HashMap<>();
static HashMap<String, Double> transcript_readcons = new HashMap<String, Double>();
public static void main(String[] args) {
//parameter treatment
if (args.length == 0) {
System.out.println("execute the program with the following parameters:");
System.out.println("-gene_id <String> : gene id");
System.out.println("-length <int> : read length");
System.out.println("-frlength <int> : fragment length distribution");
System.out.println("- SD <int> : fragment length distribution");
System.out.println("-readcounts <file> : table of gene_id, transcript_id, count tuples");
System.out.println("-non_unique_frq <int> : frequency how many non unique reads should be simulate, default: 20 %");
System.out.println("-sequence_error_rate <int> : sequence error rate in percent");
System.out.println("-dot_mutation_rate <int[4]> : dot mutation rate in percent for all bases A,C,G,T , default: 1,1,1,1");
System.out.println("-insertion_mutation_rate <int[4]> : insertion mutation rate in percent for all bases A,C,G,T , default: 1,1,1,1");
System.out.println("-deletion_mutation_rate <int[4]> : deletion mutation rate in percent for all bases A,C,G,T , default: 1,1,1,1");
System.out.println("-junction_mutation_rate <int> : junction mutation rate in percent , default: 5");
System.out.println("-junction_size <int> : affected bases at junction boundaries, default: 3");
System.out.println("-overlap_on_more_than_1_T <boolean> : to adjust difficulty, default: false");
System.out.println("-minoverlapsize <int> : to adjust difficulty, default: 50");
System.out.println("-fasta <FASTA file> : genome fasta");
System.out.println("-fidx <FASTA file index> : index for genome fasta");
System.out.println("-gtf <GTF file> : genomic annotation");
System.out.println("-biological <boolean> : simulate biological");
System.out.println("-bio_file <file> : biological similiarity file");
System.out.println("-od <output file path>: path to the output directory");
System.exit(1);
}
String gene_id = null;
int readLength = 0;
int mean = 0;
int standardDeviation = 0;
String readcountsFilename = null;
int transcriptCountforGene = 1;
int totalReadCount = 100;
double errorate = 0.01;
String fastaFilename = null;
String fastaidxFilename = null;
String gtfFilename = null;
String outputpath = null;
double dotMutationRates[] = new double[]{0, 0, 0, 0};
double inMutationRates[] = new double[]{0, 0, 0, 0};
double delMutationRates[] = new double[]{0, 0, 0, 0};
double juncMutation = 0;
int junctionSize = 3;
boolean overlapOnMoreThan1T = false;
int minoverlapsize = 0;
int nonuniqueFrequency = 50;
boolean biolog = false;
String biologFile = "";
//for output
int uniqueCount = 0;
int nonUniqueCount = 0;
int unKnownCount = 0;
for (int i = 0; i < args.length - 1; i++) {
if (args[i].equals("-length")) {
if (!args[i + 1].startsWith("-")) {
readLength = Integer.parseInt(args[i + 1]);
}
} else if (args[i].equals("-gene_id")) {
if (!args[i + 1].startsWith("-")) {
gene_id = (args[i + 1]);
}
} else if (args[i].equals("-frlength")) {
if (!args[i + 1].startsWith("-")) {
mean = Integer.parseInt(args[i + 1]);
}
} else if (args[i].equals("-SD")) {
if (!args[i + 1].startsWith("-")) {
standardDeviation = Integer.parseInt(args[i + 1]);
}
} else if (args[i].equals("-readcounts")) {
if (!args[i + 1].startsWith("-")) {
readcountsFilename = args[i + 1];
}
} else if (args[i].equals("-non_unique_frq")) {
if (!args[i + 1].startsWith("-")) {
nonuniqueFrequency = Integer.parseInt(args[i + 1]);
}
} else if (args[i].equals("-dot_mutation_rate")) {
if (!args[i + 1].startsWith("-")) {
dotMutationRates = readArrayInput(args[i + 1]);
}
} else if (args[i].equals("-insertion_mutation_rate")) {
if (!args[i + 1].startsWith("-")) {
inMutationRates = readArrayInput(args[i + 1]);
}
} else if (args[i].equals("-deletion_mutation_rate")) {
if (!args[i + 1].startsWith("-")) {
delMutationRates = readArrayInput(args[i + 1]);
}
} else if (args[i].equals("-junction_mutation_rate")) {
if (!args[i + 1].startsWith("-")) {
juncMutation = Double.parseDouble(args[i + 1]);
}
} else if (args[i].equals("-junction_size")) {
if (!args[i + 1].startsWith("-")) {
junctionSize = Integer.parseInt(args[i + 1]);
}
} else if (args[i].equals("-sequence_error_rate")) {
if (!args[i + 1].startsWith("-")) {
errorate = Double.parseDouble(args[i + 1]);
}
} else if (args[i].equals("-fasta")) {
if (!args[i + 1].startsWith("-")) {
fastaFilename = args[i + 1];
}
} else if (args[i].equals("-fidx")) {
if (!args[i + 1].startsWith("-")) {
fastaidxFilename = args[i + 1];
}
} else if (args[i].equals("-gtf")) {
if (!args[i + 1].startsWith("-")) {
gtfFilename = args[i + 1];
}
} else if (args[i].equals("-biological")) {
if (!args[i + 1].startsWith("-")) {
if (args[i + 1].equals("true")) {
biolog = true;
}
}
} else if (args[i].equals("-bio_file")) {
if (!args[i + 1].startsWith("-")) {
biologFile = args[i + 1];
}
} else if (args[i].equals("-od")) {
if (!args[i + 1].startsWith("-")) {
outputpath = args[i + 1];
}
} else if (args[i].equals("-overlap_on_more_than_1_T")) {
if (!args[i + 1].startsWith("-")) {
if (args[i + 1].equals("true")) {
overlapOnMoreThan1T = true;
}
}
} else if (args[i].equals("-minoverlapsize")) {
if (!args[i + 1].startsWith("-")) {
minoverlapsize = Integer.parseInt(args[i + 1]);
}
} else if (args[i].equals("-transcript_count_for_gene")) {
if (!args[i + 1].startsWith("-")) {
transcriptCountforGene = Integer.parseInt(args[i + 1]);
}
} else if (args[i].equals("-total_read_count")) {
if (!args[i + 1].startsWith("-")) {
totalReadCount = Integer.parseInt(args[i + 1]);
}
}
}
//notifications
if (fastaidxFilename == null) {
System.out.println("fasta index file path is missing");
System.exit(1);
} else if (gtfFilename == null) {
System.out.println("gtf file path is missing");
System.exit(1);
} else if (gene_id == null) {
System.out.println("gene id is missing");
System.exit(1);
} else if (fastaFilename == null) {
System.out.println("fasta file path is missing");
System.exit(1);
} else if (readLength == 0) {
System.out.println("readLength is missing");
System.exit(1);
} else if (mean == 0) {
System.out.println("mean is missing");
System.exit(1);
} else if (standardDeviation == 0) {
System.out.println("standard deviation is missing");
System.exit(1);
} else if (outputpath == null) {
System.out.println("outputhpath is missing");
System.exit(1);
}/* else if (totalReadCount == 0) {
System.out.println("total read count is missing");
System.exit(1);
}*/
//Dateien einlesen
//readcounts file for transcripts who should be simulated
ArrayList<String[]> readcounts = new ArrayList<>();
if (readcountsFilename != null) {
readcounts = FileUtils.readCounts(readcountsFilename);
}
boolean needOriginal = false;
if(minoverlapsize!=0 || overlapOnMoreThan1T== true){
needOriginal = true;
}
HashMap<String, Gene> gtfMap;
gtfMap = FileUtils.readGTFGeneBased(gtfFilename, gene_id, readcounts); //reads rows with gene and exon as feature
readcounts = completeReadCount(gtfMap, gene_id, transcriptCountforGene, readcounts, totalReadCount);
addOverlapsforGene(gtfMap, gene_id, overlapOnMoreThan1T, minoverlapsize);
addNonOverlapsforGene(gtfMap, gene_id);
if(needOriginal){
addOriginalOverlapsforGene(gtfMap,gene_id);
addOriginalNonOverlapsforGene(gtfMap,gene_id);
}
try {
GenomeSequenceExtractor extractor = new GenomeSequenceExtractor(new File(fastaFilename), new File(fastaidxFilename));
String geneSequence = "";
int geneStart = 0;
int rmember = 0;
HashMap<String, String> geneMap = new HashMap<>();
HashMap<String, String> original_geneMap = new HashMap<>();
FileWriter writefileFW = new FileWriter(outputpath + "/fw.fastq");
FileWriter writefileRW = new FileWriter(outputpath + "/rw.fastq");
FileWriter writefileRM = new FileWriter(outputpath + "/read.mappinginfo");
FileWriter writefileAdditonal = new FileWriter(outputpath + "/read.additionalinfo");
writefileRM.write("readid\tchr\tgene\ttranscript\tt_fw_regvec\tt_rw_regvec\tfw_regvec\trw_regvec\tfw_mut\trw_mut\ttype\tBaseDiffFw\tBaseDiffRw"+ "\n");
String[] inoutAdd = new String[]{"gene id: " + gene_id + "\tread length: " + readLength + "\tfragment length average: " + mean};
FileUtils.writeTsvLine(writefileAdditonal, inoutAdd);
writefileAdditonal.write("Transkript IDS from reads\toverlap region in percent\n");
ArrayList<String[]> otherGenes = new ArrayList<>();
for (int i = 0; i < readcounts.size(); i++) {
//System.out.println(readcounts.get(i)[0]);
String geneID = readcounts.get(i)[0];
String transcriptID = readcounts.get(i)[1];
StringBuilder transcirptSequenceBuilder = new StringBuilder();
String chr = "";
ArrayList<Integer[]> exonsInTrans = new ArrayList<>();
Gene g = gtfMap.get(geneID);
if (!geneMap.containsKey(geneID)) {
geneSequence = extractor.getSequence(g.data.seqname, g.data.start, g.data.end); //get gene Sequence
original_geneMap.put(geneID, geneSequence);
//apply mutation
Mutation mutation = simulateJunctionsMutation(g, geneSequence, juncMutation, junctionSize);
geneSequence = mutation.seq;
mutation = simulateDotMutation(g, geneSequence, dotMutationRates);
geneSequence = mutation.seq;
mutation = simulateIndelMutation(g, geneSequence, inMutationRates, delMutationRates);
geneSequence = mutation.seq;
geneMap.put(geneID, geneSequence);
} else {
geneSequence = geneMap.get(geneID);
}
// transcriptID="ENST00000643665";
chr = g.data.seqname;
geneStart = g.data.start;
Transcript t = g.transcriptMap.get(transcriptID);
//System.out.println(transcriptID);
String transcriptSeq = "";
String originalTranscriptSeq = "";
transcriptSeq = getTranscriptSequence(t, geneSequence, geneStart, exonsInTrans);
originalTranscriptSeq = getTranscriptSequence(t, original_geneMap.get(geneID), geneStart, new ArrayList<>());
//System.out.println(transcriptID);
//System.out.println(transcriptSeq);
int readcount = 0;
if (readcounts.get(i).length > 2) {
readcount = Integer.parseInt(readcounts.get(i)[2]);
}
//System.out.println(readcount);
//int rmember =0;
Random random = new Random();
String type = "";
for (int r = 0; r < readcount; r++) {
int transcriptLen = transcriptSeq.length();
int fragmentLen = getSampleFragLen(readLength, mean, standardDeviation, transcriptLen);
//int pos =selectRandomPos(transcriptLen, fragmentLen);
int pos = -1;
int mal =0;
if (biolog) {
TranscriptFragmentDistribution fragDis = new TranscriptFragmentDistribution(biologFile);
ArrayList<Integer> tmp = fragDis.transcriptFragments.get(transcriptID);
boolean inExon = false;
boolean enspace = false;
GtfRow lexon = t.exons.get(t.exons.size()-1);
int relativePos = -1;
if (tmp != null) {
NormalDistribution nd = fragDis.getFragStart(tmp);
double numreads = fragDis.transcript_readcons.get(transcriptID);
double mm = fragDis.transcript_mm.get(transcriptID);
errorate =mm/numreads;
int sample = (int)nd.sample();
// sample=41603093;
// fragmentLen=288;
//
//
//System.out.println(sample+" "+fragmentLen);
int end =lexon.end;
int check = end-sample-2;
if(check>fragmentLen){
enspace=true;
}
// pos = sample - t.getStart(); // fragSTart
for(GtfRow e: t.exons){
if(sample>= e.start && sample < e.end){
pos=sample-e.start;
inExon = true;
break;
}
}
if((enspace&&inExon)){
mal++;
if(mal>=80){
pos = selectRandomPos(transcriptLen, fragmentLen);
}
}
} else {
System.err.println("You should choose one of the following genes for biological Closeness :\n" +
"LZTFL1, CCR9, HIVEP3, SLAMF1, SLAMF7, and FYCO1");
biolog = false;
// System.exit(69);
}
}
if(pos == -1){
if (geneID.equals(gene_id)) {
if (random.nextDouble() * 100 < nonuniqueFrequency) {
pos = selectPosFromOverlap(transcriptLen, fragmentLen, t);
//type = "NonUnique area";
//nonUniqueCount += 1;
} else {
pos = selectPosFromNonOverlap(transcriptLen, fragmentLen, t);
//type = "Unique areas included";
//uniqueCount += 1;
}
if (pos == -1) {
pos = selectRandomPos(transcriptLen, fragmentLen);
}
type = checkPos(t,pos, needOriginal);
if(type.equals("Unique area")){
uniqueCount += 1;
}else if(type.equals("NonUnique area")){
nonUniqueCount += 1;
}
} else {
pos = selectRandomPos(transcriptLen, fragmentLen);
type = "Random";
}
}
//System.out.println(transcriptID+" pos: "+pos+" end: "+(pos+fragmentLen)+" t len : "+transcriptSeq.length());
String fragmentSeq = getFragmentSeq(transcriptSeq, pos, (pos + fragmentLen));
String orginalFragmentSeq = getFragmentSeq(originalTranscriptSeq, pos, (pos + fragmentLen));
if (g.data.strand.equals("-")) {
fragmentSeq = reverseCompliment(fragmentSeq);
orginalFragmentSeq = reverseCompliment(orginalFragmentSeq);
}
String[] seq = getReadSeq(fragmentSeq, readLength);
String[] originalSeq = getReadSeq(orginalFragmentSeq, readLength);
int diffFW = compareReads(seq[0], originalSeq[0]);
int diffRW = compareReads(seq[1], originalSeq[1]);
//error rate
SequenceError fw = simulateSequenceErrors(seq[0], errorate);
SequenceError rw = simulateSequenceErrors(seq[1], errorate);
FileUtils.writeFastq(rmember, fw.seq, writefileFW);
FileUtils.writeFastq(rmember, rw.seq, writefileRW);
/*String fwVectors = getVector(exonsInTrans, pos, readLength); //mit 810 getestet
int rwPos = (pos + fragmentLen) - readLength;
String rwVectors = getVector(exonsInTrans, rwPos, readLength); //mit 858 getestet
*/
/*String fwVectors = vector_pos(g.data.strand, exonsInTrans,pos,readLength);
int rwPos = (pos + fragmentLen) - readLength;
String rwVectors = vector_pos(g.data.strand,exonsInTrans, rwPos, readLength);
*/
if (g.data.strand.equals("-")) {
int r_rev_s = transcriptSeq.length() - pos;
int fw_start = r_rev_s - readLength;
int rw_start = r_rev_s - fragmentLen;
//int rwPos = (pos + fragmentLen) - readLength;
String fwVectors = getVector(exonsInTrans, fw_start, readLength); //mit 810 getestet
String rwVectors = getVector(exonsInTrans, rw_start, readLength); //mit 858 getestet
int rwStart = transcriptLen - pos - readLength;
FileUtils.writeReadMapping(getReadMappingInformation(g.data.strand, rmember, chr, geneID, transcriptID, rwStart, readLength,
fragmentLen, rwVectors, fwVectors, fw.positions, rw.positions, type, diffFW, diffRW), writefileRM);
} else {
String fwVectors = getVector(exonsInTrans, pos, readLength); //mit 810 getestet
int rwPos = (pos + fragmentLen) - readLength;
String rwVectors = getVector(exonsInTrans, rwPos, readLength); //mit 858 getestet
FileUtils.writeReadMapping(getReadMappingInformation(g.data.strand, rmember, chr, geneID, transcriptID, pos, readLength,
fragmentLen, fwVectors, rwVectors, fw.positions, rw.positions, type, diffFW, diffRW), writefileRM);
}
//FileUtils.writeFragmentlength(rmember,fragmentLen,writefilePlotFL);
//FileUtils.writeMutationDis(rmember, fw.positions, rw.positions, writefileMD);
rmember += 1;
}
String[] input = new String[2];
if(needOriginal){
input = new String[]{transcriptID, String.valueOf(t.originaloverlapinPercentage)};
}else{
input = new String[]{transcriptID, String.valueOf(t.overlapinPercentage)};
}
if (geneID.equals(gene_id)) {
FileUtils.writeTsvLine(writefileAdditonal, input);
} else {
String[] otherGene = new String[]{geneID, transcriptID, readcounts.get(i)[2]};
otherGenes.add(otherGene);
}
}
String[] unique_RC = new String[]{"reads in unique areas: ", String.valueOf(uniqueCount)};
FileUtils.writeTsvLine(writefileAdditonal, unique_RC);
String[] nonunique_RC = new String[]{"reads in non unique areas: ", String.valueOf(nonUniqueCount)};
FileUtils.writeTsvLine(writefileAdditonal, nonunique_RC);
String[] unknown_RC = new String[]{"random area: ", String.valueOf(unKnownCount)};
FileUtils.writeTsvLine(writefileAdditonal, unknown_RC);
String[] seqErrorR = new String[]{"sequence error rate:", String.valueOf(errorate)};
FileUtils.writeTsvLine(writefileAdditonal, seqErrorR);
String[] dotMutationRate = new String[]{"dotmutation rate:", "A: ", String.valueOf(dotMutationRates[0]), "\tC: ", String.valueOf(dotMutationRates[1]), "\tG: ", String.valueOf(dotMutationRates[2]), "\tT: ", String.valueOf(dotMutationRates[3])};
FileUtils.writeTsvLine(writefileAdditonal, dotMutationRate);
String[] inMutationRate = new String[]{"insertion rate:", "A: ", String.valueOf(inMutationRates[0]), "\tC: ", String.valueOf(inMutationRates[1]), "\tG: ", String.valueOf(inMutationRates[2]), "\tT: ", String.valueOf(inMutationRates[3])};
FileUtils.writeTsvLine(writefileAdditonal, inMutationRate);
String[] delMutationRate = new String[]{"deletion rate:", "A: ", String.valueOf(delMutationRates[0]), "\tC: ", String.valueOf(delMutationRates[1]), "\tG: ", String.valueOf(delMutationRates[2]), "\tT: ", String.valueOf(delMutationRates[3])};
FileUtils.writeTsvLine(writefileAdditonal, delMutationRate);
writefileAdditonal.write("junction area mutation: " + String.valueOf(juncMutation) + "\t\tjunction area size: " + String.valueOf(junctionSize) + "\n");
writefileAdditonal.write("overlap reads fit to more than two transcripts: " + String.valueOf(overlapOnMoreThan1T) + "\n");
writefileAdditonal.write("minimum overlap size: " + String.valueOf(minoverlapsize) + "\n");
writefileAdditonal.write("reads from other genes: \n");
for (String[] ele : otherGenes) {
writefileAdditonal.write(ele[0] + "\t " + ele[1] + "\t " + ele[2]);
}
writefileFW.close();
writefileRW.close();
writefileRM.close();
writefileAdditonal.close();
//writefilePlotFL.close();
//writefileMD.close();
} catch (IOException e) {
System.out.println("extractions failed: " + e.getMessage());
}
}
private static String getTranscriptSequence(Transcript t, String geneSequence, int geneStart, ArrayList<Integer[]> exonsInTrans) {
StringBuilder transcriptSequenceBuilder = new StringBuilder();
String transcriptSeq = "";
for (GtfRow exon : t.exons) {
String exonSeq = "";
if (exon.start - geneStart < geneSequence.length()) {
int end = exon.end - geneStart + 1;
if (end > geneSequence.length()) {
end = geneSequence.length();
}
exonSeq = geneSequence.substring(exon.start - geneStart, end);
}
Integer[] eChrPos = new Integer[2];
eChrPos[0] = exon.start;
eChrPos[1] = exon.end;
exonsInTrans.add(eChrPos);
transcriptSequenceBuilder.append(exonSeq);
}
transcriptSeq = transcriptSequenceBuilder.toString();
if (t.exons.get(1).strand.equals("-")) {
StringBuilder rev = new StringBuilder();
for (int k = transcriptSeq.length() - 1; k >= 0; k--) {
char c = transcriptSeq.charAt(k);
if (c == 'T') {
rev.append('A');
}
if (c == 'A') {
rev.append('T');
}
if (c == 'C') {
rev.append('G');
}
if (c == 'G') {
rev.append('C');
}
}
//rev.reverse();
transcriptSeq = rev.toString();
}
return transcriptSeq;
}
private static int compareReads(String r1, String r2) {
int diff = 0;
for (int i = 0; i < r1.length(); i++) {
if (r1.charAt(i) != r2.charAt(i)) {
diff += 1;
}
}
return diff;
}
private static double[] readArrayInput(String input) {
String[] elements = input.split(",");
if (elements.length == 1) {
double p = Double.parseDouble(elements[0]);
return new double[]{p, p, p, p};
}
if (elements.length == 4) {
double[] result = new double[4];
for (int i = 0; i < elements.length; i++) {
result[i] = Double.parseDouble(elements[i]);
}
return result;
}
System.out.println("number of input values are wrong: " + input);
System.exit(1);
return null;
}
private static ArrayList<String[]> completeReadCount(HashMap<String, Gene> gtfMap, String gene_id, int transcriptCountForGene,
ArrayList<String[]> readcounts, int totalreadCount) {
HashSet<String> transcriptsOfGivenGene = new HashSet<>();
int sumReadCounts = 0;
Gene givenGene = gtfMap.get(gene_id);
boolean tCisZero = false;
if (transcriptCountForGene == 0) {
tCisZero = true;
}
for (String[] row : readcounts) {
if (row[0].equals(gene_id)) {
transcriptsOfGivenGene.add(row[1]);
if (row.length > 2) {
sumReadCounts += Integer.parseInt(row[2]);
}
if (tCisZero) { //so that transcript count is optional
transcriptCountForGene += 1;
}
}
}
totalreadCount = totalreadCount - sumReadCounts;
int leftTranskript = transcriptCountForGene -transcriptsOfGivenGene.size();
int meanCounts = 0;
if(readcounts.size()!= 0 && readcounts.get(1).length<3){
meanCounts = totalreadCount / transcriptCountForGene; // no readcount given
}else {
meanCounts = totalreadCount / leftTranskript; // readcount given
}
//NormalDistribution nd = new NormalDistribution(meanCounts, (meanCounts / 10));
int entries = readcounts.size();
for (int i = 0; i < readcounts.size(); i++) {
String[] row = readcounts.get(i);
if (row[0].equals(gene_id)) {
transcriptsOfGivenGene.add(row[1]);
if (row.length == 2 || Integer.parseInt(row[2]) == 0) {
if (i == entries - 1) {
String[] newElement = {row[0], row[1], String.valueOf(totalreadCount)};
readcounts.set(i, newElement);
} else {
NormalDistribution nd = new NormalDistribution(meanCounts, (meanCounts / 10));
int count = (int) nd.sample();
String[] newElement = {row[0], row[1], String.valueOf(count)};
readcounts.set(i, newElement);
totalreadCount -= count;
}
}
}
}
transcriptCountForGene = transcriptCountForGene - transcriptsOfGivenGene.size();
ArrayList<String> transcriptList = new ArrayList<>(givenGene.transcriptMap.keySet());
Collections.shuffle(transcriptList);
for (String t : transcriptList) {
if (transcriptCountForGene <= 0) {
break;
}
if (!transcriptsOfGivenGene.contains(t)) {
NormalDistribution nd = new NormalDistribution(meanCounts, (meanCounts / 10));
int count = (int) nd.sample();
if (transcriptCountForGene == 1) {
count = totalreadCount;
}
String[] newElement = {gene_id, t, String.valueOf(count)}; // meanCount add standard deviation
readcounts.add(newElement);
transcriptCountForGene--;
totalreadCount -= count;
}
}
return readcounts;
}
public static String vector_pos(String strand, ArrayList<Integer[]> exonsInTrans, int fr_start, int len) {
ArrayList<String> exons = new ArrayList<>();
int ex_lens = 0;
int read_len = 0;
if (strand.equals("-")) {
for (Integer[] e : exonsInTrans) {
int startE = e[0];
int endE = e[1];
int lenE = endE - startE + 1;
ex_lens += lenE;
if (ex_lens >= fr_start) {
int ex_start = endE - (ex_lens - fr_start) + read_len + 1;
int increment = (ex_lens - fr_start);
int ex_end = ex_start + increment;
if (ex_end > endE + 1) {
ex_end = endE + 1;
}
read_len += (ex_end - ex_start);
if (read_len >= len) {
ex_end = ex_end - (read_len - len);
if (ex_end - ex_start > 0) {
String exs = ex_start + "-" + ex_end;
exons.add(exs);
}
break;
} else {
if (ex_end - ex_start > 0) {
String exs = ex_start + "-" + ex_end;
exons.add(exs);
}
}
}
}
} else {
for (Integer[] e : exonsInTrans) {
int startE = e[0];
int endE = e[1];
int lenE = endE - startE + 1;
ex_lens += lenE;
if (ex_lens >= fr_start) {
int ex_start = startE + lenE - (ex_lens - fr_start) + read_len;
int increment = (ex_lens - fr_start);
int ex_end = ex_start + increment;
if (ex_end > endE + 1) {
ex_end = endE + 1;
}
read_len += (ex_end - ex_start);
if (read_len >= len) {
ex_end = ex_end - (read_len - len);
if (ex_end - ex_start > 0) {
String exs = ex_start + "-" + ex_end;
exons.add(exs);
}
break;
} else {
if (ex_end - ex_start > 0) {
String exs = ex_start + "-" + ex_end;
exons.add(exs);
}
}
}
}
}
StringBuilder resultS = new StringBuilder();
for (String e : exons) {
if (!resultS.isEmpty()) {
resultS.append("|");
}
resultS.append(e);
}
return resultS.toString();
}
public static void addOverlapsforGene(HashMap<String, Gene> gtfMap, String gene_id, boolean overlapOnMoreThan1T, int minoverlapsize) {
Gene g = gtfMap.get(gene_id);
for (Transcript t1 : g.transcriptMap.values()) {
for (Transcript t2 : g.transcriptMap.values()) {
if (!t1.equals(t2)) {
t1.addOverlapsFor(t2, minoverlapsize);
}
}
Collections.sort(t1.overlaps, new Comparator<Overlap>() {
@Override
public int compare(Overlap o1, Overlap o2) {
return o1.startPos - o2.startPos;
}
});
if (overlapOnMoreThan1T) {
int i = 1;
while (i < t1.overlaps.size()) {
Overlap olOld = t1.overlaps.get(i - 1);
Overlap olNew = t1.overlaps.get(i);
if (olOld.startPos <= olNew.startPos && olOld.endPos >= olNew.endPos) {
//------------
// -----
t1.overlaps.remove(i - 1);
} else if (olOld.startPos >= olNew.startPos && olOld.endPos <= olNew.endPos) {
// -----
//----------
t1.overlaps.remove(i);
} else {
i++;
}
}
}
}
}
public static void addOriginalOverlapsforGene(HashMap<String, Gene> gtfMap, String gene_id ) {
Gene g = gtfMap.get(gene_id);
for (Transcript t1 : g.transcriptMap.values()) {
for (Transcript t2 : g.transcriptMap.values()) {
if (!t1.equals(t2)) {
t1.addOriginalOverlapsFor(t2);
}
}
Collections.sort(t1.orignialOverlaps, new Comparator<Overlap>() {
@Override
public int compare(Overlap o1, Overlap o2) {
return o1.startPos - o2.startPos;
}
});
}
}
public static void addNonOverlapsforGene(HashMap<String, Gene> gtfMap, String gene_id) {
Gene g = gtfMap.get(gene_id);
for (Transcript t : g.transcriptMap.values()) {
int start = 0;
int transcriptLen = 0;
for (GtfRow exon : t.exons) {
transcriptLen += exon.end - exon.start + 1;
}
//System.out.println(t.exons.get(1).attribute.get("transcript_id"));
//System.out.println("overlaps region: ");
int nonOverlapLen = 0;
for (Overlap o : t.overlaps) {
//System.out.println(o.startPos + " - " + o.endPos);
if (o.startPos <= start) {
if (start < o.endPos + 1) {
start = o.endPos + 1;
}
} else {
Nonoverlap nonoverlaps = new Nonoverlap();
nonoverlaps.startPos = start;
nonoverlaps.endPos = o.startPos - 1;
t.nonoverlaps.add(nonoverlaps);
start = o.endPos + 1;
nonOverlapLen += (nonoverlaps.endPos - nonoverlaps.startPos);
}
}
if (start < transcriptLen) {
Nonoverlap nonoverlaps = new Nonoverlap();
nonoverlaps.startPos = start;
nonoverlaps.endPos = transcriptLen - 1;
t.nonoverlaps.add(nonoverlaps);
nonOverlapLen += (nonoverlaps.endPos - nonoverlaps.startPos);
}
/*System.out.println("nonoverlap: ");
for (Nonoverlap no : t.nonoverlaps) {
System.out.println(no.startPos + " - " + no.endPos);
}
System.out.println(transcriptLen + " : " + nonOverlapLen);
*/
t.overlapinPercentage = Math.round((100 - ((double) nonOverlapLen * 100) / transcriptLen) * 1000) / 1000;
}
}
public static void addOriginalNonOverlapsforGene(HashMap<String, Gene> gtfMap, String gene_id) {
Gene g = gtfMap.get(gene_id);
for (Transcript t : g.transcriptMap.values()) {
int start = 0;
int transcriptLen = 0;
for (GtfRow exon : t.exons) {
transcriptLen += exon.end - exon.start + 1;
}
//System.out.println(t.exons.get(1).attribute.get("transcript_id"));
//System.out.println("overlaps region: ");
int nonOverlapLen = 0;
for (Overlap o : t.orignialOverlaps) {
//System.out.println(o.startPos + " - " + o.endPos);
if (o.startPos <= start) {
if (start < o.endPos + 1) {
start = o.endPos + 1;
}
} else {
Nonoverlap nonoverlaps = new Nonoverlap();
nonoverlaps.startPos = start;
nonoverlaps.endPos = o.startPos - 1;
t.orignialNonoverlaps.add(nonoverlaps);
start = o.endPos + 1;
nonOverlapLen += (nonoverlaps.endPos - nonoverlaps.startPos);
}
}
if (start < transcriptLen) {
Nonoverlap nonoverlaps = new Nonoverlap();
nonoverlaps.startPos = start;
nonoverlaps.endPos = transcriptLen - 1;
t.orignialNonoverlaps.add(nonoverlaps);
nonOverlapLen += (nonoverlaps.endPos - nonoverlaps.startPos);
}
t.originaloverlapinPercentage = Math.round((100 - ((double) nonOverlapLen * 100) / transcriptLen) * 1000) / 1000;
}
}
private static int getSampleFragLen(int readLen, int mean, int sd, int transcriptLen) {
NormalDistribution nd = new NormalDistribution(mean, sd);
int value = 0;
while (value < readLen || value > transcriptLen) {
value = (int) Math.round(nd.sample());
}
return value;
}
private static int selectRandomPos(int transcriptLen, int fragmentLen) {
double value = Math.random() * (transcriptLen - fragmentLen);
return (int) Math.floor(value);
}
private static int selectPosFromOverlap(int transcriptLen, int fragmentLen, Transcript t) {
Collections.shuffle(t.overlaps);
/*
for (Overlaps o : t.overlaps) {
//System.out.println(o.startPos + " - " + o.endPos);
if ((o.startPos + fragmentLen) <= transcriptLen) {
return o.startPos ;
} else if (o.endPos - fragmentLen >= 0) {
return o.endPos - fragmentLen;
}
}
*/
//System.out.println(transcriptLen);
//System.out.println(fragmentLen);
double random = Math.random();
for (int i = 2; i < 5; i++) {
int shift = (int) (random * (fragmentLen / i));
for (Overlap o : t.overlaps) {
if ((o.startPos + shift + fragmentLen) <= transcriptLen) {
//System.out.println("first: " + (o.startPos + shift));
return o.startPos + shift;
} else if ((o.endPos - fragmentLen - shift >= 0) && o.endPos<= fragmentLen) {
//System.out.println("second: " + (o.endPos - fragmentLen - shift));
return o.endPos - fragmentLen - shift;
}
}
}
//System.out.println("xxx - " + t.overlaps.size());
return -1;
}
private static int selectPosFromNonOverlap(int transcriptLen, int fragmentLen, Transcript t) {
Collections.shuffle(t.nonoverlaps);
double random = Math.random();
for (int i = 10; i > 8; i--) {
int shift = (int) (random * (fragmentLen / i));
for (Nonoverlap no : t.nonoverlaps) {
if (((no.startPos + shift) < no.endPos) && (no.startPos + shift + fragmentLen) <= transcriptLen) {
return no.startPos + shift;
} else if ((no.endPos <= fragmentLen) && (no.endPos - fragmentLen - shift >= 0)) {
return no.endPos - fragmentLen - shift;
}
}
}
//System.out.println("xxx - " + t.overlaps.size());
return -1;
}
private static String getFragmentSeq(String transcriptseq, int start, int end) {
int trans_len = transcriptseq.length();
String fragmentSeq = "";
fragmentSeq = transcriptseq.substring(start, end);
return fragmentSeq;
}
private static String[] getReadSeq(String fragmentSeq, int readLen) {
String forwardSeq = fragmentSeq.substring(0, readLen); //fw
StringBuilder reversSeq = new StringBuilder();
for (int i = fragmentSeq.length() - 1; i > fragmentSeq.length() - readLen - 1; i--) {
char nuk = fragmentSeq.charAt(i);
if (nuk == 'A') {
reversSeq.append('T');
}
if (nuk == 'T') {
reversSeq.append('A');
}
if (nuk == 'G') {
reversSeq.append('C');
}
if (nuk == 'C') {
reversSeq.append('G');
}
}
return new String[]{forwardSeq, reversSeq.toString()};
}
private static SequenceError simulateSequenceErrors(String transcriptSeq, double errorRate) {
int transciptLen = transcriptSeq.length();//fragmente len eigentlich
StringBuilder seq = new StringBuilder();
List<Integer> positions = new ArrayList<>();
List<Character> aList = Arrays.asList('T', 'C', 'G');
List<Character> tList = Arrays.asList('A', 'C', 'G');
List<Character> cList = Arrays.asList('A', 'T', 'G');
List<Character> gList = Arrays.asList('A', 'C', 'T');
Random random = new Random();
int randomChar = 0;
for (int i = 0; i < transciptLen; i++) {