|
5 | 5 | import org.apache.commons.io.FileUtils; |
6 | 6 | import org.apache.commons.lang3.StringUtils; |
7 | 7 | import org.apache.commons.lang3.tuple.Pair; |
| 8 | +import org.apache.logging.log4j.Logger; |
8 | 9 | import org.jetbrains.annotations.Nullable; |
9 | 10 | import org.json.JSONObject; |
10 | 11 | import org.labkey.api.collections.CaseInsensitiveHashMap; |
|
47 | 48 | import org.labkey.api.writer.PrintWriters; |
48 | 49 | import org.labkey.singlecell.SingleCellSchema; |
49 | 50 |
|
| 51 | +import java.io.BufferedReader; |
50 | 52 | import java.io.File; |
51 | 53 | import java.io.IOException; |
| 54 | +import java.nio.file.Files; |
52 | 55 | import java.util.ArrayList; |
53 | 56 | import java.util.Arrays; |
54 | 57 | import java.util.Collection; |
55 | 58 | import java.util.Date; |
56 | 59 | import java.util.HashMap; |
57 | 60 | import java.util.List; |
58 | 61 | import java.util.Map; |
| 62 | +import java.util.regex.Matcher; |
| 63 | +import java.util.regex.Pattern; |
59 | 64 |
|
60 | 65 | public class CellRangerGexCountStep extends AbstractAlignmentPipelineStep<CellRangerWrapper> implements AlignmentStep |
61 | 66 | { |
@@ -613,4 +618,88 @@ public void complete(SequenceAnalysisJobSupport support, AnalysisModel model, Co |
613 | 618 | } |
614 | 619 | } |
615 | 620 | } |
| 621 | + |
| 622 | + public enum Chemistry |
| 623 | + { |
| 624 | + // See: https://kb.10xgenomics.com/s/article/115004506263-What-is-a-barcode-inclusion-list-formerly-barcode-whitelist |
| 625 | + // cellranger-x.y.z/lib/python/cellranger/barcodes/ |
| 626 | + FivePE_V3("Single Cell 5' PE v3", "3M-5pgex-jan-2023.txt.gz"), |
| 627 | + FivePE_V2("Single Cell 5' PE v2", "737k-august-2016.txt"); |
| 628 | + |
| 629 | + final String _label; |
| 630 | + final String _inclusionListFile; |
| 631 | + |
| 632 | + Chemistry(String label, String inclusionListFile) |
| 633 | + { |
| 634 | + _label = label; |
| 635 | + _inclusionListFile = inclusionListFile; |
| 636 | + } |
| 637 | + |
| 638 | + public File getInclusionListFile(Logger logger) throws PipelineJobException |
| 639 | + { |
| 640 | + File exe = new CellRangerWrapper(logger).getExe(); |
| 641 | + if (Files.isSymbolicLink(exe.toPath())) |
| 642 | + { |
| 643 | + try |
| 644 | + { |
| 645 | + exe = Files.readSymbolicLink(exe.toPath()).toFile(); |
| 646 | + } |
| 647 | + catch (IOException e) |
| 648 | + { |
| 649 | + throw new PipelineJobException(e); |
| 650 | + } |
| 651 | + } |
| 652 | + |
| 653 | + File il = new File(exe.getParentFile(), "lib/python/cellranger/barcodes/" + _inclusionListFile); |
| 654 | + if (!il.exists()) |
| 655 | + { |
| 656 | + throw new PipelineJobException("Unable to find file: " + il.getPath()); |
| 657 | + } |
| 658 | + |
| 659 | + return il; |
| 660 | + } |
| 661 | + |
| 662 | + public static Chemistry getByLabel(String label) |
| 663 | + { |
| 664 | + for (Chemistry c : Chemistry.values()) |
| 665 | + { |
| 666 | + if (c._label.equals(label)) |
| 667 | + { |
| 668 | + return c; |
| 669 | + } |
| 670 | + } |
| 671 | + |
| 672 | + throw new IllegalArgumentException("Unknown chemistry: " + label); |
| 673 | + } |
| 674 | + } |
| 675 | + |
| 676 | + public static Chemistry inferChemistry(File cloupeFile) throws PipelineJobException |
| 677 | + { |
| 678 | + File html = new File(cloupeFile.getPath().replaceAll("_cloupe.cloupe$", "_web_summary.html")); |
| 679 | + if (!html.exists()) |
| 680 | + { |
| 681 | + throw new IllegalArgumentException("Missing file: " + html.getPath()); |
| 682 | + } |
| 683 | + |
| 684 | + final Pattern pattern = Pattern.compile("\\[\"Chemistry\",\"(.*?)\"],"); |
| 685 | + try (BufferedReader reader = Readers.getReader(html)) |
| 686 | + { |
| 687 | + String line; |
| 688 | + while ((line = reader.readLine()) != null) |
| 689 | + { |
| 690 | + Matcher m = pattern.matcher(line); |
| 691 | + if (m.find()) |
| 692 | + { |
| 693 | + String chem = m.group(1); |
| 694 | + return Chemistry.getByLabel(chem); |
| 695 | + } |
| 696 | + } |
| 697 | + } |
| 698 | + catch (IOException e) |
| 699 | + { |
| 700 | + throw new PipelineJobException(e); |
| 701 | + } |
| 702 | + |
| 703 | + throw new IllegalArgumentException("Unable to infer chemistry for file: " + html.getPath()); |
| 704 | + } |
616 | 705 | } |
0 commit comments