|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import pandas as pd\n", |
| 10 | + "import matplotlib.pyplot as plt\n", |
| 11 | + "import numpy as np\n", |
| 12 | + "import seaborn as sns\n", |
| 13 | + "%matplotlib inline\n", |
| 14 | + "import os\n", |
| 15 | + "from aicsimageio import AICSImage\n", |
| 16 | + "os.chdir('Path_To_Directory_With_TIFF_and_CSV_Files')" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 2, |
| 22 | + "metadata": {}, |
| 23 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "# Define TrackMate_Quant function, can collapse code after running first time\n", |
| 26 | + "\n", |
| 27 | + "def TrackMate_Quant(spots, img1, Z_um, Y_um, X_um, Threshold, Pct_Threshold):\n", |
| 28 | + " # Convert um positions back to pixel coordinates\n", |
| 29 | + " spots['Pixel_Z'] = spots.POSITION_Z / Z_um\n", |
| 30 | + " spots['Pixel_Y'] = spots.POSITION_Y / Y_um\n", |
| 31 | + " spots['Pixel_X'] = spots.POSITION_X / X_um\n", |
| 32 | + " \n", |
| 33 | + " # Round to integers Drop any puncta that lands on edge pixels (indexed Pixel must be > 0 and < -1 than length of the array)\n", |
| 34 | + " print(f'Spots before edge filtering: {spots.shape}')\n", |
| 35 | + " spotsF = spots.loc[:,['Pixel_Z','Pixel_Y','Pixel_X']].round(decimals=0)\n", |
| 36 | + " spotsF = spotsF.loc[((spotsF.Pixel_Z < (len(img1[0])-1)) & (spotsF.Pixel_Z > 0))&\n", |
| 37 | + " ((spotsF.Pixel_Y < (len(img1[0][0])-1)) & (spotsF.Pixel_Y > 0))&\n", |
| 38 | + " ((spotsF.Pixel_X < (len(img1[0][0][0])-1)) & (spotsF.Pixel_X > 0)),:]\n", |
| 39 | + " print(f'Spots after edge filtering: {spotsF.shape}')\n", |
| 40 | + " # Get numpy array of 3D coordinates [Z, Y, X], convert to np.int64\n", |
| 41 | + " spots_coord = spotsF.to_numpy()\n", |
| 42 | + " spots_coord_round = spots_coord.astype(np.int64)\n", |
| 43 | + " \n", |
| 44 | + " # Calcualte image volume and store (exlcuding all edge pixels, so must subtract 2 from each dimension)\n", |
| 45 | + " Pixel_Vol = Z_um*Y_um*X_um\n", |
| 46 | + " Total_Pixels = (len(img1[0])-2)*len(img1[0][0]-2)*len(img1[0][0][0]-2)\n", |
| 47 | + " Total_Vol = Total_Pixels*Pixel_Vol\n", |
| 48 | + " Neurite_Pixels = np.count_nonzero(img1[0] > Threshold)\n", |
| 49 | + " Neurite_Vol = Neurite_Pixels*Pixel_Vol\n", |
| 50 | + " Neurite_Vol_Pct = Neurite_Vol/Total_Vol\n", |
| 51 | + " \n", |
| 52 | + " # Initialize empty list for output\n", |
| 53 | + " RNAlist = []\n", |
| 54 | + " for i in list(np.arange(0,len(spotsF))):\n", |
| 55 | + " Z = spots_coord_round[i][0]\n", |
| 56 | + " Y = spots_coord_round[i][1]\n", |
| 57 | + " X = spots_coord_round[i][2]\n", |
| 58 | + " RNAlist.append([\n", |
| 59 | + " img1[0][Z][Y][X],img1[0][Z][Y-1][X+1],img1[0][Z][Y][X+1],img1[0][Z][Y+1][X+1],img1[0][Z][Y-1][X-1],\n", |
| 60 | + " img1[0][Z][Y][X-1],img1[0][Z][Y+1][X-1],img1[0][Z][Y-1][X],img1[0][Z][Y+1][X],img1[0][Z+1][Y][X],\n", |
| 61 | + " img1[0][Z+1][Y][X+1],img1[0][Z+1][Y][X-1],img1[0][Z+1][Y-1][X],img1[0][Z+1][Y+1][X],img1[0][Z-1][Y][X],\n", |
| 62 | + " img1[0][Z-1][Y][X+1],img1[0][Z-1][Y][X-1],img1[0][Z-1][Y-1][X],img1[0][Z-1][Y+1][X]\n", |
| 63 | + " ])\n", |
| 64 | + " ##\n", |
| 65 | + " RNA_Pct_list = []\n", |
| 66 | + " for i in list(np.arange(0,len(spotsF))):\n", |
| 67 | + " RNA_Pct_list.append(sum(1 for x in RNAlist[i] if (x > Threshold))/len(RNAlist[i]))\n", |
| 68 | + " # Output\n", |
| 69 | + " SpotCount = len(RNAlist)\n", |
| 70 | + " SpotCount_Pos = sum(1 for x in RNA_Pct_list if (x > Pct_Threshold))\n", |
| 71 | + " PosSpots_Percent = SpotCount_Pos/SpotCount\n", |
| 72 | + " SpotCount_Vol = SpotCount/Total_Vol\n", |
| 73 | + " SpotCount_Neurite_Vol = SpotCount/Neurite_Vol\n", |
| 74 | + " #\n", |
| 75 | + " return [SpotCount, SpotCount_Pos, PosSpots_Percent, SpotCount_Vol, SpotCount_Neurite_Vol, Total_Pixels, Total_Vol, Neurite_Pixels, Neurite_Vol, Neurite_Vol_Pct]" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 3, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "# Set directory for files\n", |
| 85 | + "directory = r'/Path_To_Directory_With_TIFF_and_CSV_Files'\n", |
| 86 | + "Tif_Paths = []\n", |
| 87 | + "RNA1_Paths = []\n", |
| 88 | + "RNA2_Paths = []\n", |
| 89 | + "\n", |
| 90 | + "#\n", |
| 91 | + "for filename in os.listdir(directory):\n", |
| 92 | + " if filename.endswith(\".tif\"):\n", |
| 93 | + " Tifp = os.path.join(directory, filename)\n", |
| 94 | + " Tif_Paths.append(Tifp)\n", |
| 95 | + " else:\n", |
| 96 | + " if filename.endswith(\"RNA1.csv\"):\n", |
| 97 | + " RNA1_sp = os.path.join(directory, filename)\n", |
| 98 | + " RNA1_Paths.append(RNA1_sp)\n", |
| 99 | + " else:\n", |
| 100 | + " if filename.endswith(\"RNA2.csv\"):\n", |
| 101 | + " RNA2_sp = os.path.join(directory, filename)\n", |
| 102 | + " RNA2_Paths.append(RNA2_sp)" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": 4, |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [], |
| 110 | + "source": [ |
| 111 | + "# YOU MUST ENTER THIS INFORMATION FROM TRACKMATE BEGINNING SCREEN IN ORDER TO CONVERT THE UM BACK TO PIXEL COORDINATES\n", |
| 112 | + "# IT MUST BE THE SAME FOR ALL IMAGES / TRACKMATE SPOT OUTPUT TO BE ANALYZED\n", |
| 113 | + "Z_um = 0.300\n", |
| 114 | + "Y_um = 0.180\n", |
| 115 | + "X_um = 0.180\n", |
| 116 | + "\n", |
| 117 | + "# Set Neurite+ pixel intensity threshold\n", |
| 118 | + "Threshold = 75\n", |
| 119 | + "# Set % of Neurite+ pixels threshold for spot calling\n", |
| 120 | + "Pct_Threshold = 0.6\n", |
| 121 | + "\n", |
| 122 | + "# Initiliaze empty list to append output to\n", |
| 123 | + "RNA1_DataList = []\n", |
| 124 | + "RNA2_DataList = []" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": 5, |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [ |
| 132 | + { |
| 133 | + "name": "stdout", |
| 134 | + "output_type": "stream", |
| 135 | + "text": [ |
| 136 | + "Spots before edge filtering: (379, 33)\n", |
| 137 | + "Spots after edge filtering: (336, 3)\n", |
| 138 | + "Spots before edge filtering: (420, 33)\n", |
| 139 | + "Spots after edge filtering: (351, 3)\n", |
| 140 | + "Spots before edge filtering: (316, 33)\n", |
| 141 | + "Spots after edge filtering: (268, 3)\n", |
| 142 | + "Spots before edge filtering: (395, 33)\n", |
| 143 | + "Spots after edge filtering: (336, 3)\n" |
| 144 | + ] |
| 145 | + } |
| 146 | + ], |
| 147 | + "source": [ |
| 148 | + "for i in list(np.arange(0,len(Tif_Paths))):\n", |
| 149 | + " img = AICSImage(Tif_Paths[i]) # Read in Tiff Image as 6D Numpy Array\n", |
| 150 | + " img1 = img.data[0][0] # Strip extra dimensions (Scene and Time)\n", |
| 151 | + " #\n", |
| 152 | + " # Read in RNA1 Spots first, run TrackMate_Quant, append RNA1 data to list\n", |
| 153 | + " spots = pd.read_csv(RNA1_Paths[i],sep=',',index_col=0) \n", |
| 154 | + " RNA1_store = TrackMate_Quant(spots, img1, Z_um, Y_um, X_um, Threshold, Pct_Threshold) \n", |
| 155 | + " RNA1_DataList.append(RNA1_store)\n", |
| 156 | + " # Read in RNA2 Spots next, run TrackMate_Quant, append RNA2 data to list\n", |
| 157 | + " spots = pd.read_csv(RNA2_Paths[i],sep=',',index_col=0) \n", |
| 158 | + " RNA2_store = TrackMate_Quant(spots, img1, Z_um, Y_um, X_um, Threshold, Pct_Threshold) \n", |
| 159 | + " RNA2_DataList.append(RNA2_store)" |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "code", |
| 164 | + "execution_count": 6, |
| 165 | + "metadata": {}, |
| 166 | + "outputs": [], |
| 167 | + "source": [ |
| 168 | + "RNA1_Spot_Data = pd.DataFrame(RNA1_DataList,columns=['SpotCount', 'SpotCount_Pos', 'PosSpots_Percent', \n", |
| 169 | + " 'SpotCount_Vol', 'SpotCount_Neurite_Vol', 'Total_Pixels', \n", |
| 170 | + " 'Total_Vol', 'Neurite_Pixels', 'Neurite_Vol', 'Neurite_Vol_Pct'])\n", |
| 171 | + "#\n", |
| 172 | + "RNA2_Spot_Data = pd.DataFrame(RNA2_DataList,columns=['SpotCount', 'SpotCount_Pos', 'PosSpots_Percent', \n", |
| 173 | + " 'SpotCount_Vol', 'SpotCount_Neurite_Vol', 'Total_Pixels', \n", |
| 174 | + " 'Total_Vol', 'Neurite_Pixels', 'Neurite_Vol', 'Neurite_Vol_Pct'])\n", |
| 175 | + "#\n", |
| 176 | + "RNA1_Spot_Data['RNA'] = 'RNA1'\n", |
| 177 | + "RNA2_Spot_Data['RNA'] = 'RNA2'\n", |
| 178 | + "MergePlot = pd.concat([RNA1_Spot_Data, RNA2_Spot_Data])\n", |
| 179 | + "MergePlot['SpotCountPos_Vol_10um3'] = MergePlot.SpotCountPos_Vol * 1e3\n", |
| 180 | + "MergePlot['SpotCountPos_Neurite_Vol_10um3'] = MergePlot.SpotCountPos_Neurite_Vol * 1e3\n", |
| 181 | + "#\n", |
| 182 | + "pattern = \"(.*?).tif\"\n", |
| 183 | + "Filenames = []\n", |
| 184 | + "for i in list(np.arange(0,len(Tif_Paths))):\n", |
| 185 | + " s = Tif_Paths[i]\n", |
| 186 | + " substring = re.search(pattern, s).group(1)\n", |
| 187 | + " Filenames.append(substring)\n", |
| 188 | + "Filenames2 = Filenames + Filenames\n", |
| 189 | + "MergePlot['Files'] = Filenames2" |
| 190 | + ] |
| 191 | + }, |
| 192 | + { |
| 193 | + "cell_type": "code", |
| 194 | + "execution_count": null, |
| 195 | + "metadata": {}, |
| 196 | + "outputs": [], |
| 197 | + "source": [ |
| 198 | + "# Write Output\n", |
| 199 | + "#MergePlot.to_csv('perField_RNA1_RNA2_MergeOutput.csv',sep=',')" |
| 200 | + ] |
| 201 | + } |
| 202 | + ], |
| 203 | + "metadata": { |
| 204 | + "kernelspec": { |
| 205 | + "display_name": "Python 3", |
| 206 | + "language": "python", |
| 207 | + "name": "python3" |
| 208 | + }, |
| 209 | + "language_info": { |
| 210 | + "codemirror_mode": { |
| 211 | + "name": "ipython", |
| 212 | + "version": 3 |
| 213 | + }, |
| 214 | + "file_extension": ".py", |
| 215 | + "mimetype": "text/x-python", |
| 216 | + "name": "python", |
| 217 | + "nbconvert_exporter": "python", |
| 218 | + "pygments_lexer": "ipython3", |
| 219 | + "version": "3.7.3" |
| 220 | + } |
| 221 | + }, |
| 222 | + "nbformat": 4, |
| 223 | + "nbformat_minor": 4 |
| 224 | +} |
0 commit comments