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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
|
%global _empty_manifest_terminate_build 0
Name: python-hicstuff
Version: 3.1.5
Release: 1
Summary: General purpose stuff to generate and handle Hi-C data in its simplest form.
License: BSD-3-Clause
URL: https://github.com/koszullab/hicstuff
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/db/2f/555171aae026916e47800901464768573d8ccd5f2a662ba60d3460676a1c/hicstuff-3.1.5.tar.gz
BuildArch: noarch
Requires: python3-numpy
Requires: python3-scipy
Requires: python3-pandas
Requires: python3-matplotlib
Requires: python3-docopt
Requires: python3-biopython
Requires: python3-requests
Requires: python3-sklearn
Requires: python3-pysam
Requires: python3-pyfastx
Requires: python3-cooler
Requires: python3-mappy
%description
# hicstuff
[](https://badge.fury.io/py/hicstuff)

[](https://github.com/koszullab/hicstuff/actions/workflows/build.yml)
[](https://hub.docker.com/r/koszullab/hicstuff)
[](https://doi.org/10.5281/zenodo.2620608)
[](https://codecov.io/gh/koszullab/hicstuff)
[](https://hicstuff.readthedocs.io)
[](https://mybinder.org/v2/gh/koszullab/hicstuff/master?filepath=doc%2Fnotebooks%2Fdemo_api.ipynb)
[](https://github.com/ambv/black)
A lightweight library that generates and handles Hi-C contact maps in either cooler-compatible 2Dbedgraph or [instaGRAAL](https://github.com/koszullab/instaGRAAL) format. It is essentially a merge of the [yahcp](https://github.com/baudrly/yahcp) pipeline, the [hicstuff](https://github.com/baudrly/hicstuff) library and extra features illustrated in the [3C tutorial](https://github.com/axelcournac/3C_tutorial) and the [DADE pipeline](https://github.com/scovit/dade), all packaged together for extra convenience.
The goal is to make generation and manipulation of Hi-C matrices as simple as possible and work for any organism.
## Table of contents
* [Installation](#Installation)
* [Usage](#Usage)
* [Full pipeline](#Full-pipeline)
* [Individual components](#Individual-components)
* [Library](#Library)
* [Connecting the modules](#Connecting-the-modules)
* [File formats](#File-formats)
* [Contributing](#Contributing)
## Installation
To install a stable version:
```sh
pip3 install -U hicstuff
```
or, for the latest development version:
```sh
pip3 install -e git+https://github.com/koszullab/hicstuff.git@master#egg=hicstuff
```
### External dependencies
Bowtie2 and/or minimap2 as well as samtools are required for the `pipeline` utility.
You can install them via the conda package manager:
```bash
conda install -c bioconda minimap2 bowtie2 samtools
```
Alternatively, on ubuntu you can also install them along with additional dependencies through APT:
```bash
apt-get install samtools bowtie2 minimap2 libbz2-dev liblzma-dev
```
### Docker installation
A pre-built docker image is available on dockerhub and can be ran using:
```bash
docker run koszullab/hicstuff
```
## Usage
The hicstuff command line interface is composed of multiple subcommands. You can always get a summary of all available commands by running:
```bash
hicstuff --help
Simple Hi-C pipeline for generating and manipulating contact matrices.
usage:
hicstuff [-hv] <command> [<args>...]
options:
-h, --help shows the help
-v, --version shows the version
The subcommands are:
digest Digest genome into a list of fragments.
distancelaw Analyse and plot distance law.
filter Filters Hi-C pairs to exclude spurious events.
iteralign Iteratively aligns reads to a reference genome.
pipeline Hi-C pipeline to generate contact matrix from fastq files.
rebin Bin the matrix and regenerate files accordingly.
subsample Bootstrap subsampling of contacts from a Hi-C map.
view Visualize a Hi-C matrix.
```
### Full pipeline
All components of the pipeline can be run at once using the `hicstuff pipeline` command. This allows to generate a contact matrix from reads in a single command. By default, the output is in GRAAL compatible COO sparse matrix format, but it can be a 2D bedgraph or cool file instead using the `--matfmt` option. More detailed documentation can be found on the readthedocs website: https://hicstuff.readthedocs.io/en/latest/index.html
usage:
pipeline [--quality-min=INT] [--size=INT] [--no-cleanup] [--start-stage=STAGE]
[--threads=INT] [--aligner=bowtie2] [--matfmt=FMT] [--prefix=PREFIX]
[--tmpdir=DIR] [--iterative] [--outdir=DIR] [--filter] [--enzyme=ENZ]
[--plot] [--circular] [--distance-law] [--duplicates] [--read-len=INT]
[--centromeres=FILE] [--remove-centromeres=INT] --genome=FILE <input1> [<input2>]
arguments:
input1: Forward fastq file, if start_stage is "fastq", sam
file for aligned forward reads if start_stage is
"bam", or a .pairs file if start_stage is "pairs".
input2: Reverse fastq file, if start_stage is "fastq", sam
file for aligned reverse reads if start_stage is
"bam", or nothing if start_stage is "pairs".
For example, to run the pipeline with minimap2 using 8 threads and generate a matrix in instagraal format in the directory `out`:
```
hicstuff pipeline -t 8 -a minimap2 -e DpnII -o out/ -g genome.fa reads_for.fq reads_rev.fq
```
If you have already aligned your reads, hicstuff pipeline can also take bam files as input. For example,
to generate a matrix in cool format with a fixed bin size of 5kb:
```
# Note the bam files have to be name-sorted, this can be done using samtools
samtools sort -n aligned_for.bam -o namesorted_for.bam
samtools sort -n aligned_rev.bam -o namesorted_rev.bam
hicstuff pipeline -S bam -e 5000 -M cool -o out/ -g genome.fa namesorted_for.bam namesorted_rev.bam
```
The pipeline can also be run from python, using the `hicstuff.pipeline` submodule. For example, this would run the pipeline with bowtie2 (default) using iterative alignment and keep all intermediate files. For more examples using the API, see the [API demo](https://hicstuff.readthedocs.io/en/latest/notebooks/demo_api.html)
```python
from hicstuff import pipeline as hpi
hpi.full_pipeline(
'genome.fa',
'end1.fq',
'end2.fq',
no_cleanup=True
iterative=True
out_dir='out',
enzyme="DpnII")
```
The general steps of the pipeline are as follows:

### Individual components
For more advanced usage, different scripts can be used independently on the command line to perform individual parts of the pipeline. This readme contains quick descriptions and example usages. To obtain detailed instructions on any subcommand, one can use `hicstuff <subcommand> --help`.
#### Iterative alignment
Truncate reads from a fastq file to 20 basepairs and iteratively extend and re-align the unmapped reads to optimize the proportion of uniquely aligned reads in a 3C library.
usage:
iteralign [--aligner=bowtie2] [--threads=1] [--min_len=20]
[--tempdir DIR] --out_sam=FILE --genome=FILE <reads.fq>
#### Digestion of the genome
Digests a fasta file into fragments based on a restriction enzyme or a
fixed chunk size. Generates two output files into the target directory
named "info_contigs.txt" and "fragments_list.txt"
usage:
digest [--plot] [--figdir=FILE] [--circular] [--size=INT]
[--outdir=DIR] --enzyme=ENZ <fasta>
For example, to digest the yeast genome with MaeII and HinfI and show histogram of fragment lengths:
`hicstuff digest --plot --outdir output_dir --enzyme MaeII,HinfI Sc_ref.fa`
#### Filtering of 3C events
Filters spurious 3C events such as loops and uncuts from the library based on a minimum distance threshold automatically estimated from the library by default. Can also plot 3C library statistics. This module takes a pairs file with 9 columns as input (readID, chr1, pos1, chr2, pos2, strand1, strand2, frag1, frag2) and filters it.
usage:
filter [--interactive | --thresholds INT-INT] [--plot]
[--figdir FILE] <input.pairs> <output.pairs>
#### Viewing the contact map
Visualize a Hi-C matrix file as a heatmap of contact frequencies. Allows to tune visualisation by binning and normalizing the matrix, and to save the output image to disk. If no output is specified, the output is displayed interactively. If two contact maps are provided, the log ratio of the first divided by the second will be shown.
usage:
view [--binning=1] [--despeckle] [--frags FILE] [--trim INT] [--n-mad FLOAT]
[--normalize] [--max=99] [--output=IMG] [--cmap=CMAP] [--dpi=INT]
[--transform=FUN] [--circular] [--region=STR] <contact_map> [<contact_map2>]
arguments:
contact_map Sparse contact matrix in bg2, cool or graal format
contact_map2 Sparse contact matrix in bg2, cool or graal format,
if given, the log ratio of contact_map/contact_map2
will be shown.
For example, to view a 1Mb region of chromosome 1 from a full genome Hi-C matrix rebinned at 10kb:
```sh
hicstuff view --normalize --binning 10kb --region chr1:10,000,000-11,000,000 --frags fragments_list.txt contact_map.tsv
```
### Library
All components of the hicstuff program can be used as python modules. See the documentation on [reathedocs](https://hicstuff.readthedocs.io). The expected contact map format for the library is a simple CSV file, and the objects handled by the library are simple ```numpy``` arrays. The various submodules of hicstuff contain various utilities.
```python
import hicstuff.digest # Functions to work with restriction fragments
import hicstuff.iteralign # Functions related to iterative alignment
import hicstuff.hicstuff # Contains utilities to modify and operate on contact maps as numpy arrays
import hicstuff.filter # Functions for filtering 3C events by type (uncut, loop)
import hicstuff.view # Utilities to visualise contact maps
import hicstuff.io # Reading and writing hicstuff files
import hicstuff.pipeline # Generation and processing of files to generate matrices.
```
### Connecting the modules
All the steps described here are handled automatically when running the `hicstuff pipeline`. But if you want to connect the different modules manually, the intermediate input and output files can be processed using some python scripting.
#### Aligning the reads
You can generate SAM files independently using your favorite read mapping software, use the command line utility `hicstuff iteralign`, or use the helper function `align_reads` in the submodule `hicstuff.pipeline`. For example, to perform iterative alignment using minimap2 (instead of bowtie2 by default):
**Using the python function:**
```python
from hicstuff import pipeline as hpi
hpi.align_reads("end1.fastq", "genome.fasta", "end1.bam", iterative=True, minimap2=True)
```
**Using the command line tool:**
```bash
hicstuff iteralign --minimap2 --iterative -f genome.fasta -o end1.sam end1.fastq
```
#### Extracting contacts from the alignment
The output from `hicstuff iteralign` is a SAM file. In order to retrieve Hi-C pairs, you need to run iteralign separately on the two fastq files and process the resulting alignment files into a name-sorted BAM file as follows using the `pipeline` submodules of hicstuff.
```python
from hicstuff import pipeline as hpi
import pysam as ps
# Sort alignments by read names and get into BAM format
ps.sort("-n", "-O", "BAM", "-o", "end1.bam.sorted", "end1.sam")
ps.sort("-n", "-O", "BAM", "-o", "end2.bam.sorted", "end2.sam")
# Combine BAM files
hpi.bam2pairs("end1.sorted.bam", "end2.sorted.bam", "output.pairs", "info_contigs.txt", min_qual=30)
```
This will generate a "pairs" file containing all read pairs where both reads have been aligned with a mapping quality of at least 30.
#### Attributing each read to a restriction fragment
To build a a contact matrix, we need to attribute each read to a fragment in the genome. This is done under the hood by performing a binary search for each read position against the list of restriction sites in the genome.
```python
from hicstuff import digest as hcd
from Bio import SeqIO
# Build a list of restriction sites for each chromosome
restrict_table = {}
for record in SeqIO.parse("genome.fasta", "fasta"):
# Get chromosome restriction table
restrict_table[record.id] = hcd.get_restriction_table(
record.seq, enzyme, circular=circular
)
# Add fragment index to pairs (readID, chr1, pos1, chr2,
# pos2, strand1, strand2, frag1, frag2)
hcd.attribute_fragments("output.pairs", "output_indexed.pairs", restrict_table)
```
#### Filtering pairs
The resulting pairs file can then be filtered, either in the command line using the `hicstuff filter` command, or in python using the `hicstuff.filter` submodule. Otherwise, the matrix can be built directly from the unfiltered pairs.
**Filtering on the command line:**
```bash
hicstuff filter output_indexed.pairs output_filtered.pairs
```
**Filtering in python:**
```python
from hicstuff import filter as hcf
uncut_thr, loop_thr = hcf.get_thresholds("output_indexed.pairs")
hcf.filter_events("output_indexed.pairs", "output_filtered.pairs", uncut_thr, loop_thr)
```
Note that both the command and the python function have various options to generate figure or tweak the filtering thresholds. These options can be displayed using `hicstuff filter -h`
#### Matrix generation
A Hi-C sparse contact matrix can then be generated using the python submodule `hicstuff.pipeline`. The matrix can be generated in GRAAL-compatible COO format, bedgraph2 or cool format.
```python
from hicstuff import pipeline as hpi
n_frags = sum(1 for line in open(fragments_list, "r")) - 1
hpi.pairs2matrix("output_filtered.pairs", "abs_fragments_contacts_weighted.txt", 'fragments_list.txt', mat_fmt="GRAAL")
```
### File formats
* pairs files: This format is used for all intermediate files in the pipeline and is also used by `hicstuff filter`. It is a tab-separated format holding informations about Hi-C pairs. It has an [official specification](https://github.com/4dn-dcic/pairix/blob/master/pairs_format_specification.md) defined by the 4D Nucleome data coordination and integration center.
* 2D bedgraph: This is an optional output format of `hicstuff pipeline` for the sparse matrix. It has two fragment per line, and the number of times they are found together. It has the following fields: **chr1, start1, end1, chr2, start2, end2, occurences**
- Those files can be [loaded by cooler](https://cooler.readthedocs.io/en/latest/cli.html?highlight=load#cooler-load) using `cooler load -f bg2 <chrom.sizes>:<binsize> in.bg2.gz out.cool` where chrom.sizes is a tab delimited file with chromosome names and length on each line, and binsize is the size of bins in the matrix.
* GRAAL sparse matrix: This is a simple tab-separated file with 3 columns: **frag1, frag2, contacts**. The id columns correspond to the absolute id of the restriction fragments (0-indexed). The first row is a header containing the number of rows, number of columns and number of nonzero entries in the matrix. Example:
```
564 564 6978
0 0 3
1 2 4
1 3 3
```
* fragments_list.txt: This tab separated file provides information about restriction fragments positions, size and GC content. Note the coordinates are 0 point basepairs, unlike the pairs format, which has 1 point basepairs. Example:
- id: 1 based restriction fragment index within chromosome.
- chrom: Chromosome identifier. Order should be the same as in info_contigs.txt or pairs files.
- start_pos: 0-based start of fragment, in base pairs.
- end_pos: 0-based end of fragment, in base pairs.
- size: Size of fragment, in base pairs.
- gc_content: Proportion of G and C nucleotide in the fragment.
```
id chrom start_pos end_pos size gc_content
1 seq1 0 21 21 0.5238095238095238
2 seq1 21 80 59 0.576271186440678
3 seq1 80 328 248 0.5201612903225806
```
* info_contigs.txt: This tab separated file gives information on contigs, such as number of restriction fragments and size. Example:
- contig: Chromosome identified. Order should be the same in pairs files or fragments_list.txt.
- length: Chromosome length, in base pairs.
- n_frags: Number of restriction fragments in chromosome.
- cumul_length: Cumulative length of previous chromosome, in base pairs.
```
contig length n_frags cumul_length
seq1 60000 409 0
seq2 20000 155 409
```
### Contributing
All contributions are welcome, in the form of bug reports, suggestions, documentation or pull requests.
We use the [numpy standard](https://numpydoc.readthedocs.io/en/latest/format.html) for docstrings when documenting functions.
The code formatting standard we use is [black](https://github.com/psf/black), with --line-length=79 to follow PEP8 recommendations. We use `pytest` with the `pytest-doctest` and `pytest-pylint` plugins as our testing framework. Ideally, new functions should have associated unit tests, placed in the `tests` folder.
To test the code, you can run:
```bash
pytest --doctest-modules --pylint --pylint-error-types=EF --pylint-rcfile=.pylintrc hicstuff tests
```
### Citation
Please cite hicstuff using the official DOI as follows:
Cyril Matthey-Doret, Lyam Baudry, Amaury Bignaud, Axel Cournac, Remi-Montagne, Nadège Guiglielmoni, Théo Foutel Rodier and Vittore F. Scolari. 2020. hicstuff: Simple library/pipeline to generate and handle Hi-C data . Zenodo. http://doi.org/10.5281/zenodo.4066363
Bibtex entry:
```
@software{cyril_matthey_doret_2020_4066351,
author = {Cyril Matthey-Doret and
Lyam Baudry and
Amaury Bignaud and
Axel Cournac and
Remi-Montagne and
Nadège Guiglielmoni and
Théo Foutel-Rodier and
Vittore F. Scolari},
title = {hicstuff: Simple library/pipeline to generate and handle Hi-C data },
month = oct,
year = 2020,
publisher = {Zenodo},
version = {v2.3.1},
doi = {10.5281/zenodo.4066351},
url = {http://doi.org/10.5281/zenodo.4066363}
}
```
%package -n python3-hicstuff
Summary: General purpose stuff to generate and handle Hi-C data in its simplest form.
Provides: python-hicstuff
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-hicstuff
# hicstuff
[](https://badge.fury.io/py/hicstuff)

[](https://github.com/koszullab/hicstuff/actions/workflows/build.yml)
[](https://hub.docker.com/r/koszullab/hicstuff)
[](https://doi.org/10.5281/zenodo.2620608)
[](https://codecov.io/gh/koszullab/hicstuff)
[](https://hicstuff.readthedocs.io)
[](https://mybinder.org/v2/gh/koszullab/hicstuff/master?filepath=doc%2Fnotebooks%2Fdemo_api.ipynb)
[](https://github.com/ambv/black)
A lightweight library that generates and handles Hi-C contact maps in either cooler-compatible 2Dbedgraph or [instaGRAAL](https://github.com/koszullab/instaGRAAL) format. It is essentially a merge of the [yahcp](https://github.com/baudrly/yahcp) pipeline, the [hicstuff](https://github.com/baudrly/hicstuff) library and extra features illustrated in the [3C tutorial](https://github.com/axelcournac/3C_tutorial) and the [DADE pipeline](https://github.com/scovit/dade), all packaged together for extra convenience.
The goal is to make generation and manipulation of Hi-C matrices as simple as possible and work for any organism.
## Table of contents
* [Installation](#Installation)
* [Usage](#Usage)
* [Full pipeline](#Full-pipeline)
* [Individual components](#Individual-components)
* [Library](#Library)
* [Connecting the modules](#Connecting-the-modules)
* [File formats](#File-formats)
* [Contributing](#Contributing)
## Installation
To install a stable version:
```sh
pip3 install -U hicstuff
```
or, for the latest development version:
```sh
pip3 install -e git+https://github.com/koszullab/hicstuff.git@master#egg=hicstuff
```
### External dependencies
Bowtie2 and/or minimap2 as well as samtools are required for the `pipeline` utility.
You can install them via the conda package manager:
```bash
conda install -c bioconda minimap2 bowtie2 samtools
```
Alternatively, on ubuntu you can also install them along with additional dependencies through APT:
```bash
apt-get install samtools bowtie2 minimap2 libbz2-dev liblzma-dev
```
### Docker installation
A pre-built docker image is available on dockerhub and can be ran using:
```bash
docker run koszullab/hicstuff
```
## Usage
The hicstuff command line interface is composed of multiple subcommands. You can always get a summary of all available commands by running:
```bash
hicstuff --help
Simple Hi-C pipeline for generating and manipulating contact matrices.
usage:
hicstuff [-hv] <command> [<args>...]
options:
-h, --help shows the help
-v, --version shows the version
The subcommands are:
digest Digest genome into a list of fragments.
distancelaw Analyse and plot distance law.
filter Filters Hi-C pairs to exclude spurious events.
iteralign Iteratively aligns reads to a reference genome.
pipeline Hi-C pipeline to generate contact matrix from fastq files.
rebin Bin the matrix and regenerate files accordingly.
subsample Bootstrap subsampling of contacts from a Hi-C map.
view Visualize a Hi-C matrix.
```
### Full pipeline
All components of the pipeline can be run at once using the `hicstuff pipeline` command. This allows to generate a contact matrix from reads in a single command. By default, the output is in GRAAL compatible COO sparse matrix format, but it can be a 2D bedgraph or cool file instead using the `--matfmt` option. More detailed documentation can be found on the readthedocs website: https://hicstuff.readthedocs.io/en/latest/index.html
usage:
pipeline [--quality-min=INT] [--size=INT] [--no-cleanup] [--start-stage=STAGE]
[--threads=INT] [--aligner=bowtie2] [--matfmt=FMT] [--prefix=PREFIX]
[--tmpdir=DIR] [--iterative] [--outdir=DIR] [--filter] [--enzyme=ENZ]
[--plot] [--circular] [--distance-law] [--duplicates] [--read-len=INT]
[--centromeres=FILE] [--remove-centromeres=INT] --genome=FILE <input1> [<input2>]
arguments:
input1: Forward fastq file, if start_stage is "fastq", sam
file for aligned forward reads if start_stage is
"bam", or a .pairs file if start_stage is "pairs".
input2: Reverse fastq file, if start_stage is "fastq", sam
file for aligned reverse reads if start_stage is
"bam", or nothing if start_stage is "pairs".
For example, to run the pipeline with minimap2 using 8 threads and generate a matrix in instagraal format in the directory `out`:
```
hicstuff pipeline -t 8 -a minimap2 -e DpnII -o out/ -g genome.fa reads_for.fq reads_rev.fq
```
If you have already aligned your reads, hicstuff pipeline can also take bam files as input. For example,
to generate a matrix in cool format with a fixed bin size of 5kb:
```
# Note the bam files have to be name-sorted, this can be done using samtools
samtools sort -n aligned_for.bam -o namesorted_for.bam
samtools sort -n aligned_rev.bam -o namesorted_rev.bam
hicstuff pipeline -S bam -e 5000 -M cool -o out/ -g genome.fa namesorted_for.bam namesorted_rev.bam
```
The pipeline can also be run from python, using the `hicstuff.pipeline` submodule. For example, this would run the pipeline with bowtie2 (default) using iterative alignment and keep all intermediate files. For more examples using the API, see the [API demo](https://hicstuff.readthedocs.io/en/latest/notebooks/demo_api.html)
```python
from hicstuff import pipeline as hpi
hpi.full_pipeline(
'genome.fa',
'end1.fq',
'end2.fq',
no_cleanup=True
iterative=True
out_dir='out',
enzyme="DpnII")
```
The general steps of the pipeline are as follows:

### Individual components
For more advanced usage, different scripts can be used independently on the command line to perform individual parts of the pipeline. This readme contains quick descriptions and example usages. To obtain detailed instructions on any subcommand, one can use `hicstuff <subcommand> --help`.
#### Iterative alignment
Truncate reads from a fastq file to 20 basepairs and iteratively extend and re-align the unmapped reads to optimize the proportion of uniquely aligned reads in a 3C library.
usage:
iteralign [--aligner=bowtie2] [--threads=1] [--min_len=20]
[--tempdir DIR] --out_sam=FILE --genome=FILE <reads.fq>
#### Digestion of the genome
Digests a fasta file into fragments based on a restriction enzyme or a
fixed chunk size. Generates two output files into the target directory
named "info_contigs.txt" and "fragments_list.txt"
usage:
digest [--plot] [--figdir=FILE] [--circular] [--size=INT]
[--outdir=DIR] --enzyme=ENZ <fasta>
For example, to digest the yeast genome with MaeII and HinfI and show histogram of fragment lengths:
`hicstuff digest --plot --outdir output_dir --enzyme MaeII,HinfI Sc_ref.fa`
#### Filtering of 3C events
Filters spurious 3C events such as loops and uncuts from the library based on a minimum distance threshold automatically estimated from the library by default. Can also plot 3C library statistics. This module takes a pairs file with 9 columns as input (readID, chr1, pos1, chr2, pos2, strand1, strand2, frag1, frag2) and filters it.
usage:
filter [--interactive | --thresholds INT-INT] [--plot]
[--figdir FILE] <input.pairs> <output.pairs>
#### Viewing the contact map
Visualize a Hi-C matrix file as a heatmap of contact frequencies. Allows to tune visualisation by binning and normalizing the matrix, and to save the output image to disk. If no output is specified, the output is displayed interactively. If two contact maps are provided, the log ratio of the first divided by the second will be shown.
usage:
view [--binning=1] [--despeckle] [--frags FILE] [--trim INT] [--n-mad FLOAT]
[--normalize] [--max=99] [--output=IMG] [--cmap=CMAP] [--dpi=INT]
[--transform=FUN] [--circular] [--region=STR] <contact_map> [<contact_map2>]
arguments:
contact_map Sparse contact matrix in bg2, cool or graal format
contact_map2 Sparse contact matrix in bg2, cool or graal format,
if given, the log ratio of contact_map/contact_map2
will be shown.
For example, to view a 1Mb region of chromosome 1 from a full genome Hi-C matrix rebinned at 10kb:
```sh
hicstuff view --normalize --binning 10kb --region chr1:10,000,000-11,000,000 --frags fragments_list.txt contact_map.tsv
```
### Library
All components of the hicstuff program can be used as python modules. See the documentation on [reathedocs](https://hicstuff.readthedocs.io). The expected contact map format for the library is a simple CSV file, and the objects handled by the library are simple ```numpy``` arrays. The various submodules of hicstuff contain various utilities.
```python
import hicstuff.digest # Functions to work with restriction fragments
import hicstuff.iteralign # Functions related to iterative alignment
import hicstuff.hicstuff # Contains utilities to modify and operate on contact maps as numpy arrays
import hicstuff.filter # Functions for filtering 3C events by type (uncut, loop)
import hicstuff.view # Utilities to visualise contact maps
import hicstuff.io # Reading and writing hicstuff files
import hicstuff.pipeline # Generation and processing of files to generate matrices.
```
### Connecting the modules
All the steps described here are handled automatically when running the `hicstuff pipeline`. But if you want to connect the different modules manually, the intermediate input and output files can be processed using some python scripting.
#### Aligning the reads
You can generate SAM files independently using your favorite read mapping software, use the command line utility `hicstuff iteralign`, or use the helper function `align_reads` in the submodule `hicstuff.pipeline`. For example, to perform iterative alignment using minimap2 (instead of bowtie2 by default):
**Using the python function:**
```python
from hicstuff import pipeline as hpi
hpi.align_reads("end1.fastq", "genome.fasta", "end1.bam", iterative=True, minimap2=True)
```
**Using the command line tool:**
```bash
hicstuff iteralign --minimap2 --iterative -f genome.fasta -o end1.sam end1.fastq
```
#### Extracting contacts from the alignment
The output from `hicstuff iteralign` is a SAM file. In order to retrieve Hi-C pairs, you need to run iteralign separately on the two fastq files and process the resulting alignment files into a name-sorted BAM file as follows using the `pipeline` submodules of hicstuff.
```python
from hicstuff import pipeline as hpi
import pysam as ps
# Sort alignments by read names and get into BAM format
ps.sort("-n", "-O", "BAM", "-o", "end1.bam.sorted", "end1.sam")
ps.sort("-n", "-O", "BAM", "-o", "end2.bam.sorted", "end2.sam")
# Combine BAM files
hpi.bam2pairs("end1.sorted.bam", "end2.sorted.bam", "output.pairs", "info_contigs.txt", min_qual=30)
```
This will generate a "pairs" file containing all read pairs where both reads have been aligned with a mapping quality of at least 30.
#### Attributing each read to a restriction fragment
To build a a contact matrix, we need to attribute each read to a fragment in the genome. This is done under the hood by performing a binary search for each read position against the list of restriction sites in the genome.
```python
from hicstuff import digest as hcd
from Bio import SeqIO
# Build a list of restriction sites for each chromosome
restrict_table = {}
for record in SeqIO.parse("genome.fasta", "fasta"):
# Get chromosome restriction table
restrict_table[record.id] = hcd.get_restriction_table(
record.seq, enzyme, circular=circular
)
# Add fragment index to pairs (readID, chr1, pos1, chr2,
# pos2, strand1, strand2, frag1, frag2)
hcd.attribute_fragments("output.pairs", "output_indexed.pairs", restrict_table)
```
#### Filtering pairs
The resulting pairs file can then be filtered, either in the command line using the `hicstuff filter` command, or in python using the `hicstuff.filter` submodule. Otherwise, the matrix can be built directly from the unfiltered pairs.
**Filtering on the command line:**
```bash
hicstuff filter output_indexed.pairs output_filtered.pairs
```
**Filtering in python:**
```python
from hicstuff import filter as hcf
uncut_thr, loop_thr = hcf.get_thresholds("output_indexed.pairs")
hcf.filter_events("output_indexed.pairs", "output_filtered.pairs", uncut_thr, loop_thr)
```
Note that both the command and the python function have various options to generate figure or tweak the filtering thresholds. These options can be displayed using `hicstuff filter -h`
#### Matrix generation
A Hi-C sparse contact matrix can then be generated using the python submodule `hicstuff.pipeline`. The matrix can be generated in GRAAL-compatible COO format, bedgraph2 or cool format.
```python
from hicstuff import pipeline as hpi
n_frags = sum(1 for line in open(fragments_list, "r")) - 1
hpi.pairs2matrix("output_filtered.pairs", "abs_fragments_contacts_weighted.txt", 'fragments_list.txt', mat_fmt="GRAAL")
```
### File formats
* pairs files: This format is used for all intermediate files in the pipeline and is also used by `hicstuff filter`. It is a tab-separated format holding informations about Hi-C pairs. It has an [official specification](https://github.com/4dn-dcic/pairix/blob/master/pairs_format_specification.md) defined by the 4D Nucleome data coordination and integration center.
* 2D bedgraph: This is an optional output format of `hicstuff pipeline` for the sparse matrix. It has two fragment per line, and the number of times they are found together. It has the following fields: **chr1, start1, end1, chr2, start2, end2, occurences**
- Those files can be [loaded by cooler](https://cooler.readthedocs.io/en/latest/cli.html?highlight=load#cooler-load) using `cooler load -f bg2 <chrom.sizes>:<binsize> in.bg2.gz out.cool` where chrom.sizes is a tab delimited file with chromosome names and length on each line, and binsize is the size of bins in the matrix.
* GRAAL sparse matrix: This is a simple tab-separated file with 3 columns: **frag1, frag2, contacts**. The id columns correspond to the absolute id of the restriction fragments (0-indexed). The first row is a header containing the number of rows, number of columns and number of nonzero entries in the matrix. Example:
```
564 564 6978
0 0 3
1 2 4
1 3 3
```
* fragments_list.txt: This tab separated file provides information about restriction fragments positions, size and GC content. Note the coordinates are 0 point basepairs, unlike the pairs format, which has 1 point basepairs. Example:
- id: 1 based restriction fragment index within chromosome.
- chrom: Chromosome identifier. Order should be the same as in info_contigs.txt or pairs files.
- start_pos: 0-based start of fragment, in base pairs.
- end_pos: 0-based end of fragment, in base pairs.
- size: Size of fragment, in base pairs.
- gc_content: Proportion of G and C nucleotide in the fragment.
```
id chrom start_pos end_pos size gc_content
1 seq1 0 21 21 0.5238095238095238
2 seq1 21 80 59 0.576271186440678
3 seq1 80 328 248 0.5201612903225806
```
* info_contigs.txt: This tab separated file gives information on contigs, such as number of restriction fragments and size. Example:
- contig: Chromosome identified. Order should be the same in pairs files or fragments_list.txt.
- length: Chromosome length, in base pairs.
- n_frags: Number of restriction fragments in chromosome.
- cumul_length: Cumulative length of previous chromosome, in base pairs.
```
contig length n_frags cumul_length
seq1 60000 409 0
seq2 20000 155 409
```
### Contributing
All contributions are welcome, in the form of bug reports, suggestions, documentation or pull requests.
We use the [numpy standard](https://numpydoc.readthedocs.io/en/latest/format.html) for docstrings when documenting functions.
The code formatting standard we use is [black](https://github.com/psf/black), with --line-length=79 to follow PEP8 recommendations. We use `pytest` with the `pytest-doctest` and `pytest-pylint` plugins as our testing framework. Ideally, new functions should have associated unit tests, placed in the `tests` folder.
To test the code, you can run:
```bash
pytest --doctest-modules --pylint --pylint-error-types=EF --pylint-rcfile=.pylintrc hicstuff tests
```
### Citation
Please cite hicstuff using the official DOI as follows:
Cyril Matthey-Doret, Lyam Baudry, Amaury Bignaud, Axel Cournac, Remi-Montagne, Nadège Guiglielmoni, Théo Foutel Rodier and Vittore F. Scolari. 2020. hicstuff: Simple library/pipeline to generate and handle Hi-C data . Zenodo. http://doi.org/10.5281/zenodo.4066363
Bibtex entry:
```
@software{cyril_matthey_doret_2020_4066351,
author = {Cyril Matthey-Doret and
Lyam Baudry and
Amaury Bignaud and
Axel Cournac and
Remi-Montagne and
Nadège Guiglielmoni and
Théo Foutel-Rodier and
Vittore F. Scolari},
title = {hicstuff: Simple library/pipeline to generate and handle Hi-C data },
month = oct,
year = 2020,
publisher = {Zenodo},
version = {v2.3.1},
doi = {10.5281/zenodo.4066351},
url = {http://doi.org/10.5281/zenodo.4066363}
}
```
%package help
Summary: Development documents and examples for hicstuff
Provides: python3-hicstuff-doc
%description help
# hicstuff
[](https://badge.fury.io/py/hicstuff)

[](https://github.com/koszullab/hicstuff/actions/workflows/build.yml)
[](https://hub.docker.com/r/koszullab/hicstuff)
[](https://doi.org/10.5281/zenodo.2620608)
[](https://codecov.io/gh/koszullab/hicstuff)
[](https://hicstuff.readthedocs.io)
[](https://mybinder.org/v2/gh/koszullab/hicstuff/master?filepath=doc%2Fnotebooks%2Fdemo_api.ipynb)
[](https://github.com/ambv/black)
A lightweight library that generates and handles Hi-C contact maps in either cooler-compatible 2Dbedgraph or [instaGRAAL](https://github.com/koszullab/instaGRAAL) format. It is essentially a merge of the [yahcp](https://github.com/baudrly/yahcp) pipeline, the [hicstuff](https://github.com/baudrly/hicstuff) library and extra features illustrated in the [3C tutorial](https://github.com/axelcournac/3C_tutorial) and the [DADE pipeline](https://github.com/scovit/dade), all packaged together for extra convenience.
The goal is to make generation and manipulation of Hi-C matrices as simple as possible and work for any organism.
## Table of contents
* [Installation](#Installation)
* [Usage](#Usage)
* [Full pipeline](#Full-pipeline)
* [Individual components](#Individual-components)
* [Library](#Library)
* [Connecting the modules](#Connecting-the-modules)
* [File formats](#File-formats)
* [Contributing](#Contributing)
## Installation
To install a stable version:
```sh
pip3 install -U hicstuff
```
or, for the latest development version:
```sh
pip3 install -e git+https://github.com/koszullab/hicstuff.git@master#egg=hicstuff
```
### External dependencies
Bowtie2 and/or minimap2 as well as samtools are required for the `pipeline` utility.
You can install them via the conda package manager:
```bash
conda install -c bioconda minimap2 bowtie2 samtools
```
Alternatively, on ubuntu you can also install them along with additional dependencies through APT:
```bash
apt-get install samtools bowtie2 minimap2 libbz2-dev liblzma-dev
```
### Docker installation
A pre-built docker image is available on dockerhub and can be ran using:
```bash
docker run koszullab/hicstuff
```
## Usage
The hicstuff command line interface is composed of multiple subcommands. You can always get a summary of all available commands by running:
```bash
hicstuff --help
Simple Hi-C pipeline for generating and manipulating contact matrices.
usage:
hicstuff [-hv] <command> [<args>...]
options:
-h, --help shows the help
-v, --version shows the version
The subcommands are:
digest Digest genome into a list of fragments.
distancelaw Analyse and plot distance law.
filter Filters Hi-C pairs to exclude spurious events.
iteralign Iteratively aligns reads to a reference genome.
pipeline Hi-C pipeline to generate contact matrix from fastq files.
rebin Bin the matrix and regenerate files accordingly.
subsample Bootstrap subsampling of contacts from a Hi-C map.
view Visualize a Hi-C matrix.
```
### Full pipeline
All components of the pipeline can be run at once using the `hicstuff pipeline` command. This allows to generate a contact matrix from reads in a single command. By default, the output is in GRAAL compatible COO sparse matrix format, but it can be a 2D bedgraph or cool file instead using the `--matfmt` option. More detailed documentation can be found on the readthedocs website: https://hicstuff.readthedocs.io/en/latest/index.html
usage:
pipeline [--quality-min=INT] [--size=INT] [--no-cleanup] [--start-stage=STAGE]
[--threads=INT] [--aligner=bowtie2] [--matfmt=FMT] [--prefix=PREFIX]
[--tmpdir=DIR] [--iterative] [--outdir=DIR] [--filter] [--enzyme=ENZ]
[--plot] [--circular] [--distance-law] [--duplicates] [--read-len=INT]
[--centromeres=FILE] [--remove-centromeres=INT] --genome=FILE <input1> [<input2>]
arguments:
input1: Forward fastq file, if start_stage is "fastq", sam
file for aligned forward reads if start_stage is
"bam", or a .pairs file if start_stage is "pairs".
input2: Reverse fastq file, if start_stage is "fastq", sam
file for aligned reverse reads if start_stage is
"bam", or nothing if start_stage is "pairs".
For example, to run the pipeline with minimap2 using 8 threads and generate a matrix in instagraal format in the directory `out`:
```
hicstuff pipeline -t 8 -a minimap2 -e DpnII -o out/ -g genome.fa reads_for.fq reads_rev.fq
```
If you have already aligned your reads, hicstuff pipeline can also take bam files as input. For example,
to generate a matrix in cool format with a fixed bin size of 5kb:
```
# Note the bam files have to be name-sorted, this can be done using samtools
samtools sort -n aligned_for.bam -o namesorted_for.bam
samtools sort -n aligned_rev.bam -o namesorted_rev.bam
hicstuff pipeline -S bam -e 5000 -M cool -o out/ -g genome.fa namesorted_for.bam namesorted_rev.bam
```
The pipeline can also be run from python, using the `hicstuff.pipeline` submodule. For example, this would run the pipeline with bowtie2 (default) using iterative alignment and keep all intermediate files. For more examples using the API, see the [API demo](https://hicstuff.readthedocs.io/en/latest/notebooks/demo_api.html)
```python
from hicstuff import pipeline as hpi
hpi.full_pipeline(
'genome.fa',
'end1.fq',
'end2.fq',
no_cleanup=True
iterative=True
out_dir='out',
enzyme="DpnII")
```
The general steps of the pipeline are as follows:

### Individual components
For more advanced usage, different scripts can be used independently on the command line to perform individual parts of the pipeline. This readme contains quick descriptions and example usages. To obtain detailed instructions on any subcommand, one can use `hicstuff <subcommand> --help`.
#### Iterative alignment
Truncate reads from a fastq file to 20 basepairs and iteratively extend and re-align the unmapped reads to optimize the proportion of uniquely aligned reads in a 3C library.
usage:
iteralign [--aligner=bowtie2] [--threads=1] [--min_len=20]
[--tempdir DIR] --out_sam=FILE --genome=FILE <reads.fq>
#### Digestion of the genome
Digests a fasta file into fragments based on a restriction enzyme or a
fixed chunk size. Generates two output files into the target directory
named "info_contigs.txt" and "fragments_list.txt"
usage:
digest [--plot] [--figdir=FILE] [--circular] [--size=INT]
[--outdir=DIR] --enzyme=ENZ <fasta>
For example, to digest the yeast genome with MaeII and HinfI and show histogram of fragment lengths:
`hicstuff digest --plot --outdir output_dir --enzyme MaeII,HinfI Sc_ref.fa`
#### Filtering of 3C events
Filters spurious 3C events such as loops and uncuts from the library based on a minimum distance threshold automatically estimated from the library by default. Can also plot 3C library statistics. This module takes a pairs file with 9 columns as input (readID, chr1, pos1, chr2, pos2, strand1, strand2, frag1, frag2) and filters it.
usage:
filter [--interactive | --thresholds INT-INT] [--plot]
[--figdir FILE] <input.pairs> <output.pairs>
#### Viewing the contact map
Visualize a Hi-C matrix file as a heatmap of contact frequencies. Allows to tune visualisation by binning and normalizing the matrix, and to save the output image to disk. If no output is specified, the output is displayed interactively. If two contact maps are provided, the log ratio of the first divided by the second will be shown.
usage:
view [--binning=1] [--despeckle] [--frags FILE] [--trim INT] [--n-mad FLOAT]
[--normalize] [--max=99] [--output=IMG] [--cmap=CMAP] [--dpi=INT]
[--transform=FUN] [--circular] [--region=STR] <contact_map> [<contact_map2>]
arguments:
contact_map Sparse contact matrix in bg2, cool or graal format
contact_map2 Sparse contact matrix in bg2, cool or graal format,
if given, the log ratio of contact_map/contact_map2
will be shown.
For example, to view a 1Mb region of chromosome 1 from a full genome Hi-C matrix rebinned at 10kb:
```sh
hicstuff view --normalize --binning 10kb --region chr1:10,000,000-11,000,000 --frags fragments_list.txt contact_map.tsv
```
### Library
All components of the hicstuff program can be used as python modules. See the documentation on [reathedocs](https://hicstuff.readthedocs.io). The expected contact map format for the library is a simple CSV file, and the objects handled by the library are simple ```numpy``` arrays. The various submodules of hicstuff contain various utilities.
```python
import hicstuff.digest # Functions to work with restriction fragments
import hicstuff.iteralign # Functions related to iterative alignment
import hicstuff.hicstuff # Contains utilities to modify and operate on contact maps as numpy arrays
import hicstuff.filter # Functions for filtering 3C events by type (uncut, loop)
import hicstuff.view # Utilities to visualise contact maps
import hicstuff.io # Reading and writing hicstuff files
import hicstuff.pipeline # Generation and processing of files to generate matrices.
```
### Connecting the modules
All the steps described here are handled automatically when running the `hicstuff pipeline`. But if you want to connect the different modules manually, the intermediate input and output files can be processed using some python scripting.
#### Aligning the reads
You can generate SAM files independently using your favorite read mapping software, use the command line utility `hicstuff iteralign`, or use the helper function `align_reads` in the submodule `hicstuff.pipeline`. For example, to perform iterative alignment using minimap2 (instead of bowtie2 by default):
**Using the python function:**
```python
from hicstuff import pipeline as hpi
hpi.align_reads("end1.fastq", "genome.fasta", "end1.bam", iterative=True, minimap2=True)
```
**Using the command line tool:**
```bash
hicstuff iteralign --minimap2 --iterative -f genome.fasta -o end1.sam end1.fastq
```
#### Extracting contacts from the alignment
The output from `hicstuff iteralign` is a SAM file. In order to retrieve Hi-C pairs, you need to run iteralign separately on the two fastq files and process the resulting alignment files into a name-sorted BAM file as follows using the `pipeline` submodules of hicstuff.
```python
from hicstuff import pipeline as hpi
import pysam as ps
# Sort alignments by read names and get into BAM format
ps.sort("-n", "-O", "BAM", "-o", "end1.bam.sorted", "end1.sam")
ps.sort("-n", "-O", "BAM", "-o", "end2.bam.sorted", "end2.sam")
# Combine BAM files
hpi.bam2pairs("end1.sorted.bam", "end2.sorted.bam", "output.pairs", "info_contigs.txt", min_qual=30)
```
This will generate a "pairs" file containing all read pairs where both reads have been aligned with a mapping quality of at least 30.
#### Attributing each read to a restriction fragment
To build a a contact matrix, we need to attribute each read to a fragment in the genome. This is done under the hood by performing a binary search for each read position against the list of restriction sites in the genome.
```python
from hicstuff import digest as hcd
from Bio import SeqIO
# Build a list of restriction sites for each chromosome
restrict_table = {}
for record in SeqIO.parse("genome.fasta", "fasta"):
# Get chromosome restriction table
restrict_table[record.id] = hcd.get_restriction_table(
record.seq, enzyme, circular=circular
)
# Add fragment index to pairs (readID, chr1, pos1, chr2,
# pos2, strand1, strand2, frag1, frag2)
hcd.attribute_fragments("output.pairs", "output_indexed.pairs", restrict_table)
```
#### Filtering pairs
The resulting pairs file can then be filtered, either in the command line using the `hicstuff filter` command, or in python using the `hicstuff.filter` submodule. Otherwise, the matrix can be built directly from the unfiltered pairs.
**Filtering on the command line:**
```bash
hicstuff filter output_indexed.pairs output_filtered.pairs
```
**Filtering in python:**
```python
from hicstuff import filter as hcf
uncut_thr, loop_thr = hcf.get_thresholds("output_indexed.pairs")
hcf.filter_events("output_indexed.pairs", "output_filtered.pairs", uncut_thr, loop_thr)
```
Note that both the command and the python function have various options to generate figure or tweak the filtering thresholds. These options can be displayed using `hicstuff filter -h`
#### Matrix generation
A Hi-C sparse contact matrix can then be generated using the python submodule `hicstuff.pipeline`. The matrix can be generated in GRAAL-compatible COO format, bedgraph2 or cool format.
```python
from hicstuff import pipeline as hpi
n_frags = sum(1 for line in open(fragments_list, "r")) - 1
hpi.pairs2matrix("output_filtered.pairs", "abs_fragments_contacts_weighted.txt", 'fragments_list.txt', mat_fmt="GRAAL")
```
### File formats
* pairs files: This format is used for all intermediate files in the pipeline and is also used by `hicstuff filter`. It is a tab-separated format holding informations about Hi-C pairs. It has an [official specification](https://github.com/4dn-dcic/pairix/blob/master/pairs_format_specification.md) defined by the 4D Nucleome data coordination and integration center.
* 2D bedgraph: This is an optional output format of `hicstuff pipeline` for the sparse matrix. It has two fragment per line, and the number of times they are found together. It has the following fields: **chr1, start1, end1, chr2, start2, end2, occurences**
- Those files can be [loaded by cooler](https://cooler.readthedocs.io/en/latest/cli.html?highlight=load#cooler-load) using `cooler load -f bg2 <chrom.sizes>:<binsize> in.bg2.gz out.cool` where chrom.sizes is a tab delimited file with chromosome names and length on each line, and binsize is the size of bins in the matrix.
* GRAAL sparse matrix: This is a simple tab-separated file with 3 columns: **frag1, frag2, contacts**. The id columns correspond to the absolute id of the restriction fragments (0-indexed). The first row is a header containing the number of rows, number of columns and number of nonzero entries in the matrix. Example:
```
564 564 6978
0 0 3
1 2 4
1 3 3
```
* fragments_list.txt: This tab separated file provides information about restriction fragments positions, size and GC content. Note the coordinates are 0 point basepairs, unlike the pairs format, which has 1 point basepairs. Example:
- id: 1 based restriction fragment index within chromosome.
- chrom: Chromosome identifier. Order should be the same as in info_contigs.txt or pairs files.
- start_pos: 0-based start of fragment, in base pairs.
- end_pos: 0-based end of fragment, in base pairs.
- size: Size of fragment, in base pairs.
- gc_content: Proportion of G and C nucleotide in the fragment.
```
id chrom start_pos end_pos size gc_content
1 seq1 0 21 21 0.5238095238095238
2 seq1 21 80 59 0.576271186440678
3 seq1 80 328 248 0.5201612903225806
```
* info_contigs.txt: This tab separated file gives information on contigs, such as number of restriction fragments and size. Example:
- contig: Chromosome identified. Order should be the same in pairs files or fragments_list.txt.
- length: Chromosome length, in base pairs.
- n_frags: Number of restriction fragments in chromosome.
- cumul_length: Cumulative length of previous chromosome, in base pairs.
```
contig length n_frags cumul_length
seq1 60000 409 0
seq2 20000 155 409
```
### Contributing
All contributions are welcome, in the form of bug reports, suggestions, documentation or pull requests.
We use the [numpy standard](https://numpydoc.readthedocs.io/en/latest/format.html) for docstrings when documenting functions.
The code formatting standard we use is [black](https://github.com/psf/black), with --line-length=79 to follow PEP8 recommendations. We use `pytest` with the `pytest-doctest` and `pytest-pylint` plugins as our testing framework. Ideally, new functions should have associated unit tests, placed in the `tests` folder.
To test the code, you can run:
```bash
pytest --doctest-modules --pylint --pylint-error-types=EF --pylint-rcfile=.pylintrc hicstuff tests
```
### Citation
Please cite hicstuff using the official DOI as follows:
Cyril Matthey-Doret, Lyam Baudry, Amaury Bignaud, Axel Cournac, Remi-Montagne, Nadège Guiglielmoni, Théo Foutel Rodier and Vittore F. Scolari. 2020. hicstuff: Simple library/pipeline to generate and handle Hi-C data . Zenodo. http://doi.org/10.5281/zenodo.4066363
Bibtex entry:
```
@software{cyril_matthey_doret_2020_4066351,
author = {Cyril Matthey-Doret and
Lyam Baudry and
Amaury Bignaud and
Axel Cournac and
Remi-Montagne and
Nadège Guiglielmoni and
Théo Foutel-Rodier and
Vittore F. Scolari},
title = {hicstuff: Simple library/pipeline to generate and handle Hi-C data },
month = oct,
year = 2020,
publisher = {Zenodo},
version = {v2.3.1},
doi = {10.5281/zenodo.4066351},
url = {http://doi.org/10.5281/zenodo.4066363}
}
```
%prep
%autosetup -n hicstuff-3.1.5
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-hicstuff -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Fri May 05 2023 Python_Bot <Python_Bot@openeuler.org> - 3.1.5-1
- Package Spec generated
|