Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packaging and csv export #3

Merged
merged 18 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
prete marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ Homepage = "https://github.com/cellgeni/cloupe"
Issues = "https://github.com/cellgeni/cloupe/issues"

[tool.hatch.version]
path = "src/cloupe"
path = "src/cloupe/__init__.py"
44 changes: 33 additions & 11 deletions src/cloupe/cloupe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import logging
import struct
import zlib
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# import matplotlib.pyplot as plt
# import matplotlib.pyplot as plt
# import matplotlib.image as mpimg


# https://www.10xgenomics.com/datasets/visium-cytassist-gene-and-protein-expression-library-of-human-tonsil-with-add-on-antibodies-h-e-6-5-mm-ffpe-2-standard
Expand Down Expand Up @@ -200,7 +200,7 @@ def barcodes_writer(self):
csv_writer = csv.writer(barcodes_file)
csv_writer.writerow(["Barcodes"])
csv_writer.writerows(
[[element] for element in self.matrices[0]['Barcodes'][:self.matrices[0]['BarcodeCount']]]
[[element] for element in self.matrices[0]['Barcodes']]
) # Efficient one line code

# write the features to the csv file
Expand All @@ -211,8 +211,8 @@ def features_writer(self):
csv_writer.writerow(["FeatureIds", "FeatureNames"])
csv_writer.writerows([[str(element) for element in pair] # Remove parentheses, single quotes, and spaces
for pair in zip(
self.matrices[0]["FeatureIds"][:self.matrices[0]["FeatureCount"]],
self.matrices[0]["FeatureNames"][:self.matrices[0]["FeatureCount"]],
self.matrices[0]["FeatureIds"],
self.matrices[0]["FeatureNames"],
)])# Using zip command along with list comprehension to convert the two list into strings for the csv as elements

# write annotations
Expand All @@ -232,19 +232,41 @@ def annotations_writer(self):
)

# plotting the spatial info
def spatial_plot(self):
def spatial_projection(self):
spatial_plot = self.cwd + '/projection_spatial.csv'
spatial_embedding = self.projections['Spatial']
plt.scatter(x=spatial_embedding[0], y=spatial_embedding[1])
plt.gca().invert_yaxis()
plt.show()
projections_1 = [["Barcodes"]+self.matrices[0]["Barcodes"]]
for column in range(len(spatial_embedding)):
projections_1.append(["d{}".format(column)]+[element for element in spatial_embedding[column]])
transposed_projections_1 = zip(*projections_1)
with open(spatial_plot, "w", newline="") as spatial_projection_file:
csv_writer = csv.writer(spatial_projection_file)
csv_writer.writerows(transposed_projections_1)

# plt.scatter(x=spatial_embedding[0], y=spatial_embedding[1])
# plt.gca().invert_yaxis()
# plt.show()

# plotting the tsne info
def tsne_projection(self):
tsne_plot = self.cwd + '/projection_tsne.csv'
tsne_embedding = self.projections['tsne']
projections_2 = [["Barcodes"] + self.matrices[0]["Barcodes"]]
for column in range(len(tsne_embedding)):
projections_2.append(["d{}".format(column)] + [element for element in tsne_embedding[column]])
transposed_projections_2 = zip(*projections_2)
with open(tsne_plot, "w", newline="") as tsne_projection_file:
csv_writer = csv.writer(tsne_projection_file)
csv_writer.writerows(transposed_projections_2)
prete marked this conversation as resolved.
Show resolved Hide resolved


if __name__=="__main__":
logging.basicConfig(level=logging.INFO, format="[%(asctime)s][%(levelname)s] %(message)s")
# cloupe_object = Cloupe("/Users/nj9/Downloads/spaceranger210_count_49384_pSKI_SP15018739_GRCh38-2020-A.cloupe")
cloupe_object = Cloupe("/Users/nj9/Downloads/spaceranger210_count_49384_pSKI_SP15018739_GRCh38-2020-A.cloupe")
# cloupe_object.barcodes_writer()
# cloupe_object.features_writer()
# cloupe_object.annotations_writer()
# cloupe_object.spatial_projection()
# Bye-bye


Expand Down