-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_editor.py
1352 lines (1107 loc) · 48.3 KB
/
test_editor.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
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
from pathlib import Path
from unittest import mock
import pytest
import charset_normalizer
from manubot_ai_editor import env_vars
from manubot_ai_editor.editor import ManuscriptEditor
from manubot_ai_editor.models import (
ManuscriptRevisionModel,
RandomManuscriptRevisionModel,
DummyManuscriptRevisionModel,
VerboseManuscriptRevisionModel,
)
MANUSCRIPTS_DIR = Path(__file__).parent / "manuscripts"
def _check_nonparagraph_lines_are_preserved(input_filepath, output_filepath):
"""
Checks whether non-paragraph lines in the input file are preserved in the output file.
Non-paragraph lines are those that represent section headers, comments, blank lines, as
defined in function ManuscriptEditor.line_is_not_part_of_paragraph.
"""
# read lines from input file
filepath = input_filepath
assert filepath.exists()
with open(filepath, "r") as infile:
input_nonpar_lines = [
line.rstrip()
for line in infile
if ManuscriptEditor.line_is_not_part_of_paragraph(line)
]
# read lines from output file
filepath = output_filepath
assert filepath.exists()
with open(filepath, "r") as infile:
output_nonpar_lines = [
line.rstrip()
for line in infile
if ManuscriptEditor.line_is_not_part_of_paragraph(line)
]
# make sure all lines that are not "paragraphs" are preserved
for input_line in input_nonpar_lines:
# make sure there are nonparagraph lines left in output file
assert (
len(output_nonpar_lines) > 0
), "Output file has less non-paragraph lines than input file"
# make sure nonparagraph lines are in the same order
assert (
input_line == output_nonpar_lines[0]
), f"{input_line} != {output_nonpar_lines[0]}"
output_nonpar_lines.remove(input_line)
# if nonparagraph lines were preserved, then the output_nonpar_liens should be empty
assert (
len(output_nonpar_lines) == 0
), f"Non-paragraph lines are not the same: {output_nonpar_lines}"
@pytest.mark.parametrize(
"model",
[
RandomManuscriptRevisionModel(),
# GPT3CompletionModel(None, None),
],
)
def test_revise_abstract(tmp_path, model):
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "ccc",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file("01.abstract.md", tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR / "ccc" / "01.abstract.md",
output_filepath=tmp_path / "01.abstract.md",
)
@pytest.mark.parametrize(
"model",
[
RandomManuscriptRevisionModel(),
# GPT3CompletionModel(None, None),
],
)
def test_revise_introduction(tmp_path, model):
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "ccc",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file("02.introduction.md", tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR / "ccc" / "02.introduction.md",
output_filepath=tmp_path / "02.introduction.md",
)
def test_get_section_from_filename():
assert ManuscriptEditor.get_section_from_filename("00.front-matter.md") is None
assert ManuscriptEditor.get_section_from_filename("01.abstract.md") == "abstract"
assert (
ManuscriptEditor.get_section_from_filename("02.introduction.md")
== "introduction"
)
assert ManuscriptEditor.get_section_from_filename("02.intro.md") is None
assert ManuscriptEditor.get_section_from_filename("03.results.md") == "results"
assert (
ManuscriptEditor.get_section_from_filename("04.10.results_comp.md") == "results"
)
assert (
ManuscriptEditor.get_section_from_filename("04.discussion.md") == "discussion"
)
assert (
ManuscriptEditor.get_section_from_filename("05.conclusions.md") == "conclusions"
)
assert (
ManuscriptEditor.get_section_from_filename("08.01.methods.ccc.md") == "methods"
)
assert (
ManuscriptEditor.get_section_from_filename("08.15.methods.giant.md")
== "methods"
)
assert ManuscriptEditor.get_section_from_filename("07.references.md") is None
assert ManuscriptEditor.get_section_from_filename("06.acknowledgements.md") is None
assert (
ManuscriptEditor.get_section_from_filename("08.supplementary.md")
== "supplementary material"
)
@mock.patch.dict(
"os.environ",
{
env_vars.SECTIONS_MAPPING: r"""
{"02.intro.md": "introduction"}
"""
},
)
def test_get_section_from_filename_using_environment_variable():
assert (
ManuscriptEditor.get_section_from_filename("02.introduction.md")
== "introduction"
)
assert ManuscriptEditor.get_section_from_filename("02.intro.md") == "introduction"
@mock.patch.dict(
"os.environ",
{
env_vars.SECTIONS_MAPPING: r"""
{"02.intro.md": }
"""
},
)
def test_get_section_from_filename_using_environment_variable_is_invalid():
assert (
ManuscriptEditor.get_section_from_filename("02.introduction.md")
== "introduction"
)
assert ManuscriptEditor.get_section_from_filename("02.intro.md") is None
@pytest.mark.parametrize(
"model",
[
RandomManuscriptRevisionModel(),
# GPT3CompletionModel(None, None),
],
)
def test_revise_results_with_header_only(tmp_path, model):
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "ccc",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file("04.00.results.md", tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR / "ccc" / "04.00.results.md",
output_filepath=tmp_path / "04.00.results.md",
)
@pytest.mark.parametrize(
"model",
[
RandomManuscriptRevisionModel(),
# GPT3CompletionModel(None, None),
],
)
def test_revise_results_intro_with_figure(tmp_path, model):
print(f"\n{str(tmp_path)}\n")
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "ccc",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file("04.05.results_intro.md", tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR / "ccc" / "04.05.results_intro.md",
output_filepath=tmp_path / "04.05.results_intro.md",
)
# make sure the "image paragraph" was exactly copied to the output file
assert (
r"""
{#fig:datasets_rel width="100%"}
""".strip()
in open(tmp_path / "04.05.results_intro.md").read()
)
@pytest.mark.parametrize(
"model",
[
RandomManuscriptRevisionModel(),
DummyManuscriptRevisionModel(add_paragraph_marks=True),
# GPT3CompletionModel(None, None),
],
)
def test_revise_results_with_figure_without_caption(tmp_path, model):
print(f"\n{str(tmp_path)}\n")
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "custom",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file("00.results_image_with_no_caption.md", tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR
/ "custom"
/ "00.results_image_with_no_caption.md",
output_filepath=tmp_path / "00.results_image_with_no_caption.md",
)
# make sure the "image paragraph" was exactly copied to the output file
assert (
r"""
{width="100%"}
The tool, again, significantly revised the text, producing a much better and more concise introductory paragraph.
For example, the revised first sentence (on the right) incorportes the ideas of "large datasets", and the "opportunities/possibilities" for "scientific exploration" in a clearly and briefly.
""".strip()
in open(tmp_path / "00.results_image_with_no_caption.md").read()
)
if isinstance(model, DummyManuscriptRevisionModel):
assert (
r"""
%%% PARAGRAPH START %%%
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC:
%%% PARAGRAPH END %%%
""".strip()
in open(tmp_path / "00.results_image_with_no_caption.md").read()
)
@pytest.mark.parametrize(
"model",
[
RandomManuscriptRevisionModel(),
DummyManuscriptRevisionModel(add_paragraph_marks=True),
# GPT3CompletionModel(None, None),
],
)
def test_revise_results_with_table_below_nonended_paragraph(tmp_path, model):
print(f"\n{str(tmp_path)}\n")
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "custom",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file("00.results_table_below_nonended_paragraph.md", tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR
/ "custom"
/ "00.results_table_below_nonended_paragraph.md",
output_filepath=tmp_path / "00.results_table_below_nonended_paragraph.md",
)
# make sure the "image paragraph" was exactly copied to the output file
assert (
r"""
| Pathway | AUC | FDR |
|:------------------------------------|:------|:---------|
| IRIS Neutrophil-Resting | 0.91 | 4.51e-35 |
| SVM Neutrophils | 0.98 | 1.43e-09 |
| PID IL8CXCR2 PATHWAY | 0.81 | 7.04e-03 |
| SIG PIP3 SIGNALING IN B LYMPHOCYTES | 0.77 | 1.95e-02 |
Table: Pathways aligned to LV603 from the MultiPLIER models. {#tbl:sup:multiplier_pathways:lv603}
The tool, again, significantly revised the text, producing a much better and more concise introductory paragraph.
For example, the revised first sentence (on the right) incorportes the ideas of "large datasets", and the "opportunities/possibilities" for "scientific exploration" in a clearly and briefly.
""".strip()
in open(tmp_path / "00.results_table_below_nonended_paragraph.md").read()
)
if isinstance(model, DummyManuscriptRevisionModel):
assert (
r"""
%%% PARAGRAPH START %%%
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC.
This is the revision of the first paragraph of the introduction of CCC:
%%% PARAGRAPH END %%%
""".strip()
in open(tmp_path / "00.results_table_below_nonended_paragraph.md").read()
)
def test_prepare_paragraph_with_simple_text():
paragraph = r"""
This is the first sentence.
And this is the second one.
And this is the third and final one.
"""
paragraph_as_list = paragraph.strip().split("\n")
paragraph_as_list = [sentence.strip() for sentence in paragraph_as_list]
assert len(paragraph_as_list) == 3
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "ccc",
)
prepared_paragraph = me.prepare_paragraph(paragraph_as_list)
assert " ".join(paragraph_as_list) + "\n" == prepared_paragraph
def test_prepare_paragraph_with_introduction_text():
paragraph = r"""
In transcriptomics, many analyses start with estimating the correlation between genes.
More sophisticated approaches built on correlation analysis can suggest gene function [@pmid:21241896], aid in discovering common and cell lineage-specific regulatory networks [@pmid:25915600], and capture important interactions in a living organism that can uncover molecular mechanisms in other species [@pmid:21606319; @pmid:16968540].
The analysis of large RNA-seq datasets [@pmid:32913098; @pmid:34844637] can also reveal complex transcriptional mechanisms underlying human diseases [@pmid:27479844; @pmid:31121115; @pmid:30668570; @pmid:32424349; @pmid:34475573].
"""
paragraph_as_list = paragraph.strip().split("\n")
paragraph_as_list = [sentence.strip() for sentence in paragraph_as_list]
assert len(paragraph_as_list) == 3
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "ccc",
)
prepared_paragraph = me.prepare_paragraph(paragraph_as_list)
assert " ".join(paragraph_as_list) + "\n" == prepared_paragraph
def test_prepare_paragraph_with_equations():
# from PhenoPLIER manuscript:
paragraph_as_list = r"""
S-PrediXcan [@doi:10.1038/s41467-018-03621-1] is the summary version of PrediXcan [@doi:10.1038/ng.3367].
PrediXcan models the trait as a linear function of the gene's expression on a single tissue using the univariate model
$$
\mathbf{y} = \mathbf{t}_l \gamma_l + \bm{\epsilon}_l,
$$ {#eq:predixcan}
where $\hat{\gamma}_l$ is the estimated effect size or regression coefficient, and $\bm{\epsilon}_l$ are the error terms with variance $\sigma_{\epsilon}^{2}$.
The significance of the association is assessed by computing the $z$-score $\hat{z}_{l}=\hat{\gamma}_l / \mathrm{se}(\hat{\gamma}_l)$ for a gene's tissue model $l$.
PrediXcan needs individual-level data to fit this model, whereas S-PrediXcan approximates PrediXcan $z$-scores using only GWAS summary statistics with the expression
$$
\hat{z}_{l} \approx \sum_{a \in model_{l}} w_a^l \frac{\hat{\sigma}_a}{\hat{\sigma}_l} \frac{\hat{\beta}_a}{\mathrm{se}(\hat{\beta}_a)},
$$ {#eq:spredixcan}
where $\hat{\sigma}_a$ is the variance of SNP $a$, $\hat{\sigma}_l$ is the variance of the predicted expression of a gene in tissue $l$, and $\hat{\beta}_a$ is the estimated effect size of SNP $a$ from the GWAS.
In these TWAS methods, the genotype variances and covariances are always estimated using the Genotype-Tissue Expression project (GTEx v8) [@doi:10.1126/science.aaz1776] as the reference panel.
Since S-PrediXcan provides tissue-specific direction of effects (for instance, whether a higher or lower predicted expression of a gene confers more or less disease risk), we used the $z$-scores in our drug repurposing approach (described below).
""".strip().split(
"\n"
)
paragraph_as_list = [sentence.strip() for sentence in paragraph_as_list]
assert len(paragraph_as_list) == 18
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "phenoplier",
)
prepared_paragraph = me.prepare_paragraph(paragraph_as_list)
assert (
r"""S-PrediXcan [@doi:10.1038/s41467-018-03621-1] is the summary version of PrediXcan [@doi:10.1038/ng.3367]. PrediXcan models the trait as a linear function of the gene's expression on a single tissue using the univariate model
$$
\mathbf{y} = \mathbf{t}_l \gamma_l + \bm{\epsilon}_l,
$$ {#eq:predixcan}
where $\hat{\gamma}_l$ is the estimated effect size or regression coefficient, and $\bm{\epsilon}_l$ are the error terms with variance $\sigma_{\epsilon}^{2}$. The significance of the association is assessed by computing the $z$-score $\hat{z}_{l}=\hat{\gamma}_l / \mathrm{se}(\hat{\gamma}_l)$ for a gene's tissue model $l$. PrediXcan needs individual-level data to fit this model, whereas S-PrediXcan approximates PrediXcan $z$-scores using only GWAS summary statistics with the expression
$$
\hat{z}_{l} \approx \sum_{a \in model_{l}} w_a^l \frac{\hat{\sigma}_a}{\hat{\sigma}_l} \frac{\hat{\beta}_a}{\mathrm{se}(\hat{\beta}_a)},
$$ {#eq:spredixcan}
where $\hat{\sigma}_a$ is the variance of SNP $a$, $\hat{\sigma}_l$ is the variance of the predicted expression of a gene in tissue $l$, and $\hat{\beta}_a$ is the estimated effect size of SNP $a$ from the GWAS. In these TWAS methods, the genotype variances and covariances are always estimated using the Genotype-Tissue Expression project (GTEx v8) [@doi:10.1126/science.aaz1776] as the reference panel. Since S-PrediXcan provides tissue-specific direction of effects (for instance, whether a higher or lower predicted expression of a gene confers more or less disease risk), we used the $z$-scores in our drug repurposing approach (described below).
"""
== prepared_paragraph
)
@pytest.mark.parametrize(
"model",
[
RandomManuscriptRevisionModel(),
DummyManuscriptRevisionModel(add_paragraph_marks=True),
],
)
def test_revise_methods_paragraph_with_too_few_sentences_or_words(tmp_path, model):
print(f"\n{str(tmp_path)}\n")
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "phenoplier",
)
me.revise_file("07.00.methods.md", tmp_path, model)
# make sure paragraph was not processed
# first, that original text is there (makes sense for the RandomManuscriptRevisionModel)
assert (
r"""
Since the gold standard of drug-disease medical indications is described with Disease Ontology IDs (DOID) [@doi:10.1093/nar/gky1032], we mapped PhenomeXcan traits to the Experimental Factor Ontology [@doi:10.1093/bioinformatics/btq099] using [@url:https://github.com/EBISPOT/EFO-UKB-mappings], and then to DOID.
"""
in open(tmp_path / "07.00.methods.md").read()
)
# second, that it was not processed as part of a paragraph (makes sense for the DummyManuscriptRevisionModel)
assert (
r"""
%%% PARAGRAPH START %%%
Since the gold standard of drug-disease medical indications is described with Disease Ontology IDs (DOID) [@doi:10.1093/nar/gky1032], we mapped PhenomeXcan traits to the Experimental Factor Ontology [@doi:10.1093/bioinformatics/btq099] using [@url:https://github.com/EBISPOT/EFO-UKB-mappings], and then to DOID.
%%% PARAGRAPH END %%%
"""
not in open(tmp_path / "07.00.methods.md").read()
)
# and same for another paragraph
assert (
r"""
We ran our regression model for all 987 LVs across the 4,091 traits in PhenomeXcan.
For replication, we ran the model in the 309 phecodes in eMERGE.
We adjusted the $p$-values using the Benjamini-Hochberg procedure.
"""
in open(tmp_path / "07.00.methods.md").read()
)
assert (
r"""
%%% PARAGRAPH START %%%
We ran our regression model for all 987 LVs across the 4,091 traits in PhenomeXcan.
For replication, we ran the model in the 309 phecodes in eMERGE.
We adjusted the $p$-values using the Benjamini-Hochberg procedure.
%%% PARAGRAPH END %%%
"""
not in open(tmp_path / "07.00.methods.md").read()
)
@pytest.mark.parametrize(
"model,filename",
[
(DummyManuscriptRevisionModel(add_paragraph_marks=True), "07.00.methods.md"),
# GPT3CompletionModel(None, None),
],
)
def test_revise_methods_with_equation(tmp_path, model, filename):
print(f"\n{str(tmp_path)}\n")
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "phenoplier",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file(filename, tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR / "phenoplier" / filename,
output_filepath=tmp_path / filename,
)
# make sure regular paragraphs are correctly marked
assert (
r"""
%%% PARAGRAPH START %%%
PhenoPLIER is a framework that combines different computational approaches to integrate gene-trait associations and drug-induced transcriptional responses with groups of functionally-related genes (referred to as gene modules or latent variables/LVs).
Gene-trait associations are computed using the PrediXcan family of methods, whereas latent variables are inferred by the MultiPLIER models applied on large gene expression compendia.
PhenoPLIER provides 1) a regression model to compute an LV-trait association, 2) a consensus clustering approach applied to the latent space to learn shared and distinct transcriptomic properties between traits, and 3) an interpretable, LV-based drug repurposing framework.
We provide the details of these methods below.
%%% PARAGRAPH END %%%
""".strip()
in open(tmp_path / filename).read()
)
# make sure the "equation paragraph" is correctly marked
assert (
r"""
%%% PARAGRAPH START %%%
S-PrediXcan [@doi:10.1038/s41467-018-03621-1] is the summary version of PrediXcan [@doi:10.1038/ng.3367].
PrediXcan models the trait as a linear function of the gene's expression on a single tissue using the univariate model
$$
\mathbf{y} = \mathbf{t}_l \gamma_l + \bm{\epsilon}_l,
$$ {#eq:predixcan}
where $\hat{\gamma}_l$ is the estimated effect size or regression coefficient, and $\bm{\epsilon}_l$ are the error terms with variance $\sigma_{\epsilon}^{2}$.
The significance of the association is assessed by computing the $z$-score $\hat{z}_{l}=\hat{\gamma}_l / \mathrm{se}(\hat{\gamma}_l)$ for a gene's tissue model $l$.
PrediXcan needs individual-level data to fit this model, whereas S-PrediXcan approximates PrediXcan $z$-scores using only GWAS summary statistics with the expression
$$
\hat{z}_{l} \approx \sum_{a \in model_{l}} w_a^l \frac{\hat{\sigma}_a}{\hat{\sigma}_l} \frac{\hat{\beta}_a}{\mathrm{se}(\hat{\beta}_a)},
$$ {#eq:spredixcan}
where $\hat{\sigma}_a$ is the variance of SNP $a$, $\hat{\sigma}_l$ is the variance of the predicted expression of a gene in tissue $l$, and $\hat{\beta}_a$ is the estimated effect size of SNP $a$ from the GWAS.
In these TWAS methods, the genotype variances and covariances are always estimated using the Genotype-Tissue Expression project (GTEx v8) [@doi:10.1126/science.aaz1776] as the reference panel.
Since S-PrediXcan provides tissue-specific direction of effects (for instance, whether a higher or lower predicted expression of a gene confers more or less disease risk), we used the $z$-scores in our drug repurposing approach (described below).
%%% PARAGRAPH END %%%
""".strip()
in open(tmp_path / filename).read()
)
@pytest.mark.parametrize(
"model,filename",
[
(
DummyManuscriptRevisionModel(add_paragraph_marks=True),
"07.00.methods_already_revised.md",
),
# GPT3CompletionModel(None, None),
],
)
def test_revise_methods_with_equation_that_was_alrady_revised(
tmp_path, model, filename
):
"""
This test was added after getting an error in processing a file that was already
processed by the manuscript editor. The error was in ManuscriptEditor.prepare_paragraph
when a None sentence was added to the equation_sentences list.
"""
print(f"\n{str(tmp_path)}\n")
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "phenoplier",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file(filename, tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR / "phenoplier" / filename,
output_filepath=tmp_path / filename,
)
@pytest.mark.parametrize(
"model,filename",
[
(
DummyManuscriptRevisionModel(add_paragraph_marks=True),
"05.methods.md",
),
# GPT3CompletionModel(None, None),
],
)
def test_revise_methods_mutator_epistasis_paper(tmp_path, model, filename):
"""
This papers has several test cases:
- it ends with multiple blank lines
- it uses block of source code or bullet points, sometimes preceded by a paragraph with a colon (:)
"""
print(f"\n{str(tmp_path)}\n")
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "mutator-epistasis",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file(filename, tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR / "mutator-epistasis" / filename,
output_filepath=tmp_path / filename,
)
assert (
r"""
%%% PARAGRAPH START %%%
Briefly, we identified private single-nucleotide mutations in each BXD that were absent from all other BXDs, as well as from the C57BL/6J and DBA/2J parents.
We required each private variant to be meet the following criteria:
* genotyped as either homozygous or heterozygous for the alternate allele, with at least 90% of sequencing reads supporting the alternate allele
* supported by at least 10 sequencing reads
* Phred-scaled genotype quality of at least 20
* must not overlap regions of the genome annotated as segmental duplications or simple repeats in GRCm38/mm10
* must occur on a parental haplotype that was inherited by at least one other BXD at the same locus; these other BXDs must be homozygous for the reference allele at the variant site
%%% PARAGRAPH END %%%
""".strip()
in open(tmp_path / filename).read()
)
assert (
r"""
### Extracting mutation signatures
We used SigProfilerExtractor (v.1.1.21) [@PMID:30371878] to extract mutation signatures from the BXD mutation data.
After converting the BXD mutation data to the "matrix" input format expected by SigProfilerExtractor, we ran the `sigProfilerExtractor` method as follows:
```python
# install the mm10 mouse reference data
genInstall.install('mm10')
# run mutation signature extraction
sig.sigProfilerExtractor(
'matrix',
/path/to/output/directory,
/path/to/input/mutations,
maximum_signatures=10,
nmf_replicates=100,
opportunity_genome="mm10",
)
```
### Comparing mutation spectra between Mouse Genomes Project strains
""".strip()
in open(tmp_path / filename).read()
)
assert (
r"""
%%% PARAGRAPH START %%%
We investigated the region implicated by our aggregate mutation spectrum distance approach on chromosome 6 by subsetting the joint-genotyped BXD VCF file (European Nucleotide Archive accession PRJEB45429 [@url:https://www.ebi.ac.uk/ena/browser/view/PRJEB45429]) using `bcftools` [@PMID:33590861].
We defined the candidate interval surrounding the cosine distance peak on chromosome 6 as the 90% bootstrap confidence interval (extending from approximately 95 Mbp to 114 Mbp).
To predict the functional impacts of both single-nucleotide variants and indels on splicing, protein structure, etc., we annotated variants in the BXD VCF using the following `snpEff` [@PMID:22728672] command:
%%% PARAGRAPH END %%%
```
java -Xmx16g -jar /path/to/snpeff/jarfile GRCm38.75 /path/to/bxd/vcf > /path/to/uncompressed/output/vcf
```
""".strip()
in open(tmp_path / filename).read()
)
@pytest.mark.parametrize(
"model",
[
RandomManuscriptRevisionModel(),
# GPT3CompletionModel(None, None),
],
)
def test_revise_supplementary_material_with_tables_and_multiline_html_comments(
tmp_path, model
):
print(f"\n{str(tmp_path)}\n")
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "ccc",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file("20.00.supplementary_material.md", tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR / "ccc" / "20.00.supplementary_material.md",
output_filepath=tmp_path / "20.00.supplementary_material.md",
)
# make sure the "table paragraph" was exactly copied to the output file
assert (
r"""
| | **Interaction confidence** <!-- $colspan="7" --> | | | | | | |
|:------:|:-----:|:-----:|:-----:|:--------:|:-----:|:-----:|:-----:|
| | **Blood** <!-- $colspan="3" --> | | | **Predicted cell type** <!-- $colspan="4" --> | | | |
| **Gene** | **Min.** | **Avg.** | **Max.** | **Cell type** | **Min.** | **Avg.** | **Max.** |
| *IFNG* | 0.19 | 0.42 | 0.54 | Natural killer cell<!-- $rowspan="2" --> | 0.74 | 0.90 | 0.99 |
| *SDS* | 0.18 | 0.29 | 0.41 | 0.65 | 0.81 | 0.94<!-- $removenext="2" --> |
| <!-- $colspan="7" --> |||||||
| *JUN* | 0.26 | 0.68 | 0.97 | Mononuclear phagocyte<!-- $rowspan="2" --> | 0.36 | 0.73 | 0.94 |
| *APOC1* | 0.22 | 0.47 | 0.77 | 0.29 | 0.50 | 0.80<!-- $removenext="2" --> |
| <!-- $colspan="7" --> |||||||
| *ZDHHC12* | 0.05 | 0.07 | 0.10 | Macrophage<!-- $rowspan="2" --> | 0.03 | 0.12 | 0.33 |
| *CCL18* | 0.74 | 0.79 | 0.86 | 0.36 | 0.70 | 0.90<!-- $removenext="2" --> |
| <!-- $colspan="7" --> |||||||
| *RASSF2* | 0.69 | 0.77 | 0.90 | Leukocyte<!-- $rowspan="2" --> | 0.66 | 0.74 | 0.88 |
| *CYTIP* | 0.74 | 0.85 | 0.91 | 0.76 | 0.84 | 0.91<!-- $removenext="2" --> |
| <!-- $colspan="7" --> |||||||
| *MYOZ1* | 0.09 | 0.17 | 0.37 | Skeletal muscle<!-- $rowspan="2" --> | 0.11 | 0.11 | 0.12 |
| *TNNI2* | 0.10 | 0.22 | 0.44 | 0.10 | 0.11 | 0.12<!-- $removenext="2" --> |
| <!-- $colspan="7" --> |||||||
| *PYGM* | 0.02 | 0.04 | 0.14 | Skeletal muscle<!-- $rowspan="2" --> | 0.01 | 0.02 | 0.04 |
| *TPM2* | 0.05 | 0.56 | 0.80 | 0.01 | 0.28 | 0.47<!-- $removenext="2" --> |
Table: Network statistics of six gene pairs shown in Figure @fig:upsetplot_coefs b for blood and predicted cell types.
Only gene pairs present in GIANT models are listed.
For each gene in the pair (first column), the minimum, average and maximum interaction coefficients with the other genes in the network are shown.
{#tbl:giant:weights}
""".strip()
in open(tmp_path / "20.00.supplementary_material.md").read()
)
# make sure the "HTML comment paragraph" was exactly copied to the output file
assert (
r"""
<!-- ![
**Predicted tissue-specific networks from GIANT for six gene pairs prioritized by correlation coefficients.**
Gene pairs are from Figure @fig:upsetplot_coefs b.
A node represents a gene and an edge the probability that two genes are part of the same biological process in a specific cell type.
The cell type for each gene network was automatically predicted using [@doi:10.1101/gr.155697.113], and it is indicated at the top-right corner of each network.
A maximum of 15 genes are shown for each subfigure.
The GIANT web application automatically determined a minimum interaction confidence (edges' weights) to be shown.
All these analyses can be performed online using the following links:
*IFNG* - *SDS* [@url:https://hb.flatironinstitute.org/gene/10993+3458],
*JUN* - *APOC1* [@url:https://hb.flatironinstitute.org/gene/3725+341],
*ZDHHC12* - *CCL18* [@url:https://hb.flatironinstitute.org/gene/6362+84885],
*RASSF2* - *CYTIP* [@url:https://hb.flatironinstitute.org/gene/9770+9595],
*MYOZ1* - *TNNI2* [@url:https://hb.flatironinstitute.org/gene/58529+7136],
*PYGM* - *TPM2* [@url:https://hb.flatironinstitute.org/gene/5837+7169].
The GIANT web-server was accessed on April 4, 2022.
](images/coefs_comp/giant_networks/auto_selected_tissues/main.svg "GIANT network interaction"){#fig:giant_gene_pairs:pred_tissue width="100%"} -->
""".strip()
in open(tmp_path / "20.00.supplementary_material.md").read()
)
@pytest.mark.parametrize(
"model",
[
RandomManuscriptRevisionModel(),
# GPT3CompletionModel(None, None),
],
)
def test_revise_supplementary_material_from_phenoplier_with_many_tables(
tmp_path, model
):
print(f"\n{str(tmp_path)}\n")
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "phenoplier",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file("50.00.supplementary_material.md", tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR
/ "phenoplier"
/ "50.00.supplementary_material.md",
output_filepath=tmp_path / "50.00.supplementary_material.md",
)
# make sure the "table paragraph" was exactly copied to the output file
assert (
r"""
<!-- LV603:multiplier_pathways:start -->
| Pathway | AUC | FDR |
|:------------------------------------|:------|:---------|
| IRIS Neutrophil-Resting | 0.91 | 4.51e-35 |
| SVM Neutrophils | 0.98 | 1.43e-09 |
| PID IL8CXCR2 PATHWAY | 0.81 | 7.04e-03 |
| SIG PIP3 SIGNALING IN B LYMPHOCYTES | 0.77 | 1.95e-02 |
Table: Pathways aligned to LV603 from the MultiPLIER models. {#tbl:sup:multiplier_pathways:lv603}
<!-- LV603:multiplier_pathways:end -->
""".strip()
in open(tmp_path / "50.00.supplementary_material.md").read()
)
# make sure the "table paragraph" was exactly copied to the output file
assert (
r"""
<!-- LV603:phenomexcan_traits_assocs:start -->
| Trait description | Sample size | Cases | FDR |
|:------------------------------------------|:--------------|:--------|:---------------|
| Basophill percentage | 349,861 | | 1.19e‑10 |
| Basophill count | 349,856 | | 1.89e‑05 |
| Treatment/medication code: ispaghula husk | 361,141 | 327 | 1.36e‑02 |
Table: Significant trait associations of LV603 in PhenomeXcan. {#tbl:sup:phenomexcan_assocs:lv603}
<!-- LV603:phenomexcan_traits_assocs:end -->
""".strip()
in open(tmp_path / "50.00.supplementary_material.md").read()
)
# make sure the "table paragraph" was exactly copied to the output file
assert (
r"""
<!-- LV603:emerge_traits_assocs:start -->
| Phecode | Trait description | Sample size | Cases | FDR |
|:----------------------------|:--------------------|:--------------|:--------|:------|
| No significant associations | | | | |
Table: Significant trait associations of LV603 in eMERGE. {#tbl:sup:emerge_assocs:lv603}
<!-- LV603:emerge_traits_assocs:end -->
""".strip()
in open(tmp_path / "50.00.supplementary_material.md").read()
)
@pytest.mark.parametrize(
"model",
[
RandomManuscriptRevisionModel(),
# GPT3CompletionModel(None, None),
],
)
def test_revise_supplementary_material_from_phenoplier_with_many_tables_and_complex_html_comments(
tmp_path, model
):
print(f"\n{str(tmp_path)}\n")
me = ManuscriptEditor(
content_dir=MANUSCRIPTS_DIR / "phenoplier",
)
model.title = me.title
model.keywords = me.keywords
me.revise_file("50.01.supplementary_material.md", tmp_path, model)
_check_nonparagraph_lines_are_preserved(
input_filepath=MANUSCRIPTS_DIR
/ "phenoplier"
/ "50.01.supplementary_material.md",
output_filepath=tmp_path / "50.01.supplementary_material.md",
)
# make sure the "table paragraph" was exactly copied to the output file
assert (
r"""
<!-- LV603:multiplier_pathways:start
this is a more complex multiline html comment -->
| Pathway | AUC | FDR |
|:------------------------------------|:------|:---------|
| IRIS Neutrophil-Resting | 0.91 | 4.51e-35 |
| SVM Neutrophils | 0.98 | 1.43e-09 |
| PID IL8CXCR2 PATHWAY | 0.81 | 7.04e-03 |
| SIG PIP3 SIGNALING IN B LYMPHOCYTES | 0.77 | 1.95e-02 |
Table: Pathways aligned to LV603 from the MultiPLIER models. {#tbl:sup:multiplier_pathways:lv603}
<!-- LV603:multiplier_pathways:end -->
""".strip()
in open(tmp_path / "50.01.supplementary_material.md").read()
)
# make sure the "table paragraph" was exactly copied to the output file
assert (
r"""
<!-- LV603:phenomexcan_traits_assocs:start
and this html comments is multiline but
also has an empty line in the middle-->
| Trait description | Sample size | Cases | FDR |
|:------------------------------------------|:--------------|:--------|:---------------|
| Basophill percentage | 349,861 | | 1.19e‑10 |
| Basophill count | 349,856 | | 1.89e‑05 |
| Treatment/medication code: ispaghula husk | 361,141 | 327 | 1.36e‑02 |
Table: Significant trait associations of LV603 in PhenomeXcan. {#tbl:sup:phenomexcan_assocs:lv603}
<!-- LV603:phenomexcan_traits_assocs:end
-->
""".strip()
in open(tmp_path / "50.01.supplementary_material.md").read()
)
# make sure the "table paragraph" was exactly copied to the output file
assert (
r"""
<!--
and this html multiline comment has a space
LV603:emerge_traits_assocs:start
-->
| Phecode | Trait description | Sample size | Cases | FDR |
|:----------------------------|:--------------------|:--------------|:--------|:------|
| No significant associations | | | | |
Table: Significant trait associations of LV603 in eMERGE. {#tbl:sup:emerge_assocs:lv603}
<!-- LV603:emerge_traits_assocs:end -->
""".strip()
in open(tmp_path / "50.01.supplementary_material.md").read()
)
@pytest.mark.parametrize(
"model",
[
VerboseManuscriptRevisionModel("Revised:\n"),
VerboseManuscriptRevisionModel("Revised:\n\n"),
VerboseManuscriptRevisionModel(
"We revised the paragraph from the Methods section of the academic paper titled 'Projecting genetic associations through gene expression patterns highlights disease etiology and drug mechanisms' as follows:\n\n"
),
],
)
def test_revise_section_where_model_says_what_it_is_doing(model):
# sometimes, the GPT model returns an initial paragraph saying "We revised"
# or "Revised:". Here I make sure the editor does not include this paragraph.
orig_paragraph_text = r"""
This is a paragraph that was not revised.
And this is the second line of the same paragraph.
And finally, a third sentence so we have more than 2.
And finally, a third sentence so we have more than 2.
And finally, a third sentence so we have more than 2.
And finally, a third sentence so we have more than 2.
"""
# make sure I have the minimum number of words to pass the "too short" check
assert len(orig_paragraph_text.split()) > 60, len(orig_paragraph_text.split())
paragraph = orig_paragraph_text.strip().split("\n")
paragraph = [sentence.strip() for sentence in paragraph]
assert len(paragraph) == 6
model.title = "Projecting genetic associations through gene expression patterns highlights disease etiology and drug mechanisms"
model.keywords = [
"genetic studies",
"functional genomics",
"gene co-expression",
"therapeutic targets",
"drug repurposing",
"clustering of complex traits",
]
paragraph_text, paragraph_revised = ManuscriptEditor.revise_and_write_paragraph(
paragraph,
model,
"methods",
)
assert paragraph_text is not None
assert paragraph_revised is not None
assert isinstance(paragraph_revised, str)
# the paragraph does not contain the "Revised:" or "We revised" initial
assert orig_paragraph_text.strip() == paragraph_revised.strip()
@pytest.mark.parametrize(